Skip to content

Commit

Permalink
Update timedelta accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche committed Mar 7, 2017
1 parent d0ec6bf commit 76d1564
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
24 changes: 14 additions & 10 deletions pandas/tests/indexes/timedeltas/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ def test_total_seconds(self):
freq='s')
expt = [1 * 86400 + 10 * 3600 + 11 * 60 + 12 + 100123456. / 1e9,
1 * 86400 + 10 * 3600 + 11 * 60 + 13 + 100123456. / 1e9]
tm.assert_almost_equal(rng.total_seconds(), np.array(expt))
tm.assert_almost_equal(rng.total_seconds(), Index(expt))

# test Series
s = Series(rng)
Expand Down Expand Up @@ -486,16 +486,16 @@ def test_append_numpy_bug_1681(self):
def test_fields(self):
rng = timedelta_range('1 days, 10:11:12.100123456', periods=2,
freq='s')
self.assert_numpy_array_equal(rng.days, np.array(
[1, 1], dtype='int64'))
self.assert_numpy_array_equal(
self.assert_index_equal(rng.days, Index([1, 1], dtype='int64'))
self.assert_index_equal(
rng.seconds,
np.array([10 * 3600 + 11 * 60 + 12, 10 * 3600 + 11 * 60 + 13],
dtype='int64'))
self.assert_numpy_array_equal(rng.microseconds, np.array(
[100 * 1000 + 123, 100 * 1000 + 123], dtype='int64'))
self.assert_numpy_array_equal(rng.nanoseconds, np.array(
[456, 456], dtype='int64'))
Index([10 * 3600 + 11 * 60 + 12, 10 * 3600 + 11 * 60 + 13],
dtype='int64'))
self.assert_index_equal(
rng.microseconds,
Index([100 * 1000 + 123, 100 * 1000 + 123], dtype='int64'))
self.assert_index_equal(rng.nanoseconds,
Index([456, 456], dtype='int64'))

self.assertRaises(AttributeError, lambda: rng.hours)
self.assertRaises(AttributeError, lambda: rng.minutes)
Expand All @@ -509,6 +509,10 @@ def test_fields(self):
tm.assert_series_equal(s.dt.seconds, Series(
[10 * 3600 + 11 * 60 + 12, np.nan], index=[0, 1]))

# preserve name (GH15589)
rng.name = 'name'
assert rng.days.name == 'name'

def test_freq_conversion(self):

# doc example
Expand Down
5 changes: 3 additions & 2 deletions pandas/tseries/tdi.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def _get_field(self, m):
else:
result = np.array([getattr(Timedelta(val), m)
for val in values], dtype='int64')
return result
return Index(result, name=self.name)

@property
def days(self):
Expand Down Expand Up @@ -442,7 +442,8 @@ def total_seconds(self):
.. versionadded:: 0.17.0
"""
return self._maybe_mask_results(1e-9 * self.asi8)
return Index(self._maybe_mask_results(1e-9 * self.asi8),
name=self.name)

def to_pytimedelta(self):
"""
Expand Down

0 comments on commit 76d1564

Please sign in to comment.