Skip to content

BUG: Inconsistent behavior of .replace() in Int64 series with <NA>. #38267

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
tongr opened this issue Dec 3, 2020 · 5 comments · Fixed by #38693
Closed

BUG: Inconsistent behavior of .replace() in Int64 series with <NA>. #38267

tongr opened this issue Dec 3, 2020 · 5 comments · Fixed by #38693
Assignees
Labels
Bug good first issue NA - MaskedArrays Related to pd.NA and nullable extension arrays Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@tongr
Copy link

tongr commented Dec 3, 2020

Code Sample

This code does work as expected (0 value is replaced by <NA> in an Int64 series).

>>> import pandas as pd
>>> s = pd.Series([0, 1]).astype("Int64")
>>> s.replace(0, pd.NA)
0    <NA>
1       1
dtype: Int64

In contrast, when having <NA> values in the series .replace() does not work as expected (0 is not replaced by <NA>).

>>> import pandas as pd
>>> s = pd.Series([0, None]).astype("Int64")
>>> s.replace(0, pd.NA)
0       0
1    <NA>
dtype: object

However, the code does work if we add .fillna() before replacing:

>>> import pandas as pd
>>> s = pd.Series([0, None]).astype("Int64")
>>> s.fillna(0).replace(0, pd.NA)
0    <NA>
1    <NA>
dtype: Int64

Problem description

When applying the replace on a Int64 series with or without <NA> I expect consistent behavior.
Also the result type of .replace() seems to be inconsistent based on the usage of .fillna() on the series (without fillna() the type changes to object).

Expected behavior

I would expect that s.replace(0, pd.NA) and s.fillna(0).replace(0, pd.NA) on an Int64 series will always lead to the same result.

Output of pd.show_versions()

Details

INSTALLED VERSIONS

commit : 67a3d42
python : 3.8.0.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-53-generic
Version : #59~18.04.1-Ubuntu SMP Wed Oct 21 12:14:56 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.4
numpy : 1.19.2
pytz : 2020.4
dateutil : 2.8.1
pip : 20.3
setuptools : 50.3.2.post20201201
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
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@tongr tongr added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 3, 2020
@jorisvandenbossche
Copy link
Member

@tongr thanks for the report!

This seems to be working correctly on master (there have been some replace related fixes):

In [2]: >>> import pandas as pd
   ...: >>> s = pd.Series([0, None]).astype("Int64")
   ...: >>> s.replace(0, pd.NA)
Out[2]: 
0    <NA>
1    <NA>
dtype: Int64

In [3]: pd.__version__
Out[3]: '1.2.0.dev0+1441.g65f0463d36'

So this will be fixed in the upcoming 1.2 release

@jorisvandenbossche jorisvandenbossche added NA - MaskedArrays Related to pd.NA and nullable extension arrays and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 3, 2020
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Dec 14, 2020
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Dec 14, 2020
@simonjayhawkins
Copy link
Member

This seems to be working correctly on master (there have been some replace related fixes):

This was originally working in 1.0.5

fixed in [3967131] BUG: df.replace with numeric values and str to_replace (#36093)

@simonjayhawkins simonjayhawkins added good first issue Needs Tests Unit test(s) needed to prevent regressions labels Dec 14, 2020
@simonjayhawkins simonjayhawkins added this to the Contributions Welcome milestone Dec 14, 2020
@rkc007
Copy link
Contributor

rkc007 commented Dec 14, 2020

take

@rkc007
Copy link
Contributor

rkc007 commented Dec 14, 2020

@simonjayhawkins I would love to work on this issue. Could you please give a brief description that where I should begin with?

@simonjayhawkins
Copy link
Member

This issue just needs a test added to the test suite before closing. pandas\tests\series\methods\test_replace.py could be a good place to start. other tests (in that module) will give an idea of the style.

@jreback jreback modified the milestones: Contributions Welcome, 1.3 Dec 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug good first issue NA - MaskedArrays Related to pd.NA and nullable extension arrays Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants