Skip to content

BUG: mapping tuples of different lengths unsupported with IndexError: tuple index out of range #50293

@sheldonkhall

Description

@sheldonkhall

Failing code snippet on current master branch:

>>> import pandas as pd
>>> data = pd.DataFrame({'data': [("a", "b",), ("b",)]})
>>> data['data'].map({("a", "b"): "new_value"})

I stumbled across this one by chance working on a data frame containing sequences recorded as tuples in a column. I am not sure what is the correct behaviour here but it seems like something that should be handled without issue as these objects are hashable. The error generated is:

Error
**********************************************************************
File "/Users/halls23/Repos/pandas/pandas/core/series.py", line 316, in Series
Failed example:
    data['data'].map({("a", "b"): "new_value"})
Exception raised:
    Traceback (most recent call last):
      File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm/docrunner.py", line 138, in __run
        exec(compile(example.source, filename, "single",
      File "<doctest Series[18]>", line 1, in <module>
        data['data'].map({("a", "b"): "new_value"})
      File "/Users/halls23/Repos/pandas/pandas/core/series.py", line 4323, in map
        new_values = self._map_values(arg, na_action=na_action)
      File "/Users/halls23/Repos/pandas/pandas/core/base.py", line 871, in _map_values
        indexer = mapper.index.get_indexer(values)
      File "/Users/halls23/Repos/pandas/pandas/core/indexes/base.py", line 3585, in get_indexer
        pself, ptarget = self._maybe_promote(target)
      File "/Users/halls23/Repos/pandas/pandas/core/indexes/base.py", line 5831, in _maybe_promote
        other = type(self).from_tuples(other)  # type: ignore[attr-defined]
      File "/Users/halls23/Repos/pandas/pandas/core/indexes/multi.py", line 210, in new_meth
        return meth(self_or_cls, *args, **kwargs)
      File "/Users/halls23/Repos/pandas/pandas/core/indexes/multi.py", line 583, in from_tuples
        arrays = list(lib.tuples_to_object_array(tuples).T)
      File "pandas/_libs/lib.pyx", line 2860, in pandas._libs.lib.tuples_to_object_array
    IndexError: tuple index out of range

Full version information:

    <BLANKLINE>
    INSTALLED VERSIONS
    ------------------
    commit           : a5cbd1e52e9078637c899167f40a29b4bc8901b9
    python           : 3.8.15.final.0
    python-bits      : 64
    OS               : Darwin
    OS-release       : 21.6.0
    Version          : Darwin Kernel Version 21.6.0: Thu Sep 29 20:12:57 PDT 2022; root:xnu-8020.240.7~1/RELEASE_X86_64
    machine          : x86_64
    processor        : i386
    byteorder        : little
    LC_ALL           : None
    LANG             : None
    LOCALE           : en_GB.UTF-8
    <BLANKLINE>
    pandas           : 2.0.0.dev0+923.ga5cbd1e52e.dirty
    numpy            : 1.23.5
    pytz             : 2022.6
    dateutil         : 2.8.2
    setuptools       : 65.5.1
    pip              : 22.3.1
    Cython           : 0.29.32
    pytest           : 7.2.0
    hypothesis       : 6.61.0
    sphinx           : 4.5.0
    blosc            : None
    feather          : None
    xlsxwriter       : 3.0.3
    lxml.etree       : 4.9.2
    html5lib         : 1.1
    pymysql          : 1.0.2
    psycopg2         : 2.9.3
    jinja2           : 3.1.2
    IPython          : 8.7.0
    pandas_datareader: None
    bs4              : 4.11.1
    bottleneck       : 1.3.5
    brotli           : 
    fastparquet      : 2022.12.0
    fsspec           : 2021.11.0
    gcsfs            : 2021.11.0
    matplotlib       : 3.6.2
    numba            : 0.56.4
    numexpr          : 2.8.4
    odfpy            : None
    openpyxl         : 3.0.10
    pandas_gbq       : None
    pyarrow          : 9.0.0
    pyreadstat       : 1.2.0
    pyxlsb           : 1.0.10
    s3fs             : 2021.11.0
    scipy            : 1.9.3
    snappy           : 
    sqlalchemy       : 1.4.45
    tables           : 3.7.0
    tabulate         : 0.9.0
    xarray           : 2022.12.0
    xlrd             : 2.0.1
    zstandard        : 0.19.0
    tzdata           : None
    qtpy             : None
    pyqt5            : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions