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(to_replace=pd.NaT, value=None) different from replace({pd.NaT: None}) #60798

Closed
3 tasks done
thunderbug1 opened this issue Jan 27, 2025 · 1 comment
Closed
3 tasks done
Labels
Bug Duplicate Report Duplicate issue or pull request replace replace method

Comments

@thunderbug1
Copy link

thunderbug1 commented Jan 27, 2025

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

df = pd.read_pickle("example.pkl")

df
                        ts               Start                 End
23 2025-01-27 09:49:44.045 2025-01-27 09:49:44                 NaT
28 2025-01-27 06:50:56.046 2025-01-27 06:50:54 2025-01-27 06:50:56


df.replace(to_replace=pd.NaT, value=None)
                        ts               Start                 End
23 2025-01-27 09:49:44.045 2025-01-27 09:49:44                 NaT
28 2025-01-27 06:50:56.046 2025-01-27 06:50:54 2025-01-27 06:50:56

df.replace({pd.NaT: None})
                            ts                Start                  End
23  2025-01-27 09:49:44.045000  2025-01-27 09:49:44                 None
28  2025-01-27 06:50:56.046000  2025-01-27 06:50:54  2025-01-27 06:50:56

df.replace(to_replace=pd.NaT, value=None).dtypes
ts       datetime64[ns]
Start    datetime64[ns]
End      datetime64[ns]
dtype: object

df.replace({pd.NaT: None}).dtypes
ts       object
Start    object
End      object
dtype: object

Issue Description

In my application I read data from a database via asyncpg and then process it with pandas.
Recently I encountered an issue where the replace command changes the datatypes of unrelated columns if I use it with a dictionary argument.
Using replace with the arguments "to_replace" and "value", however, works.

Somehow my dataframe is weird, I was not able to create a pure code example to reproduce this and only saving and loading my dataframe as a pickle file made it reproducible. However, due to GitHub limitations I cannot share the pickle file here which is probably due to pickle files being unsafe to unpickle from untrusted sources.
I did try to recreate the issue in code and also using other file formats but that somhow seems to loose important metadata that causes the issue.

This is the metadata of the dataframe as it shows in the VS-code debugger:
Image

Expected Behavior

I would expect that these two results are the same:

df.replace(to_replace=pd.NaT, value=None).dtypes
df.replace({pd.NaT: None}).dtypes

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.12.5
python-bits : 64
OS : Linux
OS-release : 5.15.167.4-microsoft-standard-WSL2
Version : #1 SMP Tue Nov 5 00:21:55 UTC 2024
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : C.UTF-8

pandas : 2.2.3
numpy : 2.2.0
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 24.2
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : 8.3.4
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.14.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None

@thunderbug1 thunderbug1 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 27, 2025
@rhshadrach
Copy link
Member

Thanks for the report. Closing as a duplicate of #60284 and #53539.

@rhshadrach rhshadrach added Duplicate Report Duplicate issue or pull request replace replace method and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Duplicate Report Duplicate issue or pull request replace replace method
Projects
None yet
Development

No branches or pull requests

2 participants