Skip to content
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

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

Closed
jorisvandenbossche opened this issue Jan 14, 2022 · 1 comment · Fixed by #45432
Labels
Bug Constructors Series/DataFrame/Index/pd.array Constructors DataFrame DataFrame data structure Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@jorisvandenbossche
Copy link
Member

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
@jorisvandenbossche jorisvandenbossche added Bug Constructors Series/DataFrame/Index/pd.array Constructors labels Jan 14, 2022
@mroeschke mroeschke added the DataFrame DataFrame data structure label Jan 14, 2022
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Jan 15, 2022
@simonjayhawkins
Copy link
Member

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:

This is a regression from 1.2.5

first bad commit: [66e6ba3] API: honor copy=True when passing dict to DataFrame (#38939)

cc @jbrockmendel

@simonjayhawkins simonjayhawkins added the Regression Functionality that used to work in a prior pandas version label Jan 15, 2022
@jreback jreback modified the milestones: 1.5, 1.4 Jan 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Constructors Series/DataFrame/Index/pd.array Constructors DataFrame DataFrame data structure Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants