[英]How to forward fill for dates in python?
Currently, I have a dataset that has data pulled on a weekly basis like such目前,我有一个数据集,每周都会提取这样的数据
Date Value
2020-06-29 5.63
2020-07-06 6.01
2020-07-13 5.83
I am looking to fill in the gaps between the data by forward filling the values like such我希望通过向前填充这样的值来填补数据之间的空白
Date Value
2020-06-29 5.63
2020-06-30 5.63
2020-07-01 5.63
2020-07-02 5.63
2020-07-03 5.63
2020-07-04 5.63
2020-07-05 5.63
2020-07-06 6.01
2020-07-07 6.01
2020-07-08 6.01
and so on等等
Is there a way to automate this procedure using python?有没有办法使用 python 自动执行此过程?
Thanks谢谢
I created the dataframe just to replicate the data, but try this我创建了 dataframe 只是为了复制数据,但试试这个
# Import libraries
import pandas as pd
import numpy as np
from datetime import datetime
# Create DataFrame
df = pd.DataFrame({'Date':['2020-06-29','2020-07-06','2020-07-13'],
'Value': [5.63, 6.01, 5.83]})
# Convert to datetime datatype
df['Date'] = pd.to_datetime(df['Date'])
# Store minimum date and max date in variables
s_range = df['Date'].min()
e_range = df['Date'].max()
# Generate date ranges between oldest and newest date
dates = pd.DataFrame(list(pd.date_range(s_range, e_range)),columns=['Date'])
# Merge dates DataFrame with original DataFrame
new_df = pd.merge(df,dates,on='Date',how='right').sort_values(['Date'])
# Reset index
new_df.reset_index(drop=True, inplace=True)
# Forward fill NaN with missing data
new_df.ffill(inplace=True)
Date Value
0 2020-06-29 5.63
1 2020-06-30 5.63
2 2020-07-01 5.63
3 2020-07-02 5.63
4 2020-07-03 5.63
5 2020-07-04 5.63
6 2020-07-05 5.63
7 2020-07-06 6.01
8 2020-07-07 6.01
9 2020-07-08 6.01
10 2020-07-09 6.01
11 2020-07-10 6.01
12 2020-07-11 6.01
13 2020-07-12 6.01
14 2020-07-13 5.83
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.