Skip to content

Adding a column via assign or __setitem__ on an empty DataFrame changes index type, removes index name #24878

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

Closed
p-himik opened this issue Jan 22, 2019 · 3 comments

Comments

@p-himik
Copy link

p-himik commented Jan 22, 2019

Code Sample, a copy-pastable example if possible

import pandas as pd

d = pd.DataFrame(dict(a=[]), index=pd.Series([], name='my_index'))
d2 = d.assign(b=[])

d.index
# Float64Index([], dtype='float64', name='my_index')

d2.index
# RangeIndex(start=0, stop=0, step=1)

d['b'] = []
d.index
# RangeIndex(start=0, stop=0, step=1)

Problem description

I don't think that adding a new column to a DataFrame should change index in any way, let alone change its type and remove its name. It doesn't matter if there's no actual data - it still forces the user to write special checks.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.1.final.0
python-bits: 64
OS: Linux
OS-release: 4.18.0-13-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.4
pytest: None
pip: 18.1
setuptools: 40.6.3
Cython: None
numpy: 1.15.4
scipy: None
pyarrow: None
xarray: None
IPython: 7.2.0
sphinx: None
patsy: None
dateutil: 2.7.5
pytz: 2018.9
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.0.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@p-himik p-himik changed the title Calling assign on an empty DataFrame changes index type, removes index name Adding a column via assign or __setattr__ on an empty DataFrame changes index type, removes index name Jan 22, 2019
@TomAugspurger
Copy link
Contributor

The bug in is in DataFrame.__setitem__, not assign.

In [23]: df = pd.DataFrame({'a': []}, index=pd.Index([], name='my_index'))

In [24]: df['b'] = []

In [25]: df.index
Out[25]: RangeIndex(start=0, stop=0, step=1)

Can you check if we have an issue for this? It sounds familiar.

@p-himik p-himik changed the title Adding a column via assign or __setattr__ on an empty DataFrame changes index type, removes index name Adding a column via assign or __setitem__ on an empty DataFrame changes index type, removes index name Jan 22, 2019
@p-himik
Copy link
Author

p-himik commented Jan 22, 2019

@TomAugspurger Didn't check __setitem__ before opening the issue. You're right: #22060

@p-himik p-himik closed this as completed Jan 22, 2019
@TomAugspurger
Copy link
Contributor

TomAugspurger commented Jan 22, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants