Home > Mobile >  what does first() do in pandas resample?
what does first() do in pandas resample?

Time:02-05

as the title implies:

IN[1] : 
dates = pd.date_range('10/10/2018', periods=11, freq='D')
close_prices = np.arange(len(dates))

close = pd.Series(close_prices, dates)
close

OUT[1]: 
2018-10-10     0
2018-10-11     1
2018-10-12     2
2018-10-13     3
2018-10-14     4
2018-10-15     5
2018-10-16     6
2018-10-17     7
2018-10-18     8
2018-10-19     9
2018-10-20    10

IN[2] : close.resample('W').first()

OUT[2] : 
2018-10-14    0
2018-10-21    5
Freq: W-SUN, dtype: int64

first what does resample & first do?

and why do we have this date 2018-10-21 as it was not existing in the series and based on what we have the 0 and 5?

Thanks

CodePudding user response:

resample('W') reorders and groups the dates so that they're each a full week.

first() selects each week.

CodePudding user response:

You have resampled your data by week. '2018-10-14' and '2018-10-21' are the last dates of each resampled week (each a Sunday). So by resampling, you have aggregated your data into weekly samples displayed on the Sundays on 10-14 and 10-21. 0 and 5 each refer to the count at the beginning of each respective week (in other words, the counts on 10-10 and 10-15, which would be the beginning Mondays of the resampled weeks ending on Sundays.

  •  Tags:  
  • Related