Replacing Nat With 0 Days
I have a pandas dataframe that looks lie: A 3 days NaT 4 days Is there a way to replace the NaT with 0 days ? thanks, Ed
Solution 1:
Please note that the other answers are not up to date anymore. The preferred syntax is:
df['column'].fillna(pd.Timedelta(seconds=0))
The previously mentioned
df['column'].fillna(0)
will results in:
FutureWarning: Passing integers to fillna is deprecated, will raise a TypeError in a future version. To retain the old behavior, pass pd.Timedelta(seconds=n) instead. df['column'] = df['column'].fillna(0)
Solution 2:
Another way will be to use .replace
d = {'A': ['3 days', '4 days', 'NaT']}
df = pd.DataFrame(data=d)
df.replace({'NaT': '0 day'}, inplace=True)
Output:
A03 days
14 days
20 day
Solution 3:
When trying to implement manoj's replace-based answer with version 0.24.1, I found that I had to use "pandas.NaT" rather than just the string "Nat":
import pandas as pd
...
df.replace({pd.NaT: "0 days"}, inplace=True)
Solution 4:
df.A = pd.to_timedelta(df.A)
df.fillna(0)
A
0 3 days
1 0 days
2 4 days
Post a Comment for "Replacing Nat With 0 Days"