Skip to content

Commit 991f959

Browse files
committed
BUG: Adjust time values with Period objects in Series.dt.end_time
1 parent 8433562 commit 991f959

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
@@ -294,7 +294,7 @@ Conversion
294294
- Bug in :meth:`DatetimeIndex.astype` when converting between timezone aware dtypes, and converting from timezone aware to naive (:issue:`18951`)
295295
- Bug in :class:`FY5253` where ``datetime`` addition and subtraction incremented incorrectly for dates on the year-end but not normalized to midnight (:issue:`18854`)
296296
- Bug in :class:`DatetimeIndex` where adding or subtracting an array-like of ``DateOffset`` objects either raised (``np.array``, ``pd.Index``) or broadcast incorrectly (``pd.Series``) (:issue:`18849`)
297-
297+
- Bug in :func:`Series.dt.end_time` where time values in ``Period`` objects were not adjusted (:issue:`17157`)
298298

299299
Indexing
300300
^^^^^^^^

pandas/core/indexes/period.py

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

634634
@property
635635
def end_time(self):
636-
return self.to_timestamp(how='end')
636+
data = (self + 1).start_time.values.astype(int) - 1
637+
return DatetimeIndex(data=data)
637638

638639
def _mpl_repr(self):
639640
# 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
@@ -462,8 +462,29 @@ def test_start_time(self):
462462
def test_end_time(self):
463463
index = PeriodIndex(freq='M', start='2016-01-01', end='2016-05-31')
464464
expected_index = date_range('2016-01-01', end='2016-05-31', freq='M')
465+
expected_index = expected_index.shift(1, freq='D').shift(-1, freq='ns')
465466
tm.assert_index_equal(index.end_time, expected_index)
466467

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

0 commit comments

Comments
 (0)