【问题标题】:Python: upsampling dataframe from daily to hourly data using ffill()Python:使用 ffill() 将数据帧从每日数据上采样到每小时数据
【发布时间】:2019-05-29 08:51:21
【问题描述】:

我正在尝试将我的数据从每日频率上采样到每小时频率并转发填充缺失的数据。

我从以下代码开始:

df1 = pd.read_csv("DATA.csv")   
df1.head(5)

然后我使用以下内容转换为日期时间字符串并将日期/时间设置为索引:

df1['DT'] = pd.to_datetime(df1['DT']).dt.strftime('%Y-%m-%d %H:%M:%S')
df1.set_index('DT')

我尝试按如下方式每小时重新采样:

df1['DT'] = df1.resample('H').ffill()

但我收到以下错误:

TypeError:仅适用于 DatetimeIndex、TimedeltaIndex 或 PeriodIndex,但得到了一个'RangeIndex'的实例

我认为我的 dtype 已经是上面 pd.to_datetime 代码所指示的日期时间。我尝试的任何方法似乎都不起作用。谁能帮帮我?

我的预期输出如下:

DT                  VALUE
2016-08-01 00:00:00 0.000000
2016-08-01 01:00:00 0.000000
2016-08-01 02:00:00 0.000000

等等

文件本身大约有 1000 行。前 50 行左右为零,以便澄清实际数据的位置:

DT                  VALUE
2018-12-13 00:00:00 24000.000000
2018-12-13 01:00:00 24000.000000
2018-12-13 02:00:00 24000.000000
...
2018-12-13 23:00:00 24000.000000
2018-12-14 00:00:00 26000.000000
2018-12-14 01:00:00 26000.000000

等等

【问题讨论】:

  • 提及您的预期输出。
  • @AbdurRehman 感谢您的评论,我已经更新了原版。
  • 在数据上提及 column_names 以使其更清晰。您的实际数据的年份为2018,而您的预期数据的年份为2016。输入正确还是错误?
  • 感谢 Abdur,我已经更新了列名。大约有 3 年的数据,前面大部分都是零。我已经在问题中澄清了。

标签: python pandas


【解决方案1】:

尝试重新分配它

df1=df1.set_index('DT')

或者

df1.set_index('DT',inplace=True)

【讨论】:

  • 您好 W-B,感谢您的回答。我试过了,但不幸的是我仍然收到同样的错误。
  • df1.VALUE.resample('H').ffill() 这有错误@GJB
  • 我现在遇到问题 AttributeError: 'DataFrame' object has no attribute 'VALUE'
  • 我错了,我打错了:TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'
【解决方案2】:

我假设您提到的数据集的一些初始行,

          DT    VALUE
0   2016-08-01  0
1   2016-08-02  0
2   2016-08-03  0
3   2016-08-04  0
4   2016-08-05  0
5   2016-08-06  0
6   2016-08-07  0
7   2016-08-08  0
8   2016-08-09  0

然后,像这样在DT 上建立索引,

df = df.set_index('DT')
df

输出:

           VALUE
   DT   
2016-08-01  0
2016-08-02  0
2016-08-03  0
2016-08-04  0
2016-08-05  0
2016-08-06  0
2016-08-07  0
2016-08-08  0
2016-08-09  0

现在,重新采样您的数据框,

df = df.resample('H').ffill()
df

输出:显示输出的一些初始值,

                VALUE
    DT  
2016-08-01 00:00:00 0
2016-08-01 01:00:00 0
2016-08-01 02:00:00 0
2016-08-01 03:00:00 0
2016-08-01 04:00:00 0
2016-08-01 05:00:00 0
2016-08-01 06:00:00 0
2016-08-01 07:00:00 0
2016-08-01 08:00:00 0
2016-08-01 09:00:00 0
2016-08-01 10:00:00 0

【讨论】:

  • @GJB 让我知道这对您有用,或者您有任何其他疑问。
  • 嗨 Abdur,我收到以下错误:索引必须单调递增或递减
  • 我发现了错误——最后的数据中有一个错误的 NaN。非常感谢 Abdur。
  • 时区或夏令时的日子呢?
【解决方案3】:

您可以将索引转换为pd.DatetimeIndex,然后对其重新采样。我也不认为你需要(或想要)strftime() 电话:

df1 = pd.read_csv("DATA.csv")
df1['DT'] = pd.to_datetime(df1['DT'])
df1.set_index('DT')
df1.index = pd.DatetimeIndex(df1.index)
df1['DT'] = df1.resample('H').ffill()

注意:你可以结合一堆这样的,它仍然很清楚,比如:

df1 = pd.read_csv("DATA.csv")
df1.index = pd.DatetimeIndex(pd.to_datetime(df1['DT']))
df1['DT'] = df1.resample('H').ffill()

【讨论】:

    猜你喜欢
    • 2018-08-26
    • 1970-01-01
    • 2020-05-25
    • 2023-03-19
    • 1970-01-01
    • 2022-11-11
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多