Skip to content

BUG: DataFrame constructor with copy=False and missing columns creates columns that are views of each other #45369

Closed
@jorisvandenbossche

Description

@jorisvandenbossche

See #44889 (comment)

With copy=False and constructing a DataFrame with missing columns in the passed data, the columns get constructed as identical arrays, so mutating one also incorrectly mutates the other columns:

In [10]: df = pd.DataFrame(index=[1, 2, 3], columns=["a", "b", "c"], copy=False)

In [11]: df.iloc[0, 0] = 0

In [12]: df
Out[12]: 
     a    b    c
1    0    0    0
2  NaN  NaN  NaN
3  NaN  NaN  NaN

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugConstructorsSeries/DataFrame/Index/pd.array ConstructorsDataFrameDataFrame data structureRegressionFunctionality 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