0

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!

spiff
  • 1,335
  • 3
  • 11
  • 23
  • 1
    The datatype of `df['MaxSettDate']` and `onemonthdate` differs, thus the `TypeError`. Convert either one of the same type before doing the logical operation `>`. – Eduard Aug 05 '16 at 02:49
  • Indeed what I am trying to understand.. How do I do that? – spiff Aug 05 '16 at 02:56
  • 1
    Well, you either search for either 'Convert float to datetime python' http://stackoverflow.com/questions/6706231/fetching-datetime-from-float-and-vice-versa-in-python or 'Convert datetime to float python' which should match what the float format of pandas produces. – Eduard Aug 05 '16 at 03:01
  • perfect! thanks so much! – spiff Aug 05 '16 at 03:22

1 Answers1

2

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')]
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419