Skip to content

Commit abe63d7

Browse files
committed
BUG: Adjust time values with Period objects in Series.dt.end_time
1 parent a18d725 commit abe63d7

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

doc/source/whatsnew/v0.23.0.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ Conversion
288288
- Bug in :class:`Series` constructor with an int or float list where specifying ``dtype=str``, ``dtype='str'`` or ``dtype='U'`` failed to convert the data elements to strings (:issue:`16605`)
289289
- Bug in :class:`Timestamp` where comparison with an array of ``Timestamp`` objects would result in a ``RecursionError`` (:issue:`15183`)
290290
- Bug in :class:`WeekOfMonth` and class:`Week` where addition and subtraction did not roll correctly (:issue:`18510`,:issue:`18672`,:issue:`18864`)
291-
291+
- Bug in :func:`Series.dt.end_time` where time values in ``Period`` objects were not adjusted (:issue:`17157`)
292292

293293
Indexing
294294
^^^^^^^^

pandas/core/indexes/period.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,8 @@ def start_time(self):
645645

646646
@property
647647
def end_time(self):
648-
return self.to_timestamp(how='end')
648+
data = (self + 1).start_time.values.astype(int) - 1
649+
return DatetimeIndex(data=data)
649650

650651
def _mpl_repr(self):
651652
# how to represent ourselves to matplotlib

pandas/tests/indexes/period/test_period.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,29 @@ def test_start_time(self):
458458
def test_end_time(self):
459459
index = PeriodIndex(freq='M', start='2016-01-01', end='2016-05-31')
460460
expected_index = date_range('2016-01-01', end='2016-05-31', freq='M')
461+
expected_index = expected_index.shift(1, freq='D').shift(-1, freq='ns')
461462
tm.assert_index_equal(index.end_time, expected_index)
462463

464+
@pytest.mark.parametrize('input_vals', [
465+
[Period('2016-01', freq='M'), Period('2016-02', freq='M')],
466+
[Period('2016-01-01', freq='D'), Period('2016-01-02', freq='D')],
467+
[Period('2016-01-01 00:00:00', freq='H'),
468+
Period('2016-01-01 01:00:00', freq='H')],
469+
[Period('2016-01-01 00:00:00', freq='M'),
470+
Period('2016-01-01 00:01:00', freq='M')],
471+
[Period('2016-01-01 00:00:00', freq='S'),
472+
Period('2016-01-01 00:00:01', freq='S')]
473+
])
474+
def test_end_time_timevalues(self, input_vals):
475+
# GH 17157
476+
# Check that the time part of the Period is adjusted by end_time
477+
# when using the dt accessor on a Series
478+
479+
s = Series(input_vals)
480+
result = s.dt.end_time
481+
expected = s.apply(lambda x: x.end_time)
482+
tm.assert_series_equal(result, expected)
483+
463484
def test_index_duplicate_periods(self):
464485
# monotonic
465486
idx = PeriodIndex([2000, 2007, 2007, 2009, 2009], freq='A-JUN')

0 commit comments

Comments
 (0)