Pandas resample to the first day in my data

I have a Yahoo daily stock price imported into the pandas framework. I want to use .resample()

to convert it to the monthly share price by taking the price of the first daily QUOTED price of each month.

.resample('MS', how='first')

      

returns the correct price of each month , but changes the index on the first day of the month, while in general the first day of the month for the quoted price could be 2 or 3 months because of holidays and weekends.

How can I resample()

only use to resample existing dates and not change them?

+6


source to share


1 answer


I think you want BMS (work month start):

.resample('BMS').first()

      

Note: before version 0.18 this was done using the deprecated how

kwarg:

.resample('BMS', how='first')

      




Alternatively, one could select a group month and take the first one with a plain old group month (and for example use nth to get the first entry in each group):

.groupby(pd.Grouper(freq='M')).nth(0)

      

Note: prior to 0.21, this was done using the deprecated TimeGrouper

:

.groupby(pd.TimeGrouper('M')).nth(0)

      

+8


source







All Articles