Skip to content

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

Closed
@ketozhang

Description

@ketozhang

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions