通过python中的一系列日期迭代,日期缺失
在这里,我得到了熊猫数据框架,每天的股票回报是日期和回报率。 但是,如果我只想保留每周的最后一天,并且数据有一些丢失的日子,我该怎么办?
import pandas as pd
df = pd.read_csv('Daily_return.csv')
df.Date = pd.to_datetime(db.Date)
count = 300
for last_day in ('2017-01-01' + 7n for n in range(count)):
实际上,我的大脑在这一点上停止工作,因为我的想象力有限……也许最大的一点是“+7n”的东西毫无意义。
Here I got a pandas data frame with daily return of stocks and columns are date and return rate.
But if I only want to keep the last day of each week, and the data has some missing days, what can I do?
import pandas as pd
df = pd.read_csv('Daily_return.csv')
df.Date = pd.to_datetime(db.Date)
count = 300
for last_day in ('2017-01-01' + 7n for n in range(count)):
Actually my brain stop working at this point with my limited imagination......Maybe one of the biggest point is "+7n" kind of stuff is meaningless with some missing dates.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将创建一个带有40个日期和40个样本返回的示例数据集,然后随机对90%进行采样以模拟丢失的日期。
此处的关键是,如果尚未,则需要将
date
列转换为DateTime,并确保您的DF按日期进行排序。然后,您可以年/每周分组并占据最后一个价值。如果您反复运行此操作,您会发现所选日期可能会更改,如果降低的值是一周的最后一天。
基于该
输出
I'll create a sample dataset with 40 dates and 40 sample returns, then sample 90 percent of that randomly to simulate the missing dates.
The key here is that you need to convert your
date
column into datetime if it isn't already, and make sure your df is sorted by the date.Then you can groupby year/week and take the last value. If you run this repeatedly you'll see that the selected dates can change if the value dropped was the last day of the week.
Based on that
Output