简体   繁体   English

如何使用Pandas的date_range从月初开始获取每周数据?

[英]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. 我想按周划分从7月1日到8月1日的日期范围。 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' ) 我正在使用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 . 我希望从2015-07-01此操作。 I know I can use timedelta or find the start day of the month and use W-WED . 我知道我可以使用timedelta或查找月份的开始日期并使用W-WED But is there any other shortcut to do the same using date_range of pandas ? 但是,有没有其他的快捷方式做同样的使用date_rangepandas

I have checked http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases , but could not come up with anything useful. 我已经检查了http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases ,但无法提出任何有用的建议。

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 我建议使用7天而不是一周的频率,这样您就可以从每月的第一天开始,而不是一周的第一天

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 但在您的示例中,这两个相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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