1

I have a data file with about 5.6million time-stamps in the format "2016-10-17 15:00:40.739". They are all strings at the moment for some reason and I need to convert them all to date times as I will later need to calculate the difference between groups of them (e.g: stamp1 -> stamp2 = 2hours, 4minutes etc).

I found another question "Converting string into datetime" but mine are in a different format and I cannot get that answer to work for me.

Any help is much appreciated.

c.timothy
  • 97
  • 10

3 Answers3

1

Use numpy's datetime64:

>>> np.datetime64('2016-10-17 15:00:40.739')
numpy.datetime64('2016-10-17T15:00:40.739')

You can easily find differences by simply subtracting, or using numpy's timedelta64:

>>> np.datetime64('2016-10-17 15:00:40.739') - np.datetime64('2016-10-15 15:00:40.739')
numpy.timedelta64(172800000,'ms')
>>> np.datetime64('2016-10-17 15:00:40.739') + np.timedelta64(1,'D')
numpy.datetime64('2016-10-18T15:00:40.739')
Tim
  • 2,756
  • 1
  • 15
  • 31
  • This works and is a really simple solution but I'm getting this as the output: 2016-10-17T15:00:40.739. Any reason there's a T in there? – c.timothy Feb 05 '18 at 16:04
  • You can convert to a `datetime` format with `from datetime import datetime; numpy.datetime64('2016-10-19T15:00:40.739').astype(datetime)` – Tim Feb 05 '18 at 16:19
1

Try this:

from datetime import datetime
a = "2016-10-17 15:00:40.739"
b = datetime.strptime(a,'%Y-%m-%d %H:%M:%S.%f')
print(b)
>>> datetime.datetime(2016, 10, 17, 15, 0, 40, 739000)

To define the format of your dates. Follow this guide: https://www.tutorialspoint.com/python/time_strptime.htm

studere
  • 156
  • 8
0

You can use the dateutil module to convert the string date to datetime object.

from dateutil import parser
dt =  parser.parse("2016-10-17 15:00:40.739")
print dt
print type(dt)

Output:

2016-10-17 15:00:40.739000
<type 'datetime.datetime'>
Rakesh
  • 81,458
  • 17
  • 76
  • 113