-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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: pd.Series.replace(to_replace=...) does not preserve the original dtype of the series #33484
Closed
3 tasks done
Labels
Bug
Dtype Conversions
Unexpected or buggy dtype conversions
ExtensionArray
Extending pandas with custom dtypes or arrays.
replace
replace method
Milestone
Comments
chrispe
added
Bug
Needs Triage
Issue that has not been reviewed by a pandas team member
labels
Apr 11, 2020
chrispe
changed the title
BUG: pd.Series.replace(to_replace=...) does not preserve the original dtype of the series
BUG: pd.Series.replace(to_replace=...) does not preserve the original dtype of the series (pandas-dev#33484)
Apr 11, 2020
chrispe
changed the title
BUG: pd.Series.replace(to_replace=...) does not preserve the original dtype of the series (pandas-dev#33484)
BUG: pd.Series.replace(to_replace=...) does not preserve the original dtype of the series
Apr 11, 2020
4 tasks
I think that this is not only limited to string dtype: [ins] In [7]: s
Out[7]:
0 1
1 2
dtype: Int64
[ins] In [8]: s.replace({1: 3})
Out[8]:
0 3
1 2
dtype: int64 [ins] In [12]: s
Out[12]:
0 True
1 False
dtype: boolean
[ins] In [13]: s.replace({True: False})
Out[13]:
0 False
1 False
dtype: object Also related: #32988 |
dsaxton
added
Dtype Conversions
Unexpected or buggy dtype conversions
and removed
Needs Triage
Issue that has not been reviewed by a pandas team member
labels
Apr 11, 2020
Added this to the description, so it should be more clear now. Thanks for the feedback @dsaxton. |
mroeschke
added
the
ExtensionArray
Extending pandas with custom dtypes or arrays.
label
Apr 11, 2020
5 tasks
#32988 is a regression from 0.25.3 whereas this issue was already present in 0.25.3 for Int64 type
|
I think I have the same issue: import pandas as pd
import numpy as np
pd.__version__ #1.1.2
s = pd.Series(['abc', None, pd.NA, 'def'], dtype="string")
print(s.dtype) # string
t = pd.Series([None], dtype="string")
print(t.dtype) # string
u = pd.Series([pd.NA], dtype="string")
print(u.dtype) # string
v = pd.Series(['abc', None, pd.NA], dtype="string")
print(v.dtype) # string
w = v.replace('abc', pd.NA)
print(w.dtype) # Now of type object, why not string?
# In Python 1.0.3, it was string |
Closed
5 tasks
8 tasks
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
ExtensionArray
Extending pandas with custom dtypes or arrays.
replace
replace method
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
(optional) I have confirmed this bug exists on the master branch of pandas.
Code Sample, a copy-pastable example
Problem description
The
pandas.Series.replace
does not return a series with the samedtype
as the the original series. This behaviour is produced only when the replace function is called using the argumentto_replace
.Notice: Similar behaviour is produced with other dtypes as well, this issue is not limited to string dtype.
The investigation should probably start here:
/pandas/core/generic.py
Expected Output
We would expect the
pandas.Series.replace(to_replace=...)
to preserve the dtype of the series.In the above code sample, both of the series should have a dtype of string.
Output of
pd.show_versions()
INSTALLED VERSIONS
commit : None python : 3.8.2.final.0 python-bits : 64 OS : Darwin OS-release : 19.4.0 machine : x86_64 processor : i386 byteorder : little LC_ALL : None LANG : None LOCALE : None.UTF-8pandas : 1.0.3
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.1.3.post20200330
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
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None
The text was updated successfully, but these errors were encountered: