在 pandas 系列中复制警告 [英] Copy Warning in Pandas Series

查看:55
本文介绍了在 pandas 系列中复制警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一列为日期时间格式的列,我想将其更改为日期格式.

I have a column which is in datetime format and I want to change it to be date format.

db['Date'] = db['Date'].apply(lambda x: x.date())

然后我得到警告:

__main__:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_index,col_indexer] = value instead

我进行了一些搜索并将其更改为:

I did some search and changed it to be:

db.loc[:,'Date'] = db.loc[:,'Date'].apply(lambda x: x.date())

错误消息是:

    if isnull(other) or (np.isscalar(other) and other == tslib.iNaT):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

当我尝试为系列分配新值时,也会发生此错误:

This error also happens when I tried to assign new values to a series:

db['new'] = db.apply(lambda x: my_func(x['A'],x['B']))

任何人都可以建议这里发生了什么以及如何摆脱警告?

Could anyone suggest what is happening here and how can I get rid of the warnings?

我注意到可以通过设置db.is_copy = False来关闭警告,但我希望将其保留在该位置,以防万一我确实犯了重要错误.因此,我仍然希望做一些事情以使代码充分运行而不会发出警告消息.

I noticed that I can turn the warning off by setting db.is_copy = False but I'd rather keep it there in case I indeed have important errors. So I still hope to do something to make the code running adequately without warning messages.

谢谢.

推荐答案

我使用

df['THING'] = df['THING'].apply(function)

一直

.正如Wes在他的注释中指出的那样在这个问题上,警告是一种启发式的.当您以这种方式进行分配时,并非总是如此,而是在视图上进行设置.进行分配,然后调用df.head()(或任何其他方式),然后查看框架是否确实发生了变化.如果有,谁在乎是否有警告?

all the time. As Wes points out in his notes on the subject, the warning is a heuristic. It's not always the case that when you're assigning in this way, you're setting on a view. Make the assignment, then call df.head() (or whatever) and see if your frame has actually changed. If it has, who cares if there's a warning?

这篇关于在 pandas 系列中复制警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆