Skip to content

BUG: Index.str.partition gives ValueError while trying to compute index name (expand=False) #12617

Closed
@pwaller

Description

@pwaller

Index.str.partition's behaviour changed in 3ab35b4 in a backwards incompatible way which doesn't seem intentional.

Code Sample, a copy-pastable example if possible

python3 -c 'import pandas; pandas.Index(["a,b", "c,d"], name="hello").str.partition(",")'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/pwaller/.local/src/pandas/pandas/core/strings.py", line 1432, in partition
    return self._wrap_result(result, expand=expand)
  File "/home/pwaller/.local/src/pandas/pandas/core/strings.py", line 1348, in _wrap_result
    return MultiIndex.from_tuples(result, names=name)
  File "/home/pwaller/.local/src/pandas/pandas/indexes/multi.py", line 889, in from_tuples
    return MultiIndex.from_arrays(arrays, sortorder=sortorder, names=names)
  File "/home/pwaller/.local/src/pandas/pandas/indexes/multi.py", line 844, in from_arrays
    names=names, verify_integrity=False)
  File "/home/pwaller/.local/src/pandas/pandas/indexes/multi.py", line 92, in __new__
    result._set_names(names)
  File "/home/pwaller/.local/src/pandas/pandas/indexes/multi.py", line 446, in _set_names
    raise ValueError('Length of names must match number of levels in '
ValueError: Length of names must match number of levels in MultiIndex.

Expected Output

In [1]: import pandas; pandas.Index(["a,b", "c,d"], name="hello").str.partition(",")
Out[1]: 
MultiIndex(levels=[['a', 'c'], [','], ['b', 'd']],
           labels=[[0, 1], [0, 0], [0, 1]])

Versions

I bisected the problem to 3ab35b4 (cc @sinhrks). The problem is not present in 0.17.1 and is present in 0.18.0rc2.

Note: when I use a name="foo", it happens to work because it has the correct length, the index name ends up being ["f", "o", "o"], so it's possible this is tested somewhere and happens to work even though it's not correct for a large number of use cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugStringsString extension data type and string data

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions