As example, I have the following code which creates a dataframe with an index containing a single value - the date '2018-03-06' (a Tuesday). Note that this date falls in the week of 2018-03-05 (a Monday):
values = [1, 1, 1]
dates = pd.to_datetime(np.repeat('2018-03-06', 3))
df = pd.DataFrame({
'value': values
}, index=dates)
df.resample('W-MON').size()
which produces:
2018-03-12 3
Freq: W-MON, dtype: int64
Why does pandas roll the date forward one week? I would have expected the result to have been resampled to 2018-03-05 since that is the week during which the values were generated and I'm using freq='W-MON'
.
UPDATE
As was pointed out, I needed to add the label
argument to resample
which defines which bin edge to use. Using label='left'
solves the problem of bucketing the dates in the correct week except when the date falls on the start of the week (in this case, Monday). For example, if I apply resample to the date 2018-03-05 using label='left'
then the resampled value is 2018-02-26 when it should be 2018-03-05.
Pandas has a built-in function called to_datetime()that converts date and time in string format to a DateTime object. As you can see, the 'date' column in the DataFrame is currently of a string-type object. Thus, to_datetime() converts the column to a series of the appropriate datetime64 dtype.
Resampling generates a unique sampling distribution on the basis of the actual data. We can apply various frequency to resample our time series data. This is a very important technique in the field of analytics. There are many other types of time series frequency available.
Resample Hourly Data to Daily Dataresample() method. To aggregate or temporal resample the data for a time period, you can take all of the values for each day and summarize them. In this case, you want total daily rainfall, so you will use the resample() method together with . sum() .
Let's try using label
and closed
see docs:
values = [1, 1, 1]
dates = pd.to_datetime(np.repeat('2018-03-06', 3))
df = pd.DataFrame({
'value': values
}, index=dates)
df.resample('W-MON', label='left',closed='left').size()
Output:
2018-03-05 3
Freq: W-MON, dtype: int64
And,
values = [1, 1, 1]
dates = pd.to_datetime(np.repeat('2018-03-05', 3))
df = pd.DataFrame({
'value': values
}, index=dates)
df.resample('W-MON', label='left',closed='left').size()
Output:
2018-03-05 3
Freq: W-MON, dtype: int64
Interesting note about the docs, the signature states that 'closed' defaults to None. However, the docstring states that 'closed' default 'left'.
I'm not sure why it's done this way and I agree that the behaviour you expected seems more intuitive. You can get your desired result by passing label='left'
as a keyword parameter. The default value in this case was 'right'
.
df.resample('W-MON', label='left').size()
From the documentation:
label : {‘right’, ‘left’}
Which bin edge label to label bucket with. The default is ‘left’ for all frequency offsets except for ‘M’, ‘A’, ‘Q’, ‘BM’, ‘BA’, ‘BQ’, and ‘W’ which all have a default of ‘right’.
I guess 'W-MON'
still counts as 'W'
which is why the default is 'right'
and therefore your example gave a result of '2018-03-12'
rather than '2018-03-05'
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With