Skip to content

BUG: DataFrame.update do not preserve string dtype #44104

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

Open
2 of 3 tasks
ali-cetin-4ss opened this issue Oct 19, 2021 · 4 comments
Open
2 of 3 tasks

BUG: DataFrame.update do not preserve string dtype #44104

ali-cetin-4ss opened this issue Oct 19, 2021 · 4 comments
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions Strings String extension data type and string data

Comments

@ali-cetin-4ss
Copy link

  • 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 master branch of pandas.

Reproducible Example

df_org = pd.DataFrame({"a": [None, "two"], "b": [1, 2]}, index=[0, 1]).astype({"a": "string", "b": "Int64"})
df_update = pd.DataFrame({"a": ["one"]}, index=[0], dtype="string")

print(df_org, "\n")
print(df_org.dtypes, "\n")

print(df_update, "\n")
print(df_update.dtypes, "\n")

# update with consistent dtype
df_org.update(df_update)

print(df_org, "\n")
print(df_org.dtypes, "\n")

# dtype not conserved - string -> object

Issue Description

String dtype column is casted to object when .update is performed even when the "other" dataframe has consistent dtypes. See code example

Expected Behavior

The dtype should be preserved after the update operation, i.e., string should remain string and not casted to object.

Installed Versions

INSTALLED VERSIONS

commit : 945c9ed
python : 3.8.10.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19043
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : Norwegian Bokmål_Norway.1252

pandas : 1.3.4
numpy : 1.20.3
pytz : 2021.3
dateutil : 2.8.2
pip : 21.1.3
setuptools : 58.2.0
Cython : None
pytest : 6.2.5
hypothesis : None
sphinx : 4.0.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.2
IPython : 7.28.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 5.0.0
pyxlsb : None
s3fs : None
scipy : 1.7.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : 0.54.0

@ali-cetin-4ss ali-cetin-4ss added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 19, 2021
@CloseChoice
Copy link
Member

>>> import pandas as pd
>>> df_org = pd.DataFrame({"a": [None, "two"], "b": [1, 2]}, index=[0, 1]).astype({"a": "string", "b": "Int64"})
>>> df_update = pd.DataFrame({"a": ["one"]}, index=[0], dtype="string")
>>> 
>>> print(df_org, "\n")
      a  b
0  <NA>  1
1   two  2 

>>> print(df_org.dtypes, "\n")
a    string
b     Int64
dtype: object 

>>> 
>>> print(df_update, "\n")
     a
0  one 

>>> print(df_update.dtypes, "\n")
a    string
dtype: object 

>>> 
>>> # update with consistent dtype
>>> df_org.update(df_update)
>>> 
>>> print(df_org, "\n")
     a  b
0  one  1
1  two  2 

>>> print(df_org.dtypes, "\n")
a    object
b     Int64
dtype: object 

Is this a behaviour that was different in previous releases? Is this really unexpected behaviour? I guess there are a couple of situations where we cast to object without necessity.

@CloseChoice CloseChoice added the Dtype Conversions Unexpected or buggy dtype conversions label Oct 27, 2021
@ali-cetin-4ss
Copy link
Author

For an outsider, this feels like update method is not aware of the "new" built-in native string data type. In practice, it's a bit confusing to unintentionally re-cast dtypes without any apparent reason.

@mroeschke mroeschke added Strings String extension data type and string data and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 30, 2021
@rpkilby
Copy link

rpkilby commented Oct 26, 2022

This issue seems to affect other new-style dtypes such as Int64. Additionally, I think this is the source of some update() calls generating FutureWarnings about loc/iloc usage.

@rpkilby
Copy link

rpkilby commented Oct 26, 2022

After digging through the issue tracker a bit further, I believe this can be closed as a duplicate of #4094.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions Strings String extension data type and string data
Projects
None yet
Development

No branches or pull requests

4 participants