Skip to content

reset_index() doesn't work with CategoricalIndex columns #19136

Closed
@Stanpol

Description

@Stanpol

Code Sample, a copy-pastable example if possible

df=pd.DataFrame({'Year': np.random.randint(2000,2017,10000), 'Month': np.random.randint(1,12,10000), 'Data': np.random.randint(0,100,10000)})
grouped=df.groupby(['Year','Month', pd.cut(df.Data, range(0,100,10))]).size().unstack()

# This doesn't work:
grouped.reset_index() #returns TypeError: unorderable types: int() < str()

# This works:
grouped.columns=grouped.columns.astype('str')
grouped.reset_index()

Problem description

reset_index() should work with dataframes that have any types of columns.

Expected Output

Two extra columns with multiindex content, in the example above - Year and Month.
Column's type changes to string? Or documentation should specify that reset_index() doesn't work with specific types of columns.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None

pandas: 0.22.0
pytest: None
pip: 9.0.1
setuptools: 36.4.0
Cython: 0.26
numpy: 1.13.3
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.1.0
sphinx: None
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: 3.4.2
numexpr: 2.6.4
feather: None
matplotlib: 2.1.1
openpyxl: None
xlrd: 1.1.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.9999999
sqlalchemy: 1.2.0
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

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