spiff spiff - 4 months ago 11
Python Question

max of two dates comes in a totally different format

I have 2 columns with dates in them, i want to create a 3rd column with the max of these 2 dates:

df['xxx_MaxSettDate'][0]
Out[186]: Timestamp('2017-01-20 00:00:00')
df['yyy_MaxSettDate'][0]
Out[166]: NaT


here is my max function:

df['MaxSettDate']=df[['xxx_MaxSettDate','yyy_MaxSettDate']].max(axis=1)


output:

df['MaxSettDate'][0]
Out[187]: 1.4848704e+18


I want to be able to do operation on this date, such as remove all dates which are lower than 1m

so I do this:

onemonthdate = date.today() + timedelta(30)
df = df[(df['MaxSettDate']>onemonthdate)]


This results in the error:

TypeError: unorderable types: float() > datetime.date()


Thoughts on how I could achieve this pls? I am geting very confused over all the solutions provided.. you could also just point me to something which I could read and understand the whole dates paradigm in python better.. thanks vm!

Answer

UPDATE:

you can convert your MaxSettDate column to datetime first:

df['MaxSettDate'] = pd.to_datetime(df['MaxSettDate'])

Demo:

In [41]: pd.to_datetime(1.4848704e+18)
Out[41]: Timestamp('2017-01-20 00:00:00')

OLD amswer:

I would use pandas Timedelta for that:

df = df[df['MaxSettDate'] > pd.datetime.now() + pd.Timedelta('30 days')]