【问题标题】:Length mismatch in pandas Dataframe resampling with specific dates熊猫数据帧中的长度不匹配与特定日期重新采样
【发布时间】:2021-08-10 21:44:30
【问题描述】:

我已经为自己的数据尝试了code。当我计算总和时它起作用。但是,如果我将索引分配给新的数据框,则会发生错误。 我注意到这是因为有时我的dfcustom_dates 之间没有数据。我仍然想将custom_dates 作为索引分配给custom_sum

对原代码的小调整:

import pandas as pd
import numpy as np
import datetime

np.random.seed(100)
df = pd.DataFrame(np.random.randint(0,100,size=(10, 1)), columns=list('A'))

df.index = pd.DatetimeIndex([datetime.date(2016,1,1),
                              datetime.date(2016,1,5),
                                 datetime.date(2016,2,1),
                             datetime.date(2016,2,2),
                              datetime.date(2016,2,5),
                                 datetime.date(2016,2,7),
                             datetime.date(2016,2,21),
                             datetime.date(2016,2,28),
                                 datetime.date(2016,2,29),
                             datetime.date(2016,3,1)
                            ])

custom_dates = pd.DatetimeIndex([datetime.date(2016,1,1),
                             datetime.date(2016,2,8),
                                 datetime.date(2016,2,10),
                             datetime.date(2016,3,1)
                            ])

custom_sum = df.groupby(custom_dates[custom_dates.searchsorted(df.index)]).sum()

还有这段代码

custom_dates.searchsorted(df.index)

给我

array([0, 1, 1, 1, 1, 1, 3, 3, 3, 3], dtype=int64)

这正是“我的 df 在 custom_dates 之间没有数据”,因为 dfdatetime.date(2016,2,8)datetime.date(2016,2,10) 之间没有数据

现在,如果我将 custom_dates 作为索引分配给 custom_sum

custom_sum.index = custom_dates

出现以下错误:

ValueError: Length mismatch: Expected axis has 3 elements, new values have 4 elements

至于我自己的数据。我的custom_dates 给了

dtype='datetime64[ns]', name='date_time', length=46899, freq=None

我的df.index 给了

dtype='datetime64[ns]', name='time_index', length=6363585, freq=None

我希望 custom_sum 中的所有实际日期为 custom_sum = df.groupby(custom_dates[custom_dates.searchsorted(df.index)]).sum() 但是,代码:

df.groupby(custom_dates[custom_dates.searchsorted(df.index)]).sum()

报错

IndexError: index 46899 is out of bounds for axis 0 with size 46899

我只能这样做

custom_dates.searchsorted(df.index)

给了

array([    0,     0,     0, ..., 46899, 46899, 46899], dtype=int64)

但没有实际日期。 所以我的问题是为什么我会在 df.groupby(custom_dates[custom_dates.searchsorted(df.index)]).sum() 中收到错误但它适用于示例? 我在这里错过了什么吗?有什么建议/cmets?谢谢!

【问题讨论】:

  • 看起来您的日期多于总和。您确定custom_dates 中的日期正确吗?
  • @MichaelDelgado 我认为我没有上传代码/错误的图像。我添加的是指向 jezrael 个人资料的链接
  • @Chaos_Is_Harmony 是的,我同意你的看法。这是因为有时我的 df 在 custom_dates 之间没有数据,导致日期多于总和。我在“custom_dates”中有正确的日期。
  • @Chaos_Is_Harmony 感谢您的帮助!!!插入索引 46899 后问题解决。

标签: python pandas


【解决方案1】:

在这种情况下,错误是说custom_sum 中只有 3 个项目,而custom_dates 列出了 4 个日期。删除错误的日期(在这种情况下为datetime.date(2016,2,10))应该可以解决维度问题。

但一般来说,要保存一个只包含满足特定条件的行的新 DataFrame,您可以使用:

new_df = custom_sum[custom_sum.index.isin(custom_dates)]

DataFrame.drop() 也有办法做到这一点。不确定哪个更有效或更理想。但我怀疑使用df.drop() 和使用inplace=True 参数可能会节省内存,因为它不会创建新的DataFrame 对象——尽管如果我在这个假设上错了,有人会纠正我。

【讨论】:

  • 感谢您的回答,请查看我对问题的新编辑。
猜你喜欢
  • 2017-06-10
  • 1970-01-01
  • 1970-01-01
  • 2018-05-06
  • 2018-11-16
  • 2019-02-28
  • 1970-01-01
  • 2018-04-28
  • 2022-01-20
相关资源
最近更新 更多