Skip to content

BUG: Series.replace widens dtype to object when imputing pd.NA #48409

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
randolf-scholz opened this issue Sep 6, 2022 · 1 comment
Closed
2 of 3 tasks
Labels

Comments

@randolf-scholz
Copy link
Contributor

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
x = pandas.Series([1.0, 3.1, float('nan')])
print(x.dtype)
y = x.replace(float('nan'), pandas.NA)
print(y.dtype)    # object ✘ (expected: Float64)
z = x.replace(1, 1+1j)
print(z.dtype)    # complex128 ✔

Issue Description

Replacing a value with pandas.NA widens the type to object.

Expected Behavior

The data type should only be widened as much as necessary. One would expect overloaded signatures such as

@overload
def replace(self: Series[np.float32], to_replace: np.float32, value: NAType) -> Series[Float32Dtype]: ...
@overload
def replace(self: Series[np.float64], to_replace: np.float64, value: NAType) -> Series[Float64Dtype]: ...
@overload
def replace(self: Series[np.int32], to_replace: np.int32, value: NAType) -> Series[Int32Dtype]: ...
@overload
def replace(self: Series[np.int64], to_replace: np.int64, value: NAType) -> Series[Int64Dtype]: ...
@overload
def replace(self: Series[np.bool_], to_replace: np.bool_, value: NAType) -> Series[BooleanDtype]: ...

related: pandas-dev/pandas-stubs#262

Installed Versions

INSTALLED VERSIONS

commit : ca60aab
python : 3.10.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-47-generic
Version : #51-Ubuntu SMP Thu Aug 11 07:51:15 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.4
numpy : 1.23.2
pytz : 2022.2.1
dateutil : 2.8.2
setuptools : 65.3.0
pip : 22.2.2
Cython : None
pytest : 7.1.2
hypothesis : None
sphinx : 5.1.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.4.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : 2.1.1
matplotlib : 3.5.3
numba : None
numexpr : 2.8.3
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 9.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.1
snappy : None
sqlalchemy : None
tables : 3.7.0
tabulate : 0.8.10
xarray : None
xlrd : None
xlwt : None
zstandard : None

@randolf-scholz randolf-scholz added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 6, 2022
@randolf-scholz randolf-scholz changed the title BUG: Series.replace BUG: Series.replace widens dtype to object when imputing pd.NA Sep 6, 2022
@phofl
Copy link
Member

phofl commented Sep 6, 2022

Hi, thanks for your report. Numpy Dtypes can not hold pd.NA, hence we are casting to object. You have to use nullable dtypes explicitly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants