为什么会报错 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()方法,并使用元组指定聚合函数的新列名。这个修改可以消除该警告。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.