Skip to content

BUG: Series.replace converts np.nan into pd.NaT implicitly #48034

Open
@ng-henry

Description

@ng-henry

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

s1 = pd.Series([pd.Timestamp(1000223), np.nan], dtype = object)

a1 = s1.replace("abcdef", pd.NaT, regex=True)
a2 = s1.replace("abcdef", pd.NaT, regex=False)

# Why?
assert a1[1] is pd.NaT # Here np.nan is implicitly converted to pd.NaT
assert a2[1] is np.nan # Here np.nan is NOT implicitly converted

Issue Description

np.nan are implicitly converted to pd.NaT when all doing:

  • Series.replace with regex = True flag
  • all values are either pd.Timestamp or np.nan

Expected Behavior

This behaviour is inconsistent. The documentation of Series.replace suggests that the function only replaces strings matching regex with the to_replace value. I did not expect for it to implicitly coerce values.

Expected behaviour is like the a2 case in the example: np.nan keeps being np.nan and does not get converted.

Where it happens

In L763-764 blocks.py, the block variable in L763 has np.nan. But after running block.convert(...), the result has pd.NaT. I believe the coercion happens specifically in L490 blocks.py:

attempt to coerce any object types to better types return a copy
of the block (if copy = True) by definition we are not an ObjectBlock
here!

Installed Versions

INSTALLED VERSIONS

commit : 4bfe3d0
python : 3.9.13.final.0
python-bits : 64
OS : Darwin
OS-release : 21.5.0
Version : Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:29 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T8101
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8

pandas : 1.4.2
numpy : 1.22.3
pytz : 2022.1
dateutil : 2.8.2
pip : 22.0.4
setuptools : 62.1.0
Cython : None
pytest : 7.1.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.0.3
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.9.3
jinja2 : 3.0.3
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : 0.8.1
fsspec : 2022.02.0
gcsfs : None
markupsafe : 2.1.1
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : 6.0.1
pyreadstat : None
pyxlsb : 1.0.9
s3fs : 0.4.2
scipy : None
snappy : None
sqlalchemy : 1.4.36
tables : None
tabulate : 0.8.9
xarray : None
xlrd : 2.0.1
xlwt : None
zstandard : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Dtype ConversionsUnexpected or buggy dtype conversionsMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolateRegressionFunctionality that used to work in a prior pandas versionreplacereplace method

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions