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

How to compare two dates that are datetime64[ns] and choose the newest

I have a dataset and I want to compare to dates, both are datetime64[ns] if one is the newest I need to choose the other.

Here is my code:

df_analisis_invertido['Fecha de la primera conversion']=df_analisis_invertido.apply(lambda x: x['Fecha de creacion'] if df_analisis_invertido['Fecha de la primera conversion'] < df_analisis_invertido['Fecha de creacion'] else x['Fecha de la primera conversion'], axis=1)

this is the error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

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

>Solution :

The approach you chose is almost fine, except the comparison of the series objects. If you replace them with x instead of the df_analisis_invertido, it should work.

Here an example:

import pandas as pd

data = {'t_first_conv':  [5, 21, 233],
    't_creation': [3, 23, 234],
    }

df = pd.DataFrame(data)
df['t_first_conv'] = pd.to_datetime(df['t_first_conv'])
df['t_creation'] = pd.to_datetime(df['t_creation'])
print(df)

# Change entry of first conversion column in case it is older/smaller than creation value (timestamps)
# Expected: 
# 0: 5     3
# 1: 23    23
# 2: 234   234

df['t_first_conv']=df.apply(lambda x: x['t_creation'] if x['t_first_conv'] < x['t_creation'] else x['t_first_conv'], axis=1)

print(df)
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