Skip to content

BUG: Index.str.partition not nan-safe #23558

Closed
@h-vetinari

Description

@h-vetinari

While working on #23167, I found a corner case where Index.str.partition and Index.str.rpartition break in the presence of NaNs. I do not believe this is intentional (and it's not mentioned in the docs):

>>> import pandas as pd
>>> pd.Index(['a', 'b', 'c']).str.partition(' ')  # works
MultiIndex(levels=[['a', 'b', 'c'], [''], ['']],
           labels=[[0, 1, 2], [0, 0, 0], [0, 0, 0]])
>>>
>>> pd.Index(['a', np.nan, 'c']).str.partition(' ')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\ProgramData\Miniconda3\envs\pandas-dev\lib\site-packages\pandas\core\strings.py", line 2391, in partition
    return self._wrap_result(result, expand=expand)
  File "C:\ProgramData\Miniconda3\envs\pandas-dev\lib\site-packages\pandas\core\strings.py", line 2014, in _wrap_result
    out = MultiIndex.from_tuples(result, names=name)
  File "C:\ProgramData\Miniconda3\envs\pandas-dev\lib\site-packages\pandas\core\indexes\multi.py", line 1326, in from_tuples
    arrays = list(lib.to_object_array_tuples(tuples).T)
  File "pandas/_libs/src\inference.pyx", line 1559, in pandas._libs.lib.to_object_array_tuples
TypeError: object of type 'float' has no len()

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolateStringsString extension data type and string data

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions