Skip to content

BUG: idxmax raises when used with tuples #43697

Closed
@helgehatt

Description

@helgehatt

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the master branch of pandas.

Reproducible Example

import pandas as pd

x = pd.Series([(1,3),(2,2),(3,1)])

# This succeeds as the max operation is performed on the first element of a tuple
assert x.max() == (3,1)

# This fails with `TypeError: reduction operation 'argmax' not allowed for this dtype`
assert x.idxmax() == 2

# Using numpy directly works as you'd expect
import numpy as np
assert np.argmax(x.values) == 2

Issue Description

Using max on a series of tuples returns the tuple as if the max operation was performed on the first element of each tuple.

Using idxmax however, raises an operation not allowed TypeError.

Expected Behavior

Even though there are several ways of achieving the desired result, I would expect consistency between the usage of max and idxmax, as it is for max and argmax in numpy.

Installed Versions

INSTALLED VERSIONS

commit : 73c6825
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-1113-azure
Version : #126~16.04.1-Ubuntu SMP Tue Apr 13 16:55:24 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.3
numpy : 1.20.1
pytz : 2021.1
dateutil : 2.8.2
pip : 21.1.3
setuptools : 52.0.0.post20210125
Cython : 0.29.23
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.22.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : 1.3.2
fsspec : 2021.06.0
fastparquet : None
gcsfs : None
matplotlib : 3.3.4
numexpr : 2.7.3
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.2
sqlalchemy : None
tables : 3.6.1
tabulate : 0.8.9
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNested DataData where the values are collections (lists, sets, dicts, objects, etc.).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions