在Pandas数据框的列中填充缺失的日期值

13
我正在使用Pandas通过数据框存储股票价格数据。该数据集中有2940行数据,如下所示:

enter image description here

时间序列数据不包含星期六和星期日的值。因此需要填补缺失值。以下是我编写的代码,但它并没有解决问题:
import pandas as pd
import numpy as np
import os
os.chdir('C:/Users/Admin/Analytics/stock-prices')

data  = pd.read_csv('stock-data.csv')

# PriceDate Column - Does not contain Saturday and Sunday stock entries
data['PriceDate'] =  pd.to_datetime(data['PriceDate'], format='%m/%d/%Y')
data = data.sort_index(by=['PriceDate'], ascending=[True])


# Starting date is Aug 25 2004
idx = pd.date_range('08-25-2004',periods=2940,freq='D')


data = data.set_index(idx)
data['newdate']=data.index
newdate=data['newdate'].values   # Create a time series column   


data = pd.merge(newdate, data, on='PriceDate', how='outer')

如何填充周六和周日的缺失值?

1个回答

28

我认为您可以使用resampleffillbfill,但在此之前需要从列PriceDate进行set_index

print (data)
   ID  PriceDate  OpenPrice  HighPrice
0   1  6/24/2016          1          2
1   2  6/23/2016          3          4
2   2  6/22/2016          5          6
3   2  6/21/2016          7          8
4   2  6/20/2016          9         10
5   2  6/17/2016         11         12
6   2  6/16/2016         13         14
data['PriceDate'] =  pd.to_datetime(data['PriceDate'], format='%m/%d/%Y')
data = data.sort_values(by=['PriceDate'], ascending=[True])
data.set_index('PriceDate', inplace=True)
print (data)
            ID  OpenPrice  HighPrice
PriceDate                           
2016-06-16   2         13         14
2016-06-17   2         11         12
2016-06-20   2          9         10
2016-06-21   2          7          8
2016-06-22   2          5          6
2016-06-23   2          3          4
2016-06-24   1          1          2

data = data.resample('D').ffill().reset_index()
print (data)
   PriceDate  ID  OpenPrice  HighPrice
0 2016-06-16   2         13         14
1 2016-06-17   2         11         12
2 2016-06-18   2         11         12
3 2016-06-19   2         11         12
4 2016-06-20   2          9         10
5 2016-06-21   2          7          8
6 2016-06-22   2          5          6
7 2016-06-23   2          3          4
8 2016-06-24   1          1          2

data = data.resample('D').bfill().reset_index()
print (data)
   PriceDate  ID  OpenPrice  HighPrice
0 2016-06-16   2         13         14
1 2016-06-17   2         11         12
2 2016-06-18   2          9         10
3 2016-06-19   2          9         10
4 2016-06-20   2          9         10
5 2016-06-21   2          7          8
6 2016-06-22   2          5          6
7 2016-06-23   2          3          4
8 2016-06-24   1          1          2

1
使用bfill().reset_index()时,会显示以下TypeError:仅适用于DatetimeIndex、TimedeltaIndex或PeriodIndex,但得到了“RangeIndex”的实例。 - User456898
2
你需要从列“PriceDate”设置索引 - data.set_index('PriceDate', inplace=True) - jezrael
2
我不确定是否理解正确 - 你需要设置新列 - data['new'] = data['PriceDate'] 吗? - jezrael
不,我已经找到了解决方案。 想要获取data['PriceDate']对应的星期几名称,且不重复。 这与此特定问题无关。在这里找到了打印不重复星期几名称的解决方案: https://dev59.com/bV0a5IYBdhLWcg3whJHW - User456898
@jezrael,你有没有想过如何在数据包含数百个不同ID时实现这个功能?例如,我有300栋建筑的时间序列数据,需要为每个建筑物单独填充时间间隙。我已经手写了一个函数,在小数据集上似乎可以工作,但速度极慢。 - Renel Chesak
@RenelChesak - 你需要使用 data = data.groupby('ID').resample('D').ffill().reset_index(level=0, drop=True).reset_index() - jezrael

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接