【问题标题】:Fill the missing date values in a Pandas Dataframe column在 Pandas 数据框列中填充缺失的日期值
【发布时间】:2016-11-16 14:56:30
【问题描述】:

我正在使用 Pandas 使用 Data Frames 存储股票价格数据。数据集中有 2940 行。数据集快照如下所示:

时间序列数据不包含周六和周日的值。因此,必须填充缺失值。
这是我写的代码,但没有解决问题:

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')

如何填补周六周日的缺失值?

【问题讨论】:

    标签: python numpy pandas time-series


    【解决方案1】:

    我认为您可以将resampleffillbfill 一起使用,但在set_index 之前来自PriceDate 列:

    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
    

    【讨论】:

    • 使用 bfill( ).reset_index( ) 时,会显示以下 TypeError:仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但获得了 'RangeIndex' 的实例
    • 您需要从列PriceDate - data.set_index('PriceDate', inplace=True) 设置索引。
    • 我不确定是否理解正确 - 你需要设置新列 - data['new'] = data['PriceDate'] 吗?
    • 不,我实际上得到了解决方案。想要 data['PriceDate'] 的相应日期名称而不重复。这与这个特定问题无关。在这里找到了打印日期名称而不重复的解决方案:stackoverflow.com/questions/30222533/…
    • @jezrael 当您的数据包含数百个不同的 ID 时,您有什么想法可以实现这一点吗?例如,我有 300 座建筑物的时间序列数据,需要为每个建筑物填写时间间隔作为单独的时间序列。我手工编写了一个函数,它似乎适用于小型数据集,但速度极慢。
    猜你喜欢
    • 2021-06-17
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多