Skip to content

BUG: DataFrame.unstack on non-consolidated frame #34708

@jbrockmendel

Description

@jbrockmendel
df = pd.DataFrame({"x": [1, 2, np.NaN], "y": [3.0, 4, np.NaN]})

df2 = df[["x"]]
df2["y"] = df["y"]
assert len(df2._mgr.blocks) == 2

>>> df2.unstack()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/core/frame.py", line 7031, in unstack
    return unstack(self, level, fill_value)
  File "pandas/core/reshape/reshape.py", line 419, in unstack
    return _unstack_frame(obj, level, fill_value=fill_value)
  File "pandas/core/reshape/reshape.py", line 435, in _unstack_frame
    unstacker = _Unstacker(obj.index, level=level)
  File "pandas/core/reshape/reshape.py", line 93, in __init__
    self.index = index.remove_unused_levels()
AttributeError: 'RangeIndex' object has no attribute 'remove_unused_levels'

Metadata

Metadata

Assignees

No one assigned

    Labels

    RegressionFunctionality that used to work in a prior pandas versionReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions