numpy: FutureWarning: elementwise comparison failed
Look at the arrays after you create them:
In [58]: a = np.array(['aug', False, False, False])
...:
In [59]: a
Out[59]: array(['aug', 'False', 'False', 'False'], dtype='<U5')
In [60]: a == 'aug'
Out[60]: array([ True, False, False, False])
In [61]: a == False
<ipython-input-61-f9ff25cfe387>:1: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
a == False
Out[61]: False
In [62]: a == 'False'
Out[62]: array([False, True, True, True])
It's a string dtype array. Testing for matching strings works. Testing for a nonstring value is wrong.
Same for nan
:
In [64]: a = np.array(['aug', np.nan, np.nan, np.nan])
In [65]: a
Out[65]: array(['aug', 'nan', 'nan', 'nan'], dtype='<U3')
In [66]: a == 'nan'
Out[66]: array([False, True, True, True])
If you must mix types - string, boolean, float, you can specify object
dtype. That makes the array more list-like.
In [67]: a = np.array(['aug', False, False, False], object)
In [68]: a
Out[68]: array(['aug', False, False, False], dtype=object)
In [69]: a == 'aug'
Out[69]: array([ True, False, False, False])
In [70]: a == False
Out[70]: array([False, True, True, True])
In [71]: a == True
Out[71]: array([False, False, False, False])
But it doesn't help with np.nan
. nan
is a special kind of float that isn't equal to anything, not even itself.
In [72]: a = np.array(['aug', np.nan, np.nan, np.nan], object)
In [73]: a
Out[73]: array(['aug', nan, nan, nan], dtype=object)
In [74]: a=='aug'
Out[74]: array([ True, False, False, False])
In [75]: a == np.nan
Out[75]: array([False, False, False, False])
isnan
is the correct way to test for nan
, but it only works with float dtype arrays:
In [76]: np.isnan(a)
Traceback (most recent call last):
File "<ipython-input-76-da86cb21b8a4>", line 1, in <module>
np.isnan(a)
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
not using numpy: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
Pandas is built upon numpy. A lot of operations with pandas dataframes are very efficient, because it uses numpy, which uses C code to use the CPU most effectively.
So, even if you are not importing numpy and explicitly using it, pandas is still using it behind the screens. This is important to know. Once you understand numpy and how pandas uses it, you will understand why some ways of doing the same thing are a lot faster than others.
To select only those rows for which a particular condition holds, you might want to use the .loc[ ]
syntax.
For you that would be: df.loc[df.Month != '0']
.
As for what things are currently supported or not: that depends on what version of Python, Numpy and Pandas you are using. I ran your code and got no warning (Python 3.7.6, Numpy 1.18.1, Pandas 1.0.1; and Python 3.6.9, Numpy 1.13.3, Pandas 1.0.1).
Warnings are not errors. Your code will still run, but when you get a warning, you should check whether the function you used does indeed do what you want it to do / expect it to do. Carefully check the documentation.
FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
The problem in your code is that:
- the result is computed for each row separately (so that the
result is a boolean Series), - but you actually need a boolean scalar (either a single True
or a single False).
To convert such a boolean Series to a single value, use either any()
or all(), according to what is needed.
Another detail: Market_value, as I suppose, is a float column and
NaN is only the way the not-a-number value is printed.
Actually, it is a special value of float type.
So comparing with 'NaN' as a string is wrong.
Note also that NaN is not equal to any other value, including other NaN.
To check whether any value is / is not NaN, use isna() or notna()
functions.
So, to sum up, run instead:
if(data['Market_value'].notna().all()):
print(data.head())
Pandas merge failing with FutureWarning: elementwise comparison failed
I was able to figure out a solution but not the cause. It turned out that my query params (i.e., user_id) were changing from int to str, when I changed how the query. I haven't been able to observe any changes to the output dataframes, but casting the str to int fixed the problem.
Related Topics
Maximum Value for Long Integer
Not Letting the Character Move Out of the Window
How to Pass a Method as a Parameter in Python
Does Python Urllib2 Automatically Uncompress Gzip Data Fetched from Webpage
Why Does Python Return 0 for Simple Division Calculation
How to Index into a Dictionary
Tkinter: Binding Mousewheel to Scrollbar
Using Logging in Multiple Modules
Pandas - Slice Large Dataframe into Chunks
Object of Custom Type as Dictionary Key
Run a Program from Python, and Have It Continue to Run After the Script Is Killed
Understanding Matplotlib.Subplots Python
Python Unicodedecodeerror - am I Misunderstanding Encode
Add a String Prefix to Each Value in a String Column Using Pandas