Skip to content

BUG or DOC: NaNs generated when enlarge DataFrame with loc[:, cols] with a DataFrame RHS #47112

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
sappersapper opened this issue May 25, 2022 · 2 comments
Closed
2 of 3 tasks
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Usage Question

Comments

@sappersapper
Copy link

sappersapper commented May 25, 2022

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
import numpy as np

df = pd.DataFrame(np.ones([3, 3]), columns=list('abc'))
df.loc[:, ['d', 'e']] = df[['a', 'b']]
df

	a 	b 	c 	d 	e
0 	1.0 	1.0 	1.0 	NaN 	NaN
1 	1.0 	1.0 	1.0 	NaN 	NaN
2 	1.0 	1.0 	1.0 	NaN 	NaN

Issue Description

To enlarge a DataFrame, these three ways can get right result:

df[['d', 'e']] = df[['a', 'b']]
df.loc[:, ['d', 'e']] = df[['a', 'b']].to_numpy()
df.loc[:, 'd'] = df['a']
df.loc[:, 'e'] = df['b']

But after df.loc[:, ['d', 'e']] = df[['a', 'b']], new columns only contain NaNs, it seems inconsistent.

I find Warning on assignment with .loc[:, cols], but the warning is about assigning existing columns, and there is no description in the documentation for Setting with enlargement.

Is it appropriate to extend the behavior of .loc[:, newcols]? or enhance the doc? or raise warning?

Expected Behavior

  a b c d e
0 1.0 1.0 1.0 1.0 1.0
1 1.0 1.0 1.0 1.0 1.0
2 1.0 1.0 1.0 1.0 1.0

Installed Versions

INSTALLED VERSIONS

commit : 4bfe3d0
python : 3.9.10.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19043
machine : AMD64
processor : Intel64
byteorder : little
LC_ALL : None
LANG : None
LOCALE : Chinese (Simplified)_China.936

pandas : 1.4.2
numpy : 1.22.4
pytz : 2022.1
dateutil : 2.8.2
pip : 22.1.1
setuptools : 62.3.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.3.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli :
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : 2.1.1
matplotlib : 3.5.1
numba : 0.54.0rc1+0.g9bed2ebb2.dirty
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 3.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.1
snappy : None
sqlalchemy : None
tables : None
tabulate : 0.8.9
xarray : 2022.3.0
xlrd : 2.0.1
xlwt : None
zstandard : None

@sappersapper sappersapper added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 25, 2022
@simonjayhawkins simonjayhawkins added the Indexing Related to indexing on series/frames, not to indexes themselves label May 25, 2022
@simonjayhawkins
Copy link
Member

Thanks @sappersapper for the report.

The issue here is touched upon in the discussion and the third code sample in #46974

cc @jorisvandenbossche

@simonjayhawkins simonjayhawkins removed the Needs Triage Issue that has not been reviewed by a pandas team member label May 25, 2022
@simonjayhawkins simonjayhawkins changed the title BUG or DOC: NaNs generated when enlarge DataFrame with loc[:, cols] BUG or DOC: NaNs generated when enlarge DataFrame with loc[:, cols] with a DataFrame RHS May 25, 2022
@phofl
Copy link
Member

phofl commented May 27, 2022

Yep, this is the same behavior as in the other issue. loc aligns your columns while setitem does not. I think we can close here, since this is not a bug right now

@phofl phofl added the Closing Candidate May be closeable, needs more eyeballs label May 27, 2022
@simonjayhawkins simonjayhawkins removed the Closing Candidate May be closeable, needs more eyeballs label May 27, 2022
@simonjayhawkins simonjayhawkins added this to the No action milestone May 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Usage Question
Projects
None yet
Development

No branches or pull requests

3 participants