Skip to content

df[col].dtype==object reverts back to df[col].dtype==datetime upon df.replace({np.nan: None}) #44498

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

Closed
rdmtinez opened this issue Nov 17, 2021 · 2 comments · Fixed by #45081
Closed
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions replace replace method
Milestone

Comments

@rdmtinez
Copy link

I explicitly coerce the dtype==datetime of a column containing NaT and datetimes (call it: DateTimeCol) to be of type object so that I can convert NaT to None (because Django complains about it). Then later during the workflow, when I have a need to replace all other NaN to None (because Django also compalins about it) using df.replace({np.nan: None}), the DateTimeCol is retransformed back into a type: datetime automatically without it explicit input and the NaT are present again which forces django to complain. Surely this is not a wanted behaviour.

`

pre replace

0 None
1 None
2 None
3 None
4 None
...
163 2013-12-19 16:30:00+01:00
164 2013-12-19 16:30:00+01:00
165 2013-12-19 16:30:00+01:00
166 2013-12-19 16:30:00+01:00
167 2013-12-19 16:30:00+01:00
Name: Tempering DateTime, Length: 168, dtype: object

df.replace({np.nan: None})

post replace

0 NaT
1 NaT
2 NaT
3 NaT
4 NaT
...
163 2013-12-19 16:30:00+01:00
164 2013-12-19 16:30:00+01:00
165 2013-12-19 16:30:00+01:00
166 2013-12-19 16:30:00+01:00
167 2013-12-19 16:30:00+01:00
Name: Tempering DateTime, Length: 168, dtype: datetime64[ns, Europe/Berlin]
`

@mroeschke
Copy link
Member

We would need a minimal, copy-pastable example: https://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports

@mroeschke mroeschke added the Needs Info Clarification about behavior needed to assess issue label Nov 21, 2021
@jbrockmendel jbrockmendel added the replace replace method label Dec 20, 2021
@jbrockmendel
Copy link
Member

I think the OP's issue is this:

import numpy as np
import pandas as pd

ser = pd.Series([None, None, pd.Timestamp.now()], dtype=object)
res = ser.replace({np.nan: None})  # <- would expect this to be a no-op, but it casts to dt64

assert res.dtype == object  # raises in master

@mroeschke mroeschke added Bug Dtype Conversions Unexpected or buggy dtype conversions and removed Needs Info Clarification about behavior needed to assess issue labels Dec 27, 2021
@jreback jreback added this to the 1.4 milestone Dec 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions replace replace method
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants