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: pd.Series.replace(to_replace=...) does not preserve the original dtype of the series #33484

Closed
3 tasks done
chrispe opened this issue Apr 11, 2020 · 4 comments · Fixed by #44940
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
Copy link
Contributor

chrispe commented Apr 11, 2020

  • 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

>>> s = pd.Series(["one", "two"], dtype="string")
>>> expected = pd.Series(["1", "2"], dtype="string")
>>> result = s.replace(to_replace={"one": "1", "two": "2"})
>>> expected
0    1
1    2
dtype: string
>>> result
0    1
1    2
dtype: object

Problem description

The pandas.Series.replace does not return a series with the same dtype as the the original series. This behaviour is produced only when the replace function is called using the argument to_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-8

pandas : 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

@chrispe chrispe added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 11, 2020
@chrispe 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 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
@dsaxton
Copy link
Member

dsaxton commented Apr 11, 2020

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 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
@chrispe
Copy link
Contributor Author

chrispe commented Apr 11, 2020

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

Added this to the description, so it should be more clear now. Thanks for the feedback @dsaxton.

@mroeschke mroeschke added the ExtensionArray Extending pandas with custom dtypes or arrays. label Apr 11, 2020
@simonjayhawkins
Copy link
Member

Also related: #32988

#32988 is a regression from 0.25.3 whereas this issue was already present in 0.25.3 for Int64 type

>>> import pandas as pd
>>>
>>> pd.__version__
'0.25.3'
>>>
>>> s = pd.Series([1, 2], dtype="Int64")
>>> s
0    1
1    2
dtype: Int64
>>>
>>> s.replace(to_replace={1: 3})
0    3
1    2
dtype: int64
>>>

@jbrockmendel jbrockmendel added the replace replace method label Sep 17, 2020
@MarcusJellinghaus
Copy link

MarcusJellinghaus commented Sep 24, 2020

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

@simonjayhawkins simonjayhawkins added this to the Contributions Welcome milestone Sep 25, 2020
@jreback jreback modified the milestones: Contributions Welcome, 1.4 Dec 17, 2021
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
Projects
None yet
7 participants