-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Adjust time values with Period objects in Series.dt.end_time #18952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4eaa60f
4557a97
5f617df
d53bb17
75aaaeb
b5b01e7
d4cf1ba
187661e
a6716ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1235,11 +1235,9 @@ def _generate_regular_range(cls, start, end, periods, freq): | |
tz = None | ||
if isinstance(start, Timestamp): | ||
tz = start.tz | ||
start = start.to_pydatetime() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This conversion |
||
|
||
if isinstance(end, Timestamp): | ||
tz = end.tz | ||
end = end.to_pydatetime() | ||
|
||
xdr = generate_range(start=start, end=end, | ||
periods=periods, offset=freq) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ | |
from pandas.core.tools.datetimes import parse_time_string | ||
|
||
from pandas._libs.lib import infer_dtype | ||
from pandas._libs import tslib, index as libindex | ||
from pandas._libs import tslib, index as libindex, Timedelta | ||
from pandas._libs.tslibs.period import (Period, IncompatibleFrequency, | ||
DIFFERENT_FREQ_INDEX, | ||
_validate_end_alias) | ||
|
@@ -501,6 +501,16 @@ def to_timestamp(self, freq=None, how='start'): | |
""" | ||
how = _validate_end_alias(how) | ||
|
||
end = how == 'E' | ||
if end: | ||
if freq == 'B': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For some reason |
||
# roll forward to ensure we land on B date | ||
adjust = Timedelta(1, 'D') - Timedelta(1, 'ns') | ||
return self.to_timestamp(how='start') + adjust | ||
else: | ||
adjust = Timedelta(1, 'ns') | ||
return (self + 1).to_timestamp(how='start') - adjust | ||
|
||
if freq is None: | ||
base, mult = _gfc(self.freq) | ||
freq = frequencies.get_to_timestamp_base(base) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -366,6 +366,19 @@ def test_periods_number_check(self): | |
with pytest.raises(ValueError): | ||
period_range('2011-1-1', '2012-1-1', 'B') | ||
|
||
def test_start_time(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add the issue number reference |
||
# GH 17157 | ||
index = PeriodIndex(freq='M', start='2016-01-01', end='2016-05-31') | ||
expected_index = date_range('2016-01-01', end='2016-05-31', freq='MS') | ||
tm.assert_index_equal(index.start_time, expected_index) | ||
|
||
def test_end_time(self): | ||
# GH 17157 | ||
index = PeriodIndex(freq='M', start='2016-01-01', end='2016-05-31') | ||
expected_index = date_range('2016-01-01', end='2016-05-31', freq='M') | ||
expected_index = expected_index.shift(1, freq='D').shift(-1, freq='ns') | ||
tm.assert_index_equal(index.end_time, expected_index) | ||
|
||
def test_index_duplicate_periods(self): | ||
# monotonic | ||
idx = PeriodIndex([2000, 2007, 2007, 2009, 2009], freq='A-JUN') | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we have an assert on how? (e.g. it has to be S/E)? if not can you add one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is done in
_validate_end_alias
I think:pandas/pandas/_libs/tslibs/period.pyx
Lines 1843 to 1850 in ad50b1d