Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dataframe fillna() for datetime based on another column

I’m trying to use .fillna to replace NaT data from another column, follows example:

id data1 data2
1 2021-02-12 16:58:38.570 2021-02-12 17:30:55.000
2 NaT 2021-04-20 14:32:46.000

Expected:

id data1 data2
1 2021-02-12 16:58:38.570 2021-02-12 17:30:55.000
2 2021-04-20 14:32:46.000 2021-04-20 14:32:46.000

I tried

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df['data1'].fillna(df['data2'], inplace=True) 

and nothing

Also tried

df['data1'] = df_final['data1'].replace('nan', np.nan).fillna(df['data2'], inplace=True) 

but converted all of the data1 column to None

Any clues?

>Solution :

It seems it’s not actually NaT but a string 'NaT'. So you could replace it with NaN, then could try bfill on axis:

df = df.replace('NaT', pd.NA).bfill(axis=1)

Output:

  id                    data1                    data2
0  1  2021-02-12 16:58:38.570  2021-02-12 17:30:55.000
1  2  2021-04-20 14:32:46.000  2021-04-20 14:32:46.000
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading