0

I am using Python and I have dates in the table of the type '2013-06-24 17:04:28' stored as strings and I want to do things such as subtract and average them.

I dont care much about the time.

What is the appropriate format to convert the string to and how do I do it?

Thanks in advance for any help.

Max.

user7289
  • 32,560
  • 28
  • 71
  • 88

2 Answers2

4

pandas has it's own Timestamp object. You can convert strings, or entire columns (Series), to Timestamps using the to_datetime function:

pd.to_datetime(df['date_col'])

See "converting to Timestamps" section of the docs.

Note: you can convert to Timestamps when reading in your DataFrame e.g. using read_csv.

It works with your date format:

In [11]: pd.to_datetime('2013-06-24 17:04:28')
Out[11]: Timestamp('2013-06-24 17:04:28', tz=None)
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
0
time.strptime("2013-06-24 17:04:28", "%Y-%m-%d %H:%M:%S")  

then you can use python's time module

jongusmoe
  • 676
  • 4
  • 16