Skip to content

Commit f2831e2

Browse files
Update timedelta accessors
1 parent 52f9008 commit f2831e2

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

pandas/tests/indexes/timedeltas/test_timedelta.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ def test_total_seconds(self):
424424
freq='s')
425425
expt = [1 * 86400 + 10 * 3600 + 11 * 60 + 12 + 100123456. / 1e9,
426426
1 * 86400 + 10 * 3600 + 11 * 60 + 13 + 100123456. / 1e9]
427-
tm.assert_almost_equal(rng.total_seconds(), np.array(expt))
427+
tm.assert_almost_equal(rng.total_seconds(), Index(expt))
428428

429429
# test Series
430430
s = Series(rng)
@@ -486,16 +486,16 @@ def test_append_numpy_bug_1681(self):
486486
def test_fields(self):
487487
rng = timedelta_range('1 days, 10:11:12.100123456', periods=2,
488488
freq='s')
489-
self.assert_numpy_array_equal(rng.days, np.array(
490-
[1, 1], dtype='int64'))
491-
self.assert_numpy_array_equal(
489+
self.assert_index_equal(rng.days, Index([1, 1], dtype='int64'))
490+
self.assert_index_equal(
492491
rng.seconds,
493-
np.array([10 * 3600 + 11 * 60 + 12, 10 * 3600 + 11 * 60 + 13],
494-
dtype='int64'))
495-
self.assert_numpy_array_equal(rng.microseconds, np.array(
496-
[100 * 1000 + 123, 100 * 1000 + 123], dtype='int64'))
497-
self.assert_numpy_array_equal(rng.nanoseconds, np.array(
498-
[456, 456], dtype='int64'))
492+
Index([10 * 3600 + 11 * 60 + 12, 10 * 3600 + 11 * 60 + 13],
493+
dtype='int64'))
494+
self.assert_index_equal(
495+
rng.microseconds,
496+
Index([100 * 1000 + 123, 100 * 1000 + 123], dtype='int64'))
497+
self.assert_index_equal(rng.nanoseconds,
498+
Index([456, 456], dtype='int64'))
499499

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

512+
# preserve name (GH15589)
513+
rng.name = 'name'
514+
assert rng.days.name == 'name'
515+
512516
def test_freq_conversion(self):
513517

514518
# doc example

pandas/tseries/tdi.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def _get_field(self, m):
374374
else:
375375
result = np.array([getattr(Timedelta(val), m)
376376
for val in values], dtype='int64')
377-
return result
377+
return Index(result, name=self.name)
378378

379379
@property
380380
def days(self):
@@ -437,7 +437,8 @@ def total_seconds(self):
437437
438438
.. versionadded:: 0.17.0
439439
"""
440-
return self._maybe_mask_results(1e-9 * self.asi8)
440+
return Index(self._maybe_mask_results(1e-9 * self.asi8),
441+
name=self.name)
441442

442443
def to_pytimedelta(self):
443444
"""

0 commit comments

Comments
 (0)