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

Time:07-31

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().

CodePudding user response:

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)
  • Related