-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
TypeError on argmax of object dtype (change from 0.20.3) #18021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Can you give a reproducible example? |
I meet the same problem.
|
Can you provide a copy-pastable example @barondu? |
sure, |
Do you have a minimal test-case, something that could go in a unit test? |
|
Here is the error message
|
Thanks, simplified a bit: In [11]: pd.Series([0, 0], dtype='object')
Out[11]:
0 0
1 0
dtype: object
In [12]: pd.Series([0, 0], dtype='object').argmax()
/Users/taugspurger/Envs/pandas-dev/bin/ipython:1: FutureWarning: 'argmax' is deprecated. Use 'idxmax' instead. The behavior of 'argmax' will be corrected to return the positional maximum in the future. Use 'series.values.argmax' to get the position of the maximum now.
#!/Users/taugspurger/Envs/pandas-dev/bin/python3.6
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-e0ba19c8565d> in <module>()
----> 1 pd.Series([0, 0], dtype='object').argmax()
~/Envs/pandas-dev/lib/python3.6/site-packages/pandas/pandas/util/_decorators.py in wrapper(*args, **kwargs)
34 def wrapper(*args, **kwargs):
35 warnings.warn(msg, klass, stacklevel=stacklevel)
---> 36 return alternative(*args, **kwargs)
37 return wrapper
38
~/Envs/pandas-dev/lib/python3.6/site-packages/pandas/pandas/core/series.py in idxmax(self, axis, skipna, *args, **kwargs)
1355 """
1356 skipna = nv.validate_argmax_with_skipna(skipna, args, kwargs)
-> 1357 i = nanops.nanargmax(_values_from_object(self), skipna=skipna)
1358 if i == -1:
1359 return np.nan
~/Envs/pandas-dev/lib/python3.6/site-packages/pandas/pandas/core/nanops.py in _f(*args, **kwargs)
72 if any(self.check(obj) for obj in obj_iter):
73 msg = 'reduction operation {name!r} not allowed for this dtype'
---> 74 raise TypeError(msg.format(name=f.__name__.replace('nan', '')))
75 try:
76 with np.errstate(invalid='ignore'):
TypeError: reduction operation 'argmax' not allowed for this dtype Is there a reason you're using object dtype here? |
We'll need to think about whether we want to emulate NumPy here though. It's nice to know ahead of time whether you function is valid or not for the type of the values being passed. With |
I think for object dtype we should not, beforehand, decide whether such an operation works or not, but IMO we should defer that to the actual objects. Eg min/max works on strings, and so it seems logical that |
Fortunately, In [1]: import pandas as pd
In [2]: pd.__version__
Out[2]: '0.20.3'
In [3]: pd.Series(['a', 'b']).argmax()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-4747fce7cbb5> in <module>()
----> 1 pd.Series(['a', 'b']).argmax()
~/miniconda3/envs/pandas-0.20.3/lib/python3.6/site-packages/pandas/core/series.py in idxmax(self, axis, skipna, *args, **kwargs)
1262 """
1263 skipna = nv.validate_argmax_with_skipna(skipna, args, kwargs)
-> 1264 i = nanops.nanargmax(_values_from_object(self), skipna=skipna)
1265 if i == -1:
1266 return np.nan
~/miniconda3/envs/pandas-0.20.3/lib/python3.6/site-packages/pandas/core/nanops.py in nanargmax(values, axis, skipna)
476 """
477 values, mask, dtype, _ = _get_values(values, skipna, fill_value_typ='-inf',
--> 478 isfinite=True)
479 result = values.argmax(axis)
480 result = _maybe_arg_null_out(result, axis, mask, skipna)
~/miniconda3/envs/pandas-0.20.3/lib/python3.6/site-packages/pandas/core/nanops.py in _get_values(values, skipna, fill_value, fill_value_typ, isfinite, copy)
194 values = _values_from_object(values)
195 if isfinite:
--> 196 mask = _isfinite(values)
197 else:
198 mask = isnull(values)
~/miniconda3/envs/pandas-0.20.3/lib/python3.6/site-packages/pandas/core/nanops.py in _isfinite(values)
237 is_integer_dtype(values) or is_bool_dtype(values)):
238 return ~np.isfinite(values)
--> 239 return ~np.isfinite(values.astype('float64'))
240
241
ValueError: could not convert string to float: 'b' But yes, I suppose that we should attempt to support it. |
Ah, yes :-) Although in numpy it works:
|
Just refreshing my memory — so in the course of tracking down the bug that prompted #16449, it turned out that |
I'm not sure to get all the thing involving here, but in the exemple given by @barondu (for MorvanZhou code), is the only solution to downgrade pandas ? Is there a simpler solution like to replace argmax by an other function ? |
As a workaround, you can call
|
I faced the same issue and I tried with |
I'm getting this issue in pandas 1.1.5 with a Series of dtype |
Seeing the same issue as @mar-ses in pandas 1.3.3, i.e. |
I was doing
action = state_action.idxmax()
where state_action was of type 'pandas.core.series.Series'. When I run in 0.21.0, it gives the following error:However, when I downgraded to pandas 0.20.3, it worked just fine. You might wanna look into this. :)
The text was updated successfully, but these errors were encountered: