问题描述
我有以下数据
4/23/2021 493107
4/26/2021 485117
4/27/2021 485117
4/28/2021 485117
4/29/2021 485117
4/30/2021 485117
5/7/2021 484691
我希望它看起来像下面这样:
4/23/2021 493107
4/24/2021 485117
4/25/2021 485117
4/26/2021 485117
4/27/2021 485117
4/28/2021 485117
4/29/2021 485117
4/30/2021 485117
5/1/2021 484691
5/2/2021 484691
5/3/2021 484691
5/4/2021 484691
5/5/2021 484691
5/6/2021 484691
5/7/2021 484691
所以它使用下面的日期来填充缺失的数据。我尝试了以下代码:
df['Date']=pd.to_datetime(df['Date'].astype(str),format='%m/%d/%Y')
df.set_index(df['Date'],inplace=True)
df = df.resample('D').sum().fillna(0)
df['crude'] = df['crude'].replace({ 0:np.nan})
df['crude'].fillna(method='ffill',inplace=True)
然而,这会导致获取上述数据并获得以下结果:
4/23/2021 493107
4/24/2021 493107
4/25/2021 493107
4/26/2021 485117
4/27/2021 485117
4/28/2021 485117
4/29/2021 485117
4/30/2021 485117
5/1/2021 485117
5/2/2021 485117
5/3/2021 485117
5/4/2021 485117
5/5/2021 485117
5/6/2021 485117
5/7/2021 969382
这与我需要的输出不匹配。
解决方法
将数据帧的索引设置为Date
,然后使用asfreq
将数据帧的索引符合/重新索引为每日频率,提供填充方法作为向后填充
df.set_index('Date').asfreq('D',method='bfill')
crude
Date
2021-04-23 493107
2021-04-24 485117
2021-04-25 485117
2021-04-26 485117
2021-04-27 485117
2021-04-28 485117
2021-04-29 485117
2021-04-30 485117
2021-05-01 484691
2021-05-02 484691
2021-05-03 484691
2021-05-04 484691
2021-05-05 484691
2021-05-06 484691
2021-05-07 484691
,
import numpy as np
import matplotlib.pyplot as plt
p = np.pi
m = 4/p
n = p/2
t = np.arange(-10,10,0.001)
#a1 = m*np.cos(n*t)
#a2 = -m/3*np.cos(3*n*t)
#a3 = m/5*np.cos(5*n*t)
#a4 = -m/7*np.cos(7*n*t)
#a5 = m/9*np.cos(9*n*t)
plt.figure(figsize = (12,6))
a=[]
for i,j in zip(range(1,21,2),range(0,20,1)):
a = ((-1)**j*m/i*np.cos(i*n*t))/2
a += a
#a = a1+a2+a3+a4+a5
plt.plot(t,a,'r-')
plt.title('Square Signal using sine harmonics',fontdict={'fontname': 'monospace','fontsize': 15})
plt.ylabel('Amplitude')
plt.xlabel('Time')
plt.grid()
plt.show()
import pandas as pd
df = pd.DataFrame({
'crude': {'4/23/2021': 493107,'4/26/2021': 485117,'4/27/2021': 485117,'4/28/2021': 485117,'4/29/2021': 485117,'4/30/2021': 485117,'5/7/2021': 484691}
})
df.index = pd.to_datetime(df.index)
df = df.resample('D').sum()
df['crude'] = df['crude'].replace(0,method='bfill')
print(df)
:
df