为什么会报错 FutureWarning: Dropping invalid columns in DataFrameGroupBy.add is deprecated. In…
Private research annotation
Sign in to add a private research label or note.
- User
为什么会报错 FutureWarning: Dropping invalid columns in DataFrameGroupBy.add is deprecated. In a future version, a TypeError will be raised. Before calling .add, select only columns which should be valid for the function. shopid_pay_count = user_pay[['shop_id', 'time_stamp']].groupby('shop_id', as_index=False).sum().rename(columns={'time_stamp':'payment_count'})
- Assistant
这个警告是因为在DataFrameGroupBy.add方法中,有些无效的列被删除了。在将来的版本中,可能会引发TypeError错误。因此,在调用.add方法之前,建议选择只需要进行计算的有效列。
在你的代码中,建议在使用groupby方法前,将user_pay数据框仅选择需要的列(即'shop_id'和'time_stamp'),可以使用以下代码来实现:
pythonshopid_pay_count = user_pay[['shop_id', 'time_stamp']].groupby('shop_id', as_index=False)['time_stamp'].sum().rename(columns={'time_stamp':'payment_count'})这样,就只选择了'shop_id'和'time_stamp'这两列进行groupby操作,并且在.sum()方法后面,只返回了'time_stamp'这一列,从而避免了警告的出现。
- User
还是有同样的警告
- Assistant
如果你已经尝试了上述代码,但仍然看到同样的警告,那么可能是因为该警告在使用pandas 1.2.x版本时已知存在,但是在使用较新版本的pandas时可能已经被修复了。
如果你使用的是较新版本的pandas,你可以尝试将代码修改为:
pythonshopid_pay_count = user_pay[['shop_id', 'time_stamp']].groupby('shop_id', as_index=False).agg(payment_count=('time_stamp', 'sum'))这将使用agg方法代替.sum()方法,并使用元组指定聚合函数的新列名。这个修改可以消除该警告。
- User
上述方法报以下错误 TypeError: datetime64 type does not support add operations
- Assistant
这个错误通常出现在使用datetime64类型数据时进行加法操作,而加法操作不是datetime64类型的基本运算。
根据你提供的代码,可能是在对
time_stamp列进行sum(求和)操作时发生了这个问题。如果是这个原因导致的,可以尝试将time_stamp列转换为timedelta64类型,从而使其支持加法操作,如下所示:pythonshopid_pay_count = user_pay[['shop_id', 'time_stamp']].groupby('shop_id', as_index=False)['time_stamp'].sum().astype('timedelta64[ns]').rename(columns={'time_stamp':'payment_count'})这样就会将
time_stamp列的类型从datetime64转换为timedelta64,使其支持加法操作。如果仍然存在问题,请检查
time_stamp列中是否有无效值,比如缺失值或非法值,这些值可能会导致该错误的出现。如果存在这些无效值,可以尝试通过填充或删除这些值来解决问题。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.