Adding a couple milliseconds to datetime
fmt = '%Y-%m-%d %H:%M:%S %z'
datelist = ['2017-02-24 18:21:49 -0800', '2017-02-24 18:21:49 -0800', '2017-02-24 18:21:49 -0700']
dateseries = pd.Series(datelist)
tmptime = dateseries .apply(datetime.strptime, args=(fmt,))
since different time zones, tmptime won't be converted to dtype datetime64 and instead stay as datetime.datetime
addms = pd.Series([1, 2, 3])
addoffset = addms * timedelta(microseconds = 10000) # series of dtype timedelta64
tmptime + addoffset
# TypeError: ufunc add cannot use operands with types dtype('O') and dtype('<m8[ns]')
Problem description
I want to add a couple milliseconds to a datetime object in pandas. The datatime object is not automatically converted into datetime64 when holding different time zones (and to_datetime() doesn't understand z in the fmt string). This creates the case where one pandas series is of type datetime while the time difference is of type timedelta64. Addition is not possible between these two types. It also does not seem possible to have pandas hold a timedelta (automatically converted to timedelta64). A workaround would be to add milliseconds in timedelta that is in a list (below)
Expected Output
addoffset2 = [i * timedelta(microseconds = 10000) for i in [1,2,3]] # list of type datetime.timedelta
tmptime + addoffset2
output
0 2017-02-24 18:21:49.010000-08:00
1 2017-02-24 18:21:49.020000-08:00
2 2017-02-24 18:21:49.030000-07:00
dtype: object
Output of pd.show_versions()
Comment From: jreback
Mixing timezones within a single Series is not performant and not supported. You are best off converting to a single timezone. (either UTC or a local) and simply adding.
In [40]: pd.to_datetime(datelist)
Out[40]: DatetimeIndex(['2017-02-25 02:21:49', '2017-02-25 02:21:49', '2017-02-25 01:21:49'], dtype='datetime64[ns]', freq=None)
In [41]: Series(pd.to_datetime(datelist).tz_localize('UTC').tz_convert('US/Pacific'))
Out[41]:
0 2017-02-24 18:21:49-08:00
1 2017-02-24 18:21:49-08:00
2 2017-02-24 17:21:49-08:00
dtype: datetime64[ns, US/Pacific]
In [42]: Series(pd.to_datetime(datelist).tz_localize('UTC').tz_convert('US/Pacific')) + pd.to_timedelta([1, 2, 3], unit='ms')
Out[42]:
0 2017-02-24 18:21:49.001000-08:00
1 2017-02-24 18:21:49.002000-08:00
2 2017-02-24 17:21:49.003000-08:00
dtype: datetime64[ns, US/Pacific]