简体   繁体   中英

How to get weekly data from beginning of month using date_range of Pandas?

I want to divide the date range starting from 1st July to 1st August, in weekly basis. But I want it to start from 1st day of the month.

I am using pd.date_range('2015-07-01', '2015-08-01', freq='W' )

But I am getting

DatetimeIndex(['2015-07-05', '2015-07-12', '2015-07-19', '2015-07-26'], dtype='datetime64[ns]', freq='W-SUN')

I want this to be done from 2015-07-01 . I know I can use timedelta or find the start day of the month and use W-WED . But is there any other shortcut to do the same using date_range of pandas ?

I have checked http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases , but could not come up with anything useful.

Any help is appreciated. Thanks in advance.

I would suggest using the frequency of 7 days instead of a week, so that you will start at the first day of the month rather than the first day of the week

pd.date_range('2015-07-01', '2015-08-01', freq='7d')

EDIT

To clarify, it is not strictly the first day of the month, but the first day you provide. But in your example those two are the same

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM