Skip to content

BUG: replace throws TypeError: Invalid value with Extension dtypes #49022

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
bollard opened this issue Oct 10, 2022 · 1 comment
Closed
2 of 3 tasks

BUG: replace throws TypeError: Invalid value with Extension dtypes #49022

bollard opened this issue Oct 10, 2022 · 1 comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@bollard
Copy link

bollard commented Oct 10, 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

pd.Series([False, True], dtype=pd.BooleanDtype()).replace(False, 10).dtype
> TypeError: Invalid value '10' for dtype boolean

pd.Series([False, True], dtype=np.bool_).replace(False, 10).dtype
> dtype('O')

Issue Description

Hi there,

Notice some inconsistent behaviour between Pandas native extension arrays, and numpy equivalent dtypes. Specifically, in the numpy case, the dtype of the Series is downcast from bool -> object, whereas in the extension array case an error is thrown instead.

I can understand why the error is thrown, but the inconsistent behaviour is confusing. I'd expect either the first case to also downcast to object, or the second case to also throw a TypeError - but open to suggestions / discussion.

Thanks!

Expected Behavior

pd.Series([False, True], dtype=pd.BooleanDtype()).replace(False, 10).dtype
> dtype('O')

Installed Versions

INSTALLED VERSIONS

commit : 87cfe4e
python : 3.10.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 165 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United Kingdom.1252

pandas : 1.5.0
numpy : 1.23.1
pytz : 2022.1
dateutil : 2.8.2
setuptools : 63.4.1
pip : 22.2.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 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
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
xlwt : None
zstandard : None
tzdata : None

@bollard bollard added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 10, 2022
@phofl
Copy link
Member

phofl commented Oct 10, 2022

Hi, thanks for your report. This Is basically a duplicate of #47577

@phofl phofl closed this as completed Oct 10, 2022
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