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: replace method with np.nan produces incorrect results #46306

Closed
2 of 3 tasks
zymon opened this issue Mar 10, 2022 · 5 comments · Fixed by #46335
Closed
2 of 3 tasks

BUG: replace method with np.nan produces incorrect results #46306

zymon opened this issue Mar 10, 2022 · 5 comments · Fixed by #46335
Labels
Bug Regression Functionality that used to work in a prior pandas version replace replace method
Milestone

Comments

@zymon
Copy link

zymon commented Mar 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

df = pd.DataFrame({'A': [0, 1, 2], 'B': [1, 0, 2]})
df.replace({0:1, 1:np.nan})

Issue Description

Pandas 1.4 changed the way the replace method works:

pandas 1.3.4:
df
A B
0 0 1
1 1 0
2 2 2
df.replace({0:1, 1:np.nan})
A B
0 1.0 NaN
1 NaN 1.0
2 2.0 2.0

pandas 1.4.1:
df
A B
0 0 1
1 1 0
2 2 2
df.replace({0:1, 1:np.nan})
A B
0 NaN NaN
1 NaN NaN
2 2.0 2.0

As you can see with the latest version first value 0 was replaced to 1 and then existing '1' and the new from replaced 0 were replaced to nan.
However, the same replacement using other values then np.nan works ok, for instance:
df.replace({0:1, 1:10})
A B
0 1 10
1 10 1
2 2 2

Expected Behavior

It should work the same way as in version 1.3.

Installed Versions

INSTALLED VERSIONS

commit : bb1f651
python : 3.9.1.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.14393
machine : AMD64
processor : Intel64 Family 6 Model 79 Stepping 0, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 1.4.0
numpy : 1.22.2
pytz : 2021.3
dateutil : 2.8.2
pip : 20.2.3
setuptools : 49.2.1
Cython : None
pytest : 7.0.0
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
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : None
zstandard : None

@zymon zymon added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 10, 2022
@zymon zymon changed the title BUG: BUG: replace method with np.nan produces incorrect results Mar 10, 2022
@lukemanley
Copy link
Member

lukemanley commented Mar 11, 2022

Looks like a regression. Confirmed that this behavior exists on the main branch.

@lukemanley lukemanley added the Regression Functionality that used to work in a prior pandas version label Mar 11, 2022
@lukemanley
Copy link
Member

It looks like the mask is not being passed here which seems to be causing the behavior:

# split so that we only upcast where necessary
return self.split_and_operate(
type(self).replace, to_replace, value, inplace=True
)

cc @jbrockmendel

@jbrockmendel
Copy link
Member

It looks like the mask is not being passed here which seems to be causing the behavior:

assuming this is correct, i think in order to pass the mask we'd need to use the splitting pattern we use at the end of Block.putmask instead of split_and_operate.

@lukemanley lukemanley removed the Needs Triage Issue that has not been reviewed by a pandas team member label Mar 12, 2022
@lukemanley
Copy link
Member

@jbrockmendel - thanks for the pointing that out. I used that pattern in the linked PR.

@lukemanley lukemanley added the replace replace method label Mar 12, 2022
@jreback jreback added this to the 1.4.2 milestone Mar 12, 2022
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Mar 12, 2022
@simonjayhawkins
Copy link
Member

first bad commit: [9cd1c6f] BUG: nullable dtypes not preserved in Series.replace (#44940)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Regression Functionality that used to work in a prior pandas version replace replace method
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants