Skip to content

BUG:: Series of timedelta + TimedeltaIndex gets casted to int64 #17250

Closed
@azjps

Description

@azjps

Code Sample, a copy-pastable example if possible

In [1]: import pandas as pd, numpy as np

In [2]: df = pd.DataFrame({'td': [np.timedelta64(0, 'ns')]})

In [3]: tds = pd.TimedeltaIndex(['0 days'], dtype='timedelta64[ns]')

In [4]: df.td + tds  # unexpected, returns dtype=int64!
Out[4]: 
0    0
Name: td, dtype: int64

In [5]: df.td + tds._data  # expected, returns dtype=timedelta64
Out[5]: 
0   0 days
Name: td, dtype: timedelta64[ns]

In [6]: df.td + tds.values
Out[6]: 
0   0 days
Name: td, dtype: timedelta64[ns]

In [7]: df["td"].dt.values + tds
Out[7]: TimedeltaIndex(['0 days'], dtype='timedelta64[ns]', freq=None)

Problem description

When adding a TimedeltaIndex and a Series with dtype=timedelta64, one would expect the output to have dtype=timedelta64, but instead it gets promoted to dtype=int64. This is easy to work around by calling .astype("timedelta64[ns]") or using the underlying numpy TimedeltaIndex.values and hence pretty minor, but I figured I'd report it. I believe this regression was introduced around 0.18; the above code works as expected in 0.15.2.

Output of pd.show_versions()

```python In [14]: pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 2.7.6.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.75-el6.x86_64.lime.1
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.utf8
LOCALE: None.None

pandas: 0.20.3
pytest: None
pip: 7.1.0
setuptools: 19.4
Cython: None
numpy: 1.13.1
scipy: None
xarray: None
IPython: 5.1.0
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
pandas_gbq: None
pandas_datareader: None

</details>

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions