0

Basically I have an entire dataframe with the timestamp in the following format: 2018-01-17T05:00:00.000000Z

And I'm looking to add different seconds on it (sometimes add 1 second, sometimes add 1 microsecond, etc).

hiimarksman
  • 261
  • 2
  • 3
  • 12

2 Answers2

0

The python datetime allows you to use milliseconds and microseconds.

>>> from datetime import datetime,timedelta
>>> dt = datetime.now()
>>> print(dt)
2019-07-05 17:21:49.523664
>>> dt1 = dt + timedelta(microseconds = 1,milliseconds = 1)
>>> print(dt1)
2019-07-05 17:21:49.524665

Regarding the nanoseconds you can find information here.

In case you have it as a string you have to transform it into datetime:

>>> from datetime import datetime,timedelta
>>> import dateutil.parser
>>> date = dateutil.parser.parse("2018-01-17T05:00:00.000000Z")
>>> print(date)
2018-01-17 05:00:00+00:00
>>> dt1 = date + timedelta(microseconds = 1,milliseconds = 1)
>>> print(dt1)
2018-01-17 05:00:00.001001+00:00

If you ask for the last part of the date +00:00, it is for the time zone you can remove it like this:

>>> dt1 = dt1.replace(tzinfo=None)
>>> print(dt1)
2018-01-17 05:00:00.001001
0

With Python 3.7 you may use datetime.fromisoformat:

import datetime
value = datetime.datetime.fromisoformat(str)
value += datetime.timedelta(seconds=1)

With older Python version you may use:

import datetime    
value = datetime.datetime.strptime(str, "%Y-%m-%dT%H:%M:%S.%fZ")
value += datetime.timedelta(seconds=1)
ipaleka
  • 3,745
  • 2
  • 13
  • 33