Skip to content

Commit a23bae4

Browse files
committed
try to solve in to_timestamp and move tests
1 parent f41a6c2 commit a23bae4

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

pandas/core/indexes/period.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,8 +651,7 @@ def start_time(self):
651651

652652
@property
653653
def end_time(self):
654-
data = (self + 1).start_time.values.astype(int) - 1
655-
return DatetimeIndex(data=data)
654+
return self.to_timestamp(how='end')
656655

657656
def _mpl_repr(self):
658657
# how to represent ourselves to matplotlib
@@ -684,7 +683,16 @@ def to_timestamp(self, freq=None, how='start'):
684683
base, mult = _gfc(freq)
685684
new_data = self.asfreq(freq, how)
686685

687-
new_data = period.periodarr_to_dt64arr(new_data._ndarray_values, base)
686+
end = how == 'E'
687+
if end:
688+
indexer = np.where(new_data.notnull())
689+
# move forward one period
690+
new_data._values[indexer] += 1
691+
new_data = period.periodarr_to_dt64arr(new_data._values, base)
692+
# subtract one nanosecond
693+
new_data[indexer] -= 1
694+
else:
695+
new_data = period.periodarr_to_dt64arr(new_data._values, base)
688696
return DatetimeIndex(new_data, freq='infer', name=self.name)
689697

690698
def _maybe_convert_timedelta(self, other):

pandas/tests/series/test_period.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import pandas as pd
44
import pandas.util.testing as tm
55
import pandas.core.indexes.period as period
6-
from pandas import Series, period_range, DataFrame
7-
6+
from pandas import Series, period_range, DataFrame, Period
7+
import pytest
88

99
def _permute(obj):
1010
return obj.take(np.random.permutation(len(obj)))
@@ -167,3 +167,23 @@ def test_truncate(self):
167167
pd.Period('2017-09-02')
168168
])
169169
tm.assert_series_equal(result2, pd.Series([2], index=expected_idx2))
170+
171+
@pytest.mark.parametrize('input_vals', [
172+
[Period('2016-01', freq='M'), Period('2016-02', freq='M')],
173+
[Period('2016-01-01', freq='D'), Period('2016-01-02', freq='D')],
174+
[Period('2016-01-01 00:00:00', freq='H'),
175+
Period('2016-01-01 01:00:00', freq='H')],
176+
[Period('2016-01-01 00:00:00', freq='M'),
177+
Period('2016-01-01 00:01:00', freq='M')],
178+
[Period('2016-01-01 00:00:00', freq='S'),
179+
Period('2016-01-01 00:00:01', freq='S')]
180+
])
181+
def test_end_time_timevalues(self, input_vals):
182+
# GH 17157
183+
# Check that the time part of the Period is adjusted by end_time
184+
# when using the dt accessor on a Series
185+
186+
s = Series(input_vals)
187+
result = s.dt.end_time
188+
expected = s.apply(lambda x: x.end_time)
189+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)