You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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
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
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
Issue Description
To enlarge a DataFrame, these three ways can get right result:
But after
df.loc[:, ['d', 'e']] = df[['a', 'b']]
, new columns only containNaNs
, 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
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
The text was updated successfully, but these errors were encountered: