简体   繁体   中英

Capturing the Timestamp values from resampled DataFrame

Using the .resample() method yields a DataFrame with a DatetimeIndex and a frequency.

Does anyone have an idea on how to iterate through the values of that DatetimeIndex?

df = pd.DataFrame(
    data=np.random.randint(0, 10, 100),
    index=pd.date_range('20220101', periods=100),
    columns=['a'],
)

df.resample('M').mean()

If you iterate, you get individual entries taking the Timestamp('2022-11-XX…', freq='M') form but I did not manage to get the date only.

g.resample('M').mean().index[0]
Timestamp('2022-01-31 00:00:00', freq='M')

I am aiming at feeding all the dates in a list for instance.

Thanks for your help !

You an convert each entry in the index into a Datetime object using .date and to a list using .tolist() as below

>>> df.resample('M').mean().index.date.tolist()
[datetime.date(2022, 1, 31), datetime.date(2022, 2, 28), datetime.date(2022, 3, 31), datetime.date(2022, 4, 30)]

You can also truncate the timestamp as follows ( reference solution )

>>> df.resample('M').mean().index.values.astype('<M8[D]')
array(['2022-01-31', '2022-02-28', '2022-03-31', '2022-04-30'],
      dtype='datetime64[D]')

This solution seems to work fine both for dates and periods:
I = [k.strftime('%Y-%m') for k in g.resample('M').groups]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM