0

this works -> df = df.replace(0, np.nan)

this does not -> df = df.replace(0, pd.NA)

It works fine in Jupyter notebooks, just not when run as a .py file

Sample code:

import numpy as np
import pandas as pd

df1 = pd.DataFrame({0, 0, 0, 0})
df2 = pd.DataFrame({0, 0, 0, 0})

df1.replace(0, np.nan, inplace=True)
df2.replace(0, pd.NA, inplace=True)

print(df1)
print(df2)

This is the end of the error message when thrown:

File site-packages\pandas\core\dtypes\base.py", line 312, in is_dtype
    if isinstance(dtype, (ABCSeries, ABCIndex, ABCDataFrame, np.dtype)):
  File "lib\site-packages\pandas\core\dtypes\generic.py", line 47, in _instancecheck
    return _check(inst) and not isinstance(inst, type)
  File "lib\site-packages\pandas\core\dtypes\generic.py", line 41, in _check
    return getattr(inst, attr, "_typ") in comp
RecursionError: maximum recursion depth exceeded while calling a Python object

Pandas version - 1.5.1
Numpy version - 1.23.4
Python version - 3.10

Pkmstr
  • 29
  • 4

0 Answers0