重样索引问题

发布于 2025-02-06 12:50:19 字数 1699 浏览 0 评论 0原文

重新采样数据框时,我会在索引col上获得密钥错误。

这是我的代码:

  if setting == 'resample':
    Freq_r = "60Min" #Min
    df1 = df1.set_index('date')
    df1 = df1.resample(Freq_r).agg({'open': 'first', 'high': 'max','low': 'min', 'close': 'last'})
    df2 = df2.set_index('date')
    df2 = df2.resample(Freq_r).agg({'open': 'first', 'high': 'max','low': 'min', 'close': 'last'})

没有重新采样数据就是这样:

    date    open_x  high_x  low_x   close_x v1_x    v2_x    conid_x open_y  high_y  low_y   close_y v1_y    v2_y    conid_y
0   1988-10-20 10:30:00 13.39   13.39   13.39   13390.0 1   0   QOA1988Z    14.61   14.63   14.61   14620.0 4   0   CLA1988Z
1   1988-10-31 10:30:00 12.80   12.80   12.80   12800.0 1   0   QOA1988Z    13.83   13.84   13.83   13840.0 2   0   CLA1988Z
2   1988-11-07 10:30:00 13.27   13.27   13.27   13270.0 1   0   QOA1989F    13.97   13.97   13.97   13970.0 1   0   CLA1988Z
3   1988-11-15 10:30:00 13.20   13.20   13.20   13200.0 1   0   QOA1989F    14.02   14.02   14.02   14020.0 1   0   CLA19

reset_index,Inplace = true等无济于事。如何解决这个问题?谢谢。

错误信息: KeyError Trackback(最近的最新电话) /USR/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py. 3360尝试: - > 3361返回self._engine.get_loc(casted_key) 3362除了KeyError为ERR:

4帧 /hashtable_class_helper.pxi in pandas._libs.hashtable.pyobjecthashtable.get_item

pandas/ _libs

)以下例外:

KeyError Trackback(最近的最新电话) /USR/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py. 3361返回self._engine.get_loc(casted_key) 3362除了keyError为err: - > 3363从ERR提出KeyError(key) 3364 3365如果IS_SCALAR(键)和ISNA(键)而不是self.hasnans:

keyError:'date'

When I resample dataframe I get key error on the index col.

Here is my code:

  if setting == 'resample':
    Freq_r = "60Min" #Min
    df1 = df1.set_index('date')
    df1 = df1.resample(Freq_r).agg({'open': 'first', 'high': 'max','low': 'min', 'close': 'last'})
    df2 = df2.set_index('date')
    df2 = df2.resample(Freq_r).agg({'open': 'first', 'high': 'max','low': 'min', 'close': 'last'})

Without resampling data is like this:

    date    open_x  high_x  low_x   close_x v1_x    v2_x    conid_x open_y  high_y  low_y   close_y v1_y    v2_y    conid_y
0   1988-10-20 10:30:00 13.39   13.39   13.39   13390.0 1   0   QOA1988Z    14.61   14.63   14.61   14620.0 4   0   CLA1988Z
1   1988-10-31 10:30:00 12.80   12.80   12.80   12800.0 1   0   QOA1988Z    13.83   13.84   13.83   13840.0 2   0   CLA1988Z
2   1988-11-07 10:30:00 13.27   13.27   13.27   13270.0 1   0   QOA1989F    13.97   13.97   13.97   13970.0 1   0   CLA1988Z
3   1988-11-15 10:30:00 13.20   13.20   13.20   13200.0 1   0   QOA1989F    14.02   14.02   14.02   14020.0 1   0   CLA19

reset_index, inplace=True etc didn't help. How to fix this? Thank you.

Error message:
KeyError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3360 try:
-> 3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:

4 frames
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'date'

The above exception was the direct cause of the following exception:

KeyError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
-> 3363 raise KeyError(key) from err
3364
3365 if is_scalar(key) and isna(key) and not self.hasnans:

KeyError: 'date'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘逸的'云 2025-02-13 12:50:19

df = dfx.Reset_index(level ='date')

似乎已经解决了它。我将原始DF分配给了称为DFX的温度DF,并重置索引指定日期。

我不了解问题或解决方案,任何解释都会有所帮助。谢谢。

df= dfx.reset_index(level='date')

seems to have resolved it. I assigned the original df into a temp df called dfx and reset index specifying date.

I don't understand the issue or the resolution though, any explanation will help. Thank you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文