Skip to content

set_index breaks with multi keys but one empty #1971

@hayd

Description

@hayd

Migrated from StackOverflow: http://stackoverflow.com/questions/12598520/set-index-on-multiple-columns-with-one-empty-column

df = DataFrame([
    dict(a=1, p=0), 
    dict(a=2, m=10), 
    dict(a=3, m=11, p=20), 
    dict(a=4, m=12, p=21)
], columns=('a', 'm', 'p', 'x'))
     a     m    p     x
  0  1   NaN    0   NaN
  1  2    10  NaN   NaN
  2  3    11   20   NaN
  3  4    12   21   NaN
# single column index on an empty column works
df.set_index(['x'])
# two-columns index on non-empty columns works
df.set_index(['a', 'm'])
df.set_index(['a', 'p'])
df.set_index(['m', 'p'])

# but two-columns index including an empty column fails
df.set_index(['a', 'x'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/pandas-0.8.2.dev_f5a74d4_20120725-py2.7-macosx-10.8-x86_64.egg/pandas/core/frame.py", line 2328, in set_index
    if verify_integrity and not index.is_unique:
  File "properties.pyx", line 27, in pandas.lib.cache_readonly.__get__ (pandas/src/tseries.c:95395)
  File "/Library/Python/2.7/site-packages/pandas-0.8.2.dev_f5a74d4_20120725-py2.7-macosx-10.8-x86_64.egg/pandas/core/index.py", line 227, in is_unique
    return self._engine.is_unique
  File "engines.pyx", line 186, in pandas.lib.IndexEngine.is_unique.__get__ (pandas/src/tseries.c:115047)
  File "engines.pyx", line 215, in pandas.lib.IndexEngine._do_unique_check (pandas/src/tseries.c:115456)
  File "engines.pyx", line 228, in pandas.lib.IndexEngine._ensure_mapping_populated (pandas/src/tseries.c:115629)
  File "engines.pyx", line 231, in pandas.lib.IndexEngine.initialize (pandas/src/tseries.c:115678)
  File "engines.pyx", line 212, in pandas.lib.IndexEngine._get_index_values (pandas/src/tseries.c:115414)
  File "/Library/Python/2.7/site-packages/pandas-0.8.2.dev_f5a74d4_20120725-py2.7-macosx-10.8-x86_64.egg/pandas/core/index.py", line 247, in <lambda>
    return self._engine_type(lambda: self.values, len(self))
  File "/Library/Python/2.7/site-packages/pandas-0.8.2.dev_f5a74d4_20120725-py2.7-macosx-10.8-x86_64.egg/pandas/core/index.py", line 1363, in values
    for lev, lab in zip(self.levels, self.labels)]
  File "/Library/Python/2.7/site-packages/pandas-0.8.2.dev_f5a74d4_20120725-py2.7-macosx-10.8-x86_64.egg/pandas/core/common.py", line 348, in ndtake
    return arr.take(_ensure_platform_int(indexer), axis=axis, out=out)
IndexError: index -1 is out of bounds for axis 0 with size 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions