Issue
import pandas as pd
print(pd.date_range(start='1999-08-01', end='2000-07-01', freq='D'))
print(pd.period_range(start='1999-08-01', end='2000-07-01', freq='D'))
print(pd.date_range(start='1999-08', end='2000-07', freq='M'))
#This doesn't include anything from 2000-07, ie is not stop-inclusive - length 11
print(pd.period_range(start='1999-08', end='2000-07', freq='M')) # Length 12
Of these, three are stop-inclusive as one might naively define, whereas the date_range with frequency "M" is not. Why is that?
Solution
This is because 2000-07
is understood as 2000-07-01
and M
is the end of month (opposite to MS
the start of month) so 2000-07-31
is after 2000-07-01
. If you want to include "July" change the frequency from M
to MS
but it will change the day:
>>> pd.date_range(start='1999-08', end='2000-07', freq='M')[[0, -1]]
DatetimeIndex(['1999-08-31', '2000-06-30'], dtype='datetime64[ns]', freq=None)
>>> pd.date_range(start='1999-08', end='2000-07', freq='MS')[[0, -1]]
DatetimeIndex(['1999-08-01', '2000-07-01'], dtype='datetime64[ns]', freq=None)
Answered By - Corralien
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.