-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
Inconsistent behavior for df.replace() with NaN, NaT and None #29024
Comments
Most of this is caused by First of all, this function does not differentiate between NaN and NaT, which explains your first and second result. The other issue is the switching between NaN and None in the "Value" column when calling replace multiple times. For this we have to consider in more detail how pandas actually replaces values: pandas first splits the DataFrame into multiple blocks, and then replaces the values in each block. The block type depends on the data type. In the above example, the DataFrame is split into 3 blocks: "Name" becomes an This means that on first replacement, as in your example 1 and 2, the "Value" column will contain Your last example is basically the same, as the replacements are performed sequentially. I'm unsure what the best way to fix this would be, but maybe this helps someone who wants to try. |
I've been having similar issues with counter-intuitive handling of NaT and NaN values when dealing with the DataFrame.replace() method. Has this issue been worked on at all or is it still open? |
Thanks a lot, bro. It's so valuable information |
Any fix on this issue ? I still encounter this error on pandas 1.1.3 |
From my limited testing it seems this specific issue was fixed in pandas 1.2.5 but has resurfaced again in 1.3.3 |
Can conform, this is again broken in 1.3.3. See small example below: import pandas as pd
import numpy as np
import datetime as dt
df = pd.DataFrame({'X': [1, 2, 3, 4, 5],
'A': ["2021-01-01", "2021-01-02", "2021-01-03", "", "2021-01-05"],
'B': [np.NaN, 6, 7, 8, np.NaN],
'C': ['a', 'b', 'c', 'd', 'e']})
df = df.astype({'A': 'datetime64[ns]'})
Replace Show output from `pd.show_versions()`:
|
this issue was not closed as it needs thorough tests and likely a patch - anyone in the community can push a PR and the core team can review |
I've also been banging my face against a keyboard because of this bug. For those of you looking for a quick and easy workaround to making sure NaNs and NaTs are out of your dataframes, I've found replacing NaT first then NaN the way to go: # Note that the order here matters!
df = df.replace({pd.NaT: None}).replace({np.NaN: None}) As a good practice, I've also been dropping a comment above this line linking to this issue to make sure other engineers don't get tripped up on this down the road (at least until this issue is resolved). |
Thanks stevenleeg, you inspired my own workaround. For some reason, your approach worked for me for NaN values but not NaT, which is what I was more concerned with. So instead, I replaced all NaN values with 0 and then replaced with None. df1 = df1.replace(np.NaN, 0).replace(0, None) |
Hi, based on @stevenleeg solution this worked for me in order to remove NaT values. Thank you!!
|
Code Sample, a copy-pastable example if possible
Problem description
This might seem somewhat related to #17494. Here I am using a dict to replace (which is the recommended way to do it in the related issue) but I suspect the function calls itself and passes
None
(replacement value) to thevalue
arg, hitting the default arg value.When calling
df.replace()
to replace NaN or NaT with None, I found several behaviours which don't seem right to me :This is a problem because I'm unable to replace only NaT or only NaN. This is also a problem because if I want to replace both, I intuitively call replace with the dict
{pd.NaT: None, np.NaN: None}
but end up with NaNs.I suspect two problems here : NaN, NaT and None being all considered as equals, and replace() calling itself with None as value argument.
Expected Output
Output of
pd.show_versions()
pandas: 0.24.2
pytest: None
pip: 19.2.2
setuptools: 41.0.1
Cython: None
numpy: 1.16.4
scipy: None
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.7.5
pytz: 2018.7
blosc: None
bottleneck: None
tables: 3.5.1
numexpr: 2.7.0
feather: None
matplotlib: None
openpyxl: 2.6.2
xlrd: 1.2.0
xlwt: 1.3.0
xlsxwriter: 1.1.8
lxml.etree: 4.2.5
bs4: None
html5lib: 1.0.1
sqlalchemy: 1.2.14
pymysql: None
psycopg2: 2.8.3 (dt dec pq3 ext lo64)
jinja2: 2.10.1
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None
The text was updated successfully, but these errors were encountered: