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: df.loc[:, col] type assignments doesn't propagate dtype changes #52612

Closed
2 of 3 tasks
briochh opened this issue Apr 11, 2023 · 1 comment
Closed
2 of 3 tasks

BUG: df.loc[:, col] type assignments doesn't propagate dtype changes #52612

briochh opened this issue Apr 11, 2023 · 1 comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@briochh
Copy link

briochh commented Apr 11, 2023

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([1.0, 2.0, 3.0])
print(df[0].dtypes)
df.loc[:, 0] = df.loc[:, 0].astype(int)
print(df[0].dtypes)

Issue Description

It used to be possible to modify a columns dtype using some thing like:
df.loc[:, col] = df.loc[:, col].astype(int)
Since version 2.0 it seems that the RHS is now cast to the LHS dtype when using this sort of assignment (if possible). Therefore the dtype of the Series associated with col maybe silently unchanged.

Perhaps this is by design and not a bug. Should column modification not use this syntax and instead use df[col]=?

Expected Behavior

Expected the second print statement to return int64 not float64

Installed Versions

INSTALLED VERSIONS

commit : 478d340
python : 3.11.3.final.0
python-bits : 64
OS : Darwin
OS-release : 22.4.0
Version : Darwin Kernel Version 22.4.0: Mon Mar 6 20:59:28 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_NZ.UTF-8
pandas : 2.0.0
numpy : 1.24.2
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.6.1
pip : 23.0.1
Cython : None
pytest : 7.3.0
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.12.0
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.1
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@briochh briochh added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 11, 2023
@phofl
Copy link
Member

phofl commented Apr 12, 2023

Duplicate of #52593

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

2 participants