I have a dataframe that has a datetime index and a single column containing the price of the asset. For example,
date asset_value 2023-05-30 136.57000000 2023-05-31 133.30000000 2023-06-01 134.83000000 2023-06-02 134.63000000 2023-06-05 133.73000000
I have several years of data. I’d like to resample the dataframe for different periods and compute the min() and max() values for that period. For example,
“
df.resample(“W”).agg({‘asset_value’: [“min”, “max”]})
date min max
2023-06-04 133.30000000 136.57000000
2023-06-11 133.73000000 136.20000000
2023-06-18 138.93000000 141.79000000
2023-06-25 136.07000000 138.92000000
2023-07-02 139.80000000 146.55000000
“
This returns the min, max and right edge date for the bin. However, I would also like to know the date the min and max occurred.
Any advice how to do this? Thanks.
I did try adding additional columns to the original dataframe hoping that information would display in the resampled dataframe, but it didn’t. See below.
“
#Make sure the index is a datetime index
df.index = pd.to_datetime(df.index)
df.reset_index(inplace=True)
#Add the day of the week, day, month and year as columns
df['day_of_week'] = df['date'].dt.dayofweek
df['day_of_month'] = df['date'].dt.day
df['month'] = df['date'].dt.month
df['year'] = df['date'].dt.year
#Set the index back to my datetime index and drop the date column.
df.set_index(df['date'], inplace=True)
df.drop(['date'], axis=1, inplace=True)
“
Jay Murphy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.