Skip to content

DataFrame.reset_index(inplace=True) with column names collision brakes dataframe #26998

Open
@s-wakaba

Description

@s-wakaba

Code Sample, a copy-pastable example if possible

import pandas as pd
import numpy as np

a = pd.DataFrame(
    np.random.random(12).reshape([4,3]),
    columns = ['A', 'B', 'C'],
    index = pd.MultiIndex.from_product([['p','q'],['x','y']], names=['A','D']),
)

print(a)
#             A         B         C
# A D                              
# p x  0.809155  0.181919  0.957133
#   y  0.625364  0.802854  0.065737
# q x  0.288116  0.570645  0.944983
#   y  0.926358  0.684897  0.724123

try:
    a.reset_index(inplace=True)
except ValueError as e:
    print(e) # cannot insert A, already exists

print(a)
#      D         A         B         C
# A D                                 
# p x  x  0.809155  0.181919  0.957133
#   y  y  0.625364  0.802854  0.065737
# q x  x  0.288116  0.570645  0.944983
#   y  y  0.926358  0.684897  0.724123

Problem description

when there are columns with the same name in data-body and multi-index on calling DataFrame.reset_index(), ValueError is thrown. however, if inplace=True option is specified, this operation is not atomic and leaves DataFrame itself incomplete.
I think keeping original data is better and consistent when such exceptions are occurred.

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.7.1.final.0
python-bits: 64
OS: Linux
OS-release: 3.10.0-862.3.2.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.24.2
pytest: 4.0.2
pip: 10.0.1
setuptools: 39.0.1
Cython: 0.29.2
numpy: 1.15.4
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 7.2.0
sphinx: 1.8.2
patsy: 0.5.1
dateutil: 2.7.5
pytz: 2018.7
blosc: None
bottleneck: 1.2.1
tables: 3.4.4
numexpr: 2.6.8
feather: None
matplotlib: 3.0.2
openpyxl: 2.5.12
xlrd: 1.2.0
xlwt: 1.3.0
xlsxwriter: 1.1.2
lxml.etree: 4.2.5
bs4: 4.6.3
html5lib: 1.0.1
sqlalchemy: 1.2.15
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BuginplaceRelating to inplace parameter or equivalent

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions