Skip to content

BUG: 1.4.0 does not preserve initially empty Index and appended by loc assignment. #45621

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
2 of 3 tasks
ketozhang opened this issue Jan 25, 2022 · 2 comments · Fixed by #45623
Closed
2 of 3 tasks

BUG: 1.4.0 does not preserve initially empty Index and appended by loc assignment. #45621

ketozhang opened this issue Jan 25, 2022 · 2 comments · Fixed by #45623
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@ketozhang
Copy link

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

df = pd.DataFrame(columns=["a", "b"]).set_index("a")
print(df.index, id(df.index))  # Index([], dtype='object', name='a') 4644041536
df.loc[0] = {"b": 0}
print(df.index, id(df.index))  # Int64Index([0], dtype='int64') 4726025904

Issue Description

Given an empty dataframe with a index column name set, when adding a row on a specific index using df.loc[INDEX], the original index is not preserved. Furthermore, what gets returned is not typeIndex but is Int64Index when the passed in index is an integer.

Expected Behavior

The modified index should print out Index([0], dtype='int64', name='a').

Installed Versions

1.4.0

@ketozhang ketozhang added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 25, 2022
@ketozhang ketozhang changed the title BUG: BUG: Pandas 1.4.0 does not preserve the same Index instantiated with DataFrame after loc assignment. Jan 25, 2022
@ketozhang ketozhang changed the title BUG: Pandas 1.4.0 does not preserve the same Index instantiated with DataFrame after loc assignment. BUG: 1.4.0 does not preserve the same Index instantiated with DataFrame after loc assignment. Jan 25, 2022
@ketozhang ketozhang changed the title BUG: 1.4.0 does not preserve the same Index instantiated with DataFrame after loc assignment. BUG: 1.4.0 does not preserve initially empty Index and appended by loc assignment. Jan 25, 2022
@phofl
Copy link
Member

phofl commented Jan 25, 2022

@jbrockmendel This was changed in #43507, looks somehow intended. Did you mean to change this also for the index?

@jbrockmendel
Copy link
Member

IIRC the dtype change is expected, but losing the name is not

@phofl phofl added Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 25, 2022
@phofl phofl added this to the 1.4.1 milestone Jan 25, 2022
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Feb 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants