pandas FutureWarning:元素比较失败;而是返回标量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36620175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FutureWarning: elementwise comparison failed; returning scalar instead
提问by trench
I am receiving a warning and I want to check if this will break. I am using np.where like this in a lot of cases (it is similar, for me, to an if statement in excel). Is there a better or more pythonic or pandas way to do this? I'm trying to turn one dimension into something I can easily do mathematical operations on.
我收到警告,我想检查这是否会中断。我在很多情况下都像这样使用 np.where(对我来说,它类似于 excel 中的 if 语句)。有没有更好或更多的pythonic或pandas方法来做到这一点?我试图将一维变成我可以轻松进行数学运算的东西。
df['closed_item'] = np.where(df['result']=='Action Taken', 1, 0)
FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
result = getattr(x, name)(y)
INSTALLED VERSIONS
------------------
python: 3.5.1.final.0
python-bits: 64
OS: Windows
OS-release: 10
pandas: 0.18.0
nose: 1.3.7
pip: 8.1.0
setuptools: 20.2.2
Cython: 0.23.4
numpy: 1.11.0
scipy: 0.17.0
statsmodels: 0.6.1
xarray: None
IPython: 4.0.0
sphinx: 1.3.1
patsy: 0.4.0
dateutil: 2.4.2
pytz: 2015.7
blosc: None
bottleneck: None
tables: 3.2.2
numexpr: 2.5.1
matplotlib: 1.5.1
openpyxl: 2.2.6
xlrd: 0.9.4
xlwt: 1.0.0
xlsxwriter: 0.7.7
lxml: 3.4.4
bs4: 4.4.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.9
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.38.0
回答by Arn
The issue that you mentioned is actually quite complex, so let me divide it into parts using your words:
你说的这个问题其实很复杂,我就用你的话分成几个部分:
I am receiving a warning and I want to check if this will break
我收到警告,我想检查这是否会中断
A Warning
is a statement that is telling you to be cautious with how you handle your coding logic. A well-designed warning is not going to break your code; if it were a case, it would be an Exception
.
AWarning
是一个声明,它告诉您要谨慎处理编码逻辑。精心设计的警告不会破坏您的代码;如果是一个案例,它将是一个Exception
.
While you need to be concerned if there are problems with your output or performance, often you may ignore a warning ceteris paribus. So in your case, if everything else is OK and you do not plan to update the software, you do not need to do anything to suppress the warning. However, if you need to, you may use the following snippet:
虽然您需要担心您的输出或性能是否存在问题,但通常您可能会忽略其他条件不变的警告。因此,在您的情况下,如果其他一切正常并且您不打算更新软件,则无需执行任何操作来抑制警告。但是,如果需要,您可以使用以下代码段:
import warnings
with warnings.catch_warnings():
warnings.filterwarnings('ignore', r'elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison(.*)')
I am using np.where like this in a lot of cases (it is similar, for me, to an if statement in excel).
我在很多情况下都像这样使用 np.where(对我来说,它类似于 excel 中的 if 语句)。
Note that there is a DataFrame.where
method in pandas.
请注意,DataFrame.where
pandas中有一个方法。
Is there a better or more pythonic or pandas way to do this?
有没有更好或更多的pythonic或pandas方法来做到这一点?
Yes, there are two ways that you can use to make your code more pandas-like: If you want to get a number of columns that will work like dummies, you may use
是的,有两种方法可以使您的代码更像 Pandas:如果您想获得一些可以像傻瓜一样工作的列,您可以使用
pd.get_dummies(df.result)
It will produce a data frame with all possible dummy values it could find in a series. If this sounds to you like an overkill, do not worry there are ways to single out just one such variable.
它将生成一个数据框,其中包含它可以在一系列中找到的所有可能的虚拟值。如果这听起来像是一种矫枉过正,请不要担心有办法只挑出一个这样的变量。
In pandas boolean True
and False
are commonly used to binary classify matches within a series or a dataframe so in your case, one could perform the following operation:
在 pandas boolean 中True
,False
通常用于对系列或数据帧中的匹配项进行二进制分类,因此在您的情况下,可以执行以下操作:
df.closed_item = df.result == 'Action Taken'
I'm trying to turn one dimension into something I can easily do mathematical operations on.
我试图将一维变成我可以轻松进行数学运算的东西。
However, if you want the output to contain integer values so that it matches yours, you may use this piece of code:
但是,如果您希望输出包含整数值以使其与您的匹配,则可以使用以下代码:
df.closed_item = (df.result == 'Action Taken'`).astype(int)
As a side note, I do not think this warning propagates to newer versions, i.e. 0.13
and above (as expected since it is a future warning), so you may also considering an update.
作为旁注,我认为此警告不会传播到较新的版本,即0.13
及更高版本(正如预期的那样,因为它是未来的警告),因此您也可以考虑进行更新。
回答by Allen Sun
This warning occurs when comparing "int" and "str" in your dataset. Add .astype(int) to your comparison dataset. Try:
比较数据集中的“int”和“str”时会出现此警告。将 .astype(int) 添加到您的比较数据集。尝试:
df['closed_item'] = np.where(df['result'].astype(str)=='Action Taken', 1, 0)