Panda Series dtype for numerical values that includes Nan / Series.idxmax() with np.NA raises a TypeError

Viewed 86

samples used

test = pd.Series(range(10))                                   # dtype('int64')
test2 = pd.Series([1, 2, 3, 4, np.nan])                       # dtype('float64')
test3 = pd.Series([1, 2, 3, 4, pd.NA], dtype='Int32')         # Int32Dtype()

test.idxmax()  # -> work as expected
test_2.idxmax()  # -> work as expected
test_3.idxmax()  # -> raise a TypeError

The error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-112-6be3612f06b5> in <module>
----> 1 test3.idxmax()

~/.local/share/virtualenvs/IRONHACK-M0-SaubP/lib/python3.9/site-packages/pandas/core/series.py in idxmax(self, axis, skipna, *args, **kwargs)
   2170         """
   2171         skipna = nv.validate_argmax_with_skipna(skipna, args, kwargs)
-> 2172         i = nanops.nanargmax(self._values, skipna=skipna)
   2173         if i == -1:
   2174             return np.nan

~/.local/share/virtualenvs/IRONHACK-M0-SaubP/lib/python3.9/site-packages/pandas/core/nanops.py in _f(*args, **kwargs)
     69             try:
     70                 with np.errstate(invalid="ignore"):
---> 71                     return f(*args, **kwargs)
     72             except ValueError as e:
     73                 # we want to transform an object array

~/.local/share/virtualenvs/IRONHACK-M0-SaubP/lib/python3.9/site-packages/pandas/core/nanops.py in nanargmax(values, axis, skipna, mask)
   1025     """
   1026     values, mask, _, _, _ = _get_values(values, True, fill_value_typ="-inf", mask=mask)
-> 1027     result = values.argmax(axis)
   1028     result = _maybe_arg_null_out(result, axis, mask, skipna)
   1029     return result

TypeError: argmax() takes 1 positional argument but 2 were given

My question

The first two Series are usual and everything works. But as I understood, pd.NA and the 'Int32' dtype was supposed to be a combination that authorise the manipulation of integers while keeping na values, a big improvement if that's the case.

But as you can see, if I want to use such a combination, I need to always think of using col.astype('float').idxmax() to prevent the exception.

Is there another solution ?

0 Answers
Related