In the resample()
method, the kind=
can be used if we want to convert the index of the result to PeriodIndex
.
s = pd._testing.makeTimeSeries(100)
s.resample("10D").mean().index
# DatetimeIndex(['2000-01-03', '2000-01-13', '2000-01-23', '2000-02-02',
# '2000-02-12', '2000-02-22', '2000-03-03', '2000-03-13',
# '2000-03-23', '2000-04-02', '2000-04-12', '2000-04-22',
# '2000-05-02', '2000-05-12'],
# dtype='datetime64[ns]', freq='10D')
s.resample("10D", kind="period").mean().index
# PeriodIndex(['2000-01-03', '2000-01-13', '2000-01-23', '2000-02-02',
# '2000-02-12', '2000-02-22', '2000-03-03', '2000-03-13',
# '2000-03-23', '2000-04-02', '2000-04-12', '2000-04-22',
# '2000-05-02', '2000-05-12'],
# dtype='period[10D]')
However, the pd.Grouper()
does not implement it.
s.groupby(pd.Grouper(freq="10D")).mean().index
# DatetimeIndex(['2000-01-03', '2000-01-13', '2000-01-23', '2000-02-02',
# '2000-02-12', '2000-02-22', '2000-03-03', '2000-03-13',
# '2000-03-23', '2000-04-02', '2000-04-12', '2000-04-22',
# '2000-05-02', '2000-05-12'],
# dtype='datetime64[ns]', freq='10D')
s.groupby(pd.Grouper(freq="10D", kind="period")).mean().index
# DatetimeIndex(['2000-01-03', '2000-01-13', '2000-01-23', '2000-02-02',
# '2000-02-12', '2000-02-22', '2000-03-03', '2000-03-13',
# '2000-03-23', '2000-04-02', '2000-04-12', '2000-04-22',
# '2000-05-02', '2000-05-12'],
# dtype='datetime64[ns]', freq='10D')
""" note: TypeError is not raised """
expected:
s.groupby(pd.Grouper(freq="10D", kind="period")).mean().index
# PeriodIndex(['2000-01-03', '2000-01-13', '2000-01-23', '2000-02-02',
# '2000-02-12', '2000-02-22', '2000-03-03', '2000-03-13',
# '2000-03-23', '2000-04-02', '2000-04-12', '2000-04-22',
# '2000-05-02', '2000-05-12'],
# dtype='period[10D]')
Comment From: wany-oh
more complex example:
df = pd.DataFrame(
{"A": np.tile(np.arange(2).repeat(5), 10), "B": np.arange(100)},
index=pd._testing.makeDateIndex(100),
)
df.groupby([pd.Grouper(freq="10D"), "A"]).mean().index.dtypes[0]
# dtype('<M8[ns]')
df.groupby([pd.Grouper(freq="10D", kind="peirod"), "A"]).mean().index.dtypes[0]
# dtype('<M8[ns]')
""" work around(?) """
df.groupby([pd.Grouper(freq="10D"), "A"]).mean().rename(index=lambda x: x.to_period("10D"), level=0).index.dtypes[0]
# period[10D]
Comment From: jreback
this was on purpose
simply change the index to PI before resample
Comment From: jbrockmendel
-1 on adding keywords to pd.Grouper if there is a viable alternative
Comment From: phofl
Closing, please ping to reopen if we missed anything