Skip to content

regression in error message for DatetimeIndex subtraction #25497

Closed
@simonjayhawkins

Description

@simonjayhawkins

Code Sample, a copy-pastable example if possible

import pandas as pd
td = pd.Timedelta('1 days')
dti = pd.date_range('20130101', periods=3, name='bar')
td - dti

Problem description

0.25.0.dev0+179.g0a61ecdf6

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\simon\OneDrive\code\pandas-simonjayhawkins\pandas\core\indexes\datetimelike.py", line 507, in __rsub__
    result = self._data.__rsub__(maybe_unwrap_index(other))
  File "C:\Users\simon\OneDrive\code\pandas-simonjayhawkins\pandas\core\arrays\datetimelike.py", line 1325, in __rsub__
    return -(self - other)
TypeError: bad operand type for unary -: 'DatetimeArray'

Expected Output

same as 0.23.4

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-2a4f19a48f3e> in <module>
      3 td = pd.Timedelta('1 days')
      4 dti = pd.date_range('20130101', periods=3, name='bar')
----> 5 td - dti

~\Anaconda3\lib\site-packages\pandas\core\indexes\datetimelike.py in __rsub__(self, other)
    949                 from pandas import DatetimeIndex
    950                 return DatetimeIndex(other) - self
--> 951             return -(self - other)
    952         cls.__rsub__ = __rsub__
    953 

~\Anaconda3\lib\site-packages\pandas\core\ops.py in invalid_op(self, other)
    174     def invalid_op(self, other=None):
    175         raise TypeError("cannot perform {name} with this index type: "
--> 176                         "{typ}".format(name=name, typ=type(self).__name__))
    177 
    178     invalid_op.__name__ = name

TypeError: cannot perform __neg__ with this index type: DatetimeIndex

Output of pd.show_versions()

INSTALLED VERSIONS

commit: 0a61ecd
python: 3.7.2.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: None.None

pandas: 0.25.0.dev0+179.g0a61ecdf6
pytest: 4.2.0
pip: 19.0.1
setuptools: 40.8.0
Cython: 0.29.5
numpy: 1.15.4
scipy: 1.2.1
pyarrow: 0.11.1
xarray: 0.11.3
IPython: 7.2.0
sphinx: 1.8.4
patsy: 0.5.1
dateutil: 2.7.5
pytz: 2018.9
blosc: None
bottleneck: 1.2.1
tables: 3.4.4
numexpr: 2.6.9
feather: None
matplotlib: 3.0.2
openpyxl: 2.6.0
xlrd: 1.2.0
xlwt: 1.3.0
xlsxwriter: 1.1.2
lxml.etree: 4.3.1
bs4: 4.7.1
html5lib: 1.0.1
sqlalchemy: 1.2.18
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: 0.2.0
fastparquet: 0.2.1
pandas_gbq: None
pandas_datareader: None
gcsfs: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDatetimeDatetime data dtypeError ReportingIncorrect or improved errors from pandasNumeric OperationsArithmetic, Comparison, and Logical operationsTimedeltaTimedelta data type

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions