Skip to content

Commit 3c5ea75

Browse files
committed
try to solve in to_timestamp and move tests
1 parent 991f959 commit 3c5ea75

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

pandas/core/indexes/period.py

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

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

639638
def _mpl_repr(self):
640639
# how to represent ourselves to matplotlib
@@ -666,7 +665,16 @@ def to_timestamp(self, freq=None, how='start'):
666665
base, mult = _gfc(freq)
667666
new_data = self.asfreq(freq, how)
668667

669-
new_data = period.periodarr_to_dt64arr(new_data._values, base)
668+
end = how == 'E'
669+
if end:
670+
indexer = np.where(new_data.notnull())
671+
# move forward one period
672+
new_data._values[indexer] += 1
673+
new_data = period.periodarr_to_dt64arr(new_data._values, base)
674+
# subtract one nanosecond
675+
new_data[indexer] -= 1
676+
else:
677+
new_data = period.periodarr_to_dt64arr(new_data._values, base)
670678
return DatetimeIndex(new_data, freq='infer', name=self.name)
671679

672680
def _maybe_convert_timedelta(self, other):

pandas/tests/indexes/period/test_period.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -465,26 +465,6 @@ def test_end_time(self):
465465
expected_index = expected_index.shift(1, freq='D').shift(-1, freq='ns')
466466
tm.assert_index_equal(index.end_time, expected_index)
467467

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-
488468
def test_index_duplicate_periods(self):
489469
# monotonic
490470
idx = PeriodIndex([2000, 2007, 2007, 2009, 2009], freq='A-JUN')

pandas/tests/series/test_period.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pandas.util.testing as tm
55
import pandas.core.indexes.period as period
66
from pandas import Series, period_range, DataFrame, Period
7-
7+
import pytest
88

99
def _permute(obj):
1010
return obj.take(np.random.permutation(len(obj)))
@@ -278,3 +278,23 @@ def test_truncate(self):
278278
pd.Period('2017-09-02')
279279
])
280280
tm.assert_series_equal(result2, pd.Series([2], index=expected_idx2))
281+
282+
@pytest.mark.parametrize('input_vals', [
283+
[Period('2016-01', freq='M'), Period('2016-02', freq='M')],
284+
[Period('2016-01-01', freq='D'), Period('2016-01-02', freq='D')],
285+
[Period('2016-01-01 00:00:00', freq='H'),
286+
Period('2016-01-01 01:00:00', freq='H')],
287+
[Period('2016-01-01 00:00:00', freq='M'),
288+
Period('2016-01-01 00:01:00', freq='M')],
289+
[Period('2016-01-01 00:00:00', freq='S'),
290+
Period('2016-01-01 00:00:01', freq='S')]
291+
])
292+
def test_end_time_timevalues(self, input_vals):
293+
# GH 17157
294+
# Check that the time part of the Period is adjusted by end_time
295+
# when using the dt accessor on a Series
296+
297+
s = Series(input_vals)
298+
result = s.dt.end_time
299+
expected = s.apply(lambda x: x.end_time)
300+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)