You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
importpandasaspdfrompandas._libs.tslibsimportto_offset# 1# Freq argument is ignored when using different multiplehourly=to_offset("H")
p=pd.Period("2020-01-01", freq="24H")
assertpd.Period(p, hourly).freq==to_offset("24H")
# 2# asfreq shifts value, even when using same frequencyp=pd.Period("2020-01-01", freq="24H")
assertp!=p.asfreq(p.freq)
# also, consider this exampledr=pd.date_range("2020", freq="2d", periods=3)
s1=dr.to_series().asfreq(dr.freq).to_period()
s2=dr.to_series().to_period().asfreq(dr.freq)
# one would expect s1 and s2 to be the same, but of course not!>>>s12020-01-012020-01-012020-01-032020-01-032020-01-052020-01-05Freq: 2D, dtype: datetime64[ns]
>>>s22020-01-022020-01-012020-01-042020-01-032020-01-062020-01-05Freq: 2D, dtype: datetime64[ns]
# 3# When providing two periods in period_range, only start of end is taken into considerationpr=pd.period_range(pd.Period("2020-01-01 00:00", "6H"), pd.Period("2020-01-01 18:00", "6H"), freq="H")
pr[0] =="2020-01-01 0:00"pr[-1] =="2020-01-01 18:00"# why not 23:00?len(pr) ==19# which of course is inconsistent withpr=pd.period_range(pd.Period("2020Q1", "Q"), pd.Period("2020Q2", "Q"), freq="M")
pr[0] =="2020-03"# why not 2020-01?pr[-1] =="2020-06"# which then again behaves differently fromdr=pd.date_range(pd.Timestamp("2020Q1", "Q"), pd.Timestamp("2020Q2", "Q"), freq="M")
dr[0] =="2020-01-31"dr[-1] == "'2020-03-31'
Issue Description
The behaviour of Period and period_range is just very surprising and inconsistent.
Is is inconsistent in itself but also when comparing period_range with date_range.
Uh oh!
There was an error while loading. Please reload this page.
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
The behaviour of
Period
andperiod_range
is just very surprising and inconsistent.Is is inconsistent in itself but also when comparing
period_range
withdate_range
.See also: #47465
Expected Behavior
I naively would expect that a
Period
represents a time-range. There is a start where the period begins and an end where it ends:Here
p
represents everything on the first two days of 2020.If I use
period_range
, I would expect it to take the entire range of start and end into account:So far so good. Let's try a different frequency:
How naive of me! Of course the second argument is neither inclusive nor exclusive when generating the range, but a happy mix of both:
The new range includes everything from
start.start_time
untilpd.Period(end.start_time, "D").end_time
just one would expect.The rules are clear now.
So let's just try a different example.
We know: The start of
pr
should bestart.start_time
and end should bepd.Period(end.start_time, "D").end_time
:🤯
The text was updated successfully, but these errors were encountered: