Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
pwaller opened this issue Mar 14, 2016 · 2 comments
Labels
Bug Strings String extension data type and string data
Milestone

Comments

@pwaller
Copy link
Contributor

pwaller commented Mar 14, 2016

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.

@pwaller pwaller changed the title REGR: Index.str.partition stopped working REGR: Index.str.partition gives ValueError while trying to compute index name Mar 14, 2016
@sinhrks sinhrks added Bug Strings String extension data type and string data labels Mar 14, 2016
@sinhrks sinhrks added this to the 0.18.1 milestone Mar 14, 2016
@sinhrks
Copy link
Member

sinhrks commented Mar 14, 2016

Thanks, and sorry for the inconvenience. I'll check soon.

@jreback
Copy link
Contributor

jreback commented Mar 14, 2016

Tthis is not a regression. The name argument you were using in 0.17.1 didn't do anything (a MultiIndex does not have a name, only names for the levels),

So the result (w/o specifying name) is correct.

The expand=False case has a bug in it.

In [50]: pandas.Index(["a,b", "c,d"], expand=True).str.partition(",")
Out[50]: 
MultiIndex(levels=[[u'a', u'c'], [u','], [u'b', u'd']],
           labels=[[0, 1], [0, 0], [0, 1]])

In [51]: pandas.Index(["a,b", "c,d"], expand=True).str.partition(",").names
Out[51]: FrozenList([None, None, None])

In [52]: pd.__version__
Out[52]: '0.18.0+7.g17a5982'

@jreback jreback changed the title REGR: Index.str.partition gives ValueError while trying to compute index name BUG: Index.str.partition gives ValueError while trying to compute index name (expand=False) Mar 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Strings String extension data type and string data
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants