Adding a datetime.timedelta to a series of datetime64[ns, UTC] breaks when using the Series .add(offset) method, but works when doing series + offset. This behaviour seems unexpected to me.
Furthermore, adding a pd.Timedelta drops timezone.
import pandas as pd
import datetime
import pytz
datetime_series = pd.Series([datetime.datetime(2016, 5, 1, tzinfo = pytz.utc)])
offset = datetime.timedelta(days=1)
print datetime_series.dtype
>>> datetime64[ns, UTC]
# Fails
datetime_series.add(offset)
----------------------------------------------------
---------
TypeError Traceback (most recent call last)
<ipython-input-12-89cc70a4801f> in <module>()
8
9 # Fails
---> 10 datetime_series.add(offset)
11
12 # Works as expected
/home/adrian/.virtualenvs/pandas/local/lib/python2.7/site-packages/pandas/core/ops.pyc in flex_wrapper(self, other, level, fill_value, axis)
937 self = self.fillna(fill_value)
938
--> 939 return self._constructor(op(self.values, other),
940 self.index).__finalize__(self)
941
TypeError: ufunc add cannot use operands with types dtype('<M8[ns]') and `dtype('O')
Expected Output
# Works as expected
datetime_series + offset`
>>> 0 2016-05-02 00:00:00+00:00
>>> dtype: datetime64[ns, UTC]
Moreover, if the usages of offset
in all of the above code are replaced with pd.Timedelta(offset)
then the breaking line works but drops timezone. The line described as working does not drop timezone when replacing with pd.Timedelta(offset)
output of pd.show_versions()
INSTALLED VERSIONS
commit: None python: 2.7.6.final.0 python-bits: 64 OS: Linux OS-release: 3.19.0-66-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_GB.UTF-8
pandas: 0.18.1 nose: 1.3.7 pip: 8.1.2 setuptools: 26.0.0 Cython: None numpy: 1.9.0 scipy: 0.14.0 statsmodels: None xarray: None IPython: 5.0.0 sphinx: None patsy: None dateutil: 2.3 pytz: 2016.6.1 blosc: None bottleneck: None tables: None numexpr: None matplotlib: 1.4.3 openpyxl: None xlrd: None xlwt: None xlsxwriter: None lxml: 3.6.4 bs4: None html5lib: 0.999 httplib2: 0.9 apiclient: 1.1 sqlalchemy: None pymysql: None psycopg2: 2.6.1 (dt dec pq3 ext lo64) jinja2: 2.8 boto: 2.26.0 pandas_datareader: None
Comment From: jreback
This is fixed already in master / 0.19.0 (releasing soon). quite a number of bug fixes w.r.t. datetime w/tz arithmetic.
In [8]: datetime_series+offset
Out[8]:
0 2016-05-02 00:00:00+00:00
dtype: datetime64[ns, UTC]
In [9]: datetime_series.add(offset)
Out[9]:
0 2016-05-02 00:00:00+00:00
dtype: datetime64[ns, UTC]
In [10]: pd.__version__
Out[10]: '0.18.1+395.g5152cdd'
Comment From: grutts
Ah brilliant! I searched but didn't find the other issue when I made this one, sorry.