Skip to content

BUG: RecursionError when attempting to replace np.nan values under main branch #45725

@roib20

Description

@roib20

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


NaN_values = [np.nan, np.nan]
ser = pd.Series(data=NaN_values)
df = pd.DataFrame(data=NaN_values)


try:
    ser = ser.replace({np.nan: pd.NA})
except RecursionError:
    print("RecursionError: maximum recursion depth exceeded while calling a Python object")

try:
    df = df.replace({np.nan: None})
except RecursionError:
    print("RecursionError: maximum recursion depth exceeded while calling a Python object")

Issue Description

Note: this bug is not present in 1.4.x branch, but it is in the main branch and 1.5 milestone.

Let's say we have a Series or DataFrame containing numpy.nan values (a common scenario), and we want to convert them all to Panda's own type pandas.NA, or alternatively to the native Python None type. One way to do this conversion is by using the replace method as shown in the example. From my testing this works in pandas 1.4.0 as well as 1.3.5, but not in the main branch. A RecursionError exception is thrown.

Suggested tags: 1.5 Milestone, Regression, replace, Dtype Conversions

Expected Behavior

The replace method should successfully replace np.nan with the desired value, without errors. This behavior works in pandas 1.4.0 and previous versions and should be fixed for the pandas 1.5 release.

Installed Versions

commit : f77480f

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

pandas : 1.5.0.dev0+234.gf77480f9f5
numpy : 1.22.1
pytz : 2021.3
dateutil : 2.8.2
pip : 21.1.2
setuptools : 57.0.0
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
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
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugRegressionFunctionality that used to work in a prior pandas versionreplacereplace method

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions