0

Blame it on my bad design. I have the column datatype of datetime as text. And now, I need to compare (<) 2 datetime values if one is greater than the other. Getting the exception as "not all arguments converted during string formatting"

The values are in "YYYY-MM-DD HH:MM:SS" format

Harsha
  • 323
  • 1
  • 17

2 Answers2

0

A solution is to use pandas.to_datetime to get the Timestamp object from the string for which a comparison is much easier. Example:

import pandas as pd

date1 = pd.to_datetime("1991-01-20 05:00:00")
date2 = pd.to_datetime("2005-10-01 15:12:00")
print(date1 < date2) # True
b-fg
  • 3,959
  • 2
  • 28
  • 44
0

You can use time.mktime(parse(ts).timetuple()) where 'ts' is your timestamp string.

Ex:

>>> ts1 = datetime.datetime.now().strftime("%Y-%M-%d %I:%M:%S")
>>> ts2 = datetime.datetime.now().strftime("%Y-%M-%d %I:%M:%S")
>>>
>>> ts1
'2018-02-21 03:02:58'
>>> ts2
'2018-03-21 03:03:11'
>>>
>>> time.mktime(parse(ts1).timetuple())
1519162378.0
>>>
>>> time.mktime(parse(ts2).timetuple())
1521581591.0
Akhilesha
  • 21
  • 4