spiff spiff - 1 year ago 79
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:

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

here is my max function:



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 Source


you can convert your MaxSettDate column to datetime first:

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


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')]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download