Skip to content

Reindex on nearest datetime gives UFuncTypeError - subtract with dtype('<M8[ns]') and dtype('O') #31964

Closed
@andrewcooke

Description

@andrewcooke

Code Sample

import pandas as pd
print(pd.__version__)
i1 = pd.DatetimeIndex(['2016-06-26 14:27:26+00:00'])
i2 = pd.DatetimeIndex(['2016-07-04 14:00:59+00:00'])
f2 = pd.DataFrame(index=i2)
f2.reindex(i1, method='nearest')

Problem description

Running the above code gives:

Python 3.7.4 (default, Jul 27 2019, 21:25:02) 
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> print(pd.__version__)
1.0.1
>>> i1 = pd.DatetimeIndex(['2016-06-26 14:27:26+00:00'])
>>> i2 = pd.DatetimeIndex(['2016-07-04 14:00:59+00:00'])
>>> f2 = pd.DataFrame(index=i2)
>>> f2.reindex(i1, method='nearest')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/andrew/project/ch2/choochoo/py/env/lib/python3.7/site-packages/pandas/util/_decorators.py", line 227, in wrapper
    return func(*args, **kwargs)
  File "/home/andrew/project/ch2/choochoo/py/env/lib/python3.7/site-packages/pandas/core/frame.py", line 3856, in reindex
    return self._ensure_type(super().reindex(**kwargs))
  File "/home/andrew/project/ch2/choochoo/py/env/lib/python3.7/site-packages/pandas/core/generic.py", line 4544, in reindex
    axes, level, limit, tolerance, method, fill_value, copy
  File "/home/andrew/project/ch2/choochoo/py/env/lib/python3.7/site-packages/pandas/core/frame.py", line 3744, in _reindex_axes
    index, method, copy, level, fill_value, limit, tolerance
  File "/home/andrew/project/ch2/choochoo/py/env/lib/python3.7/site-packages/pandas/core/frame.py", line 3760, in _reindex_index
    new_index, method=method, level=level, limit=limit, tolerance=tolerance
  File "/home/andrew/project/ch2/choochoo/py/env/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 3145, in reindex
    target, method=method, limit=limit, tolerance=tolerance
  File "/home/andrew/project/ch2/choochoo/py/env/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 2740, in get_indexer
    indexer = self._get_nearest_indexer(target, limit, tolerance)
  File "/home/andrew/project/ch2/choochoo/py/env/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 2821, in _get_nearest_indexer
    left_distances = abs(self.values[left_indexer] - target)
numpy.core._exceptions.UFuncTypeError: ufunc 'subtract' cannot use operands with types dtype('<M8[ns]') and dtype('O')

This is a problem because it occurs in a larger dataset that I need to reindex. It used to work fine with earlier versions of Pandas (although I do not know which, sorry).

Expected Output

Expected output would be something like

>>> f2.reindex(i1)
Empty DataFrame
Columns: []
Index: [2016-06-26 14:27:26+00:00]

Output of pd.show_versions()

>>> pd.show_versions()

INSTALLED VERSIONS
------------------
commit           : None
python           : 3.7.4.final.0
python-bits      : 64
OS               : Linux
OS-release       : 4.12.14-lp151.28.36-default
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : None
LANG             : en_GB.UTF-8
LOCALE           : en_GB.UTF-8

pandas           : 1.0.1
numpy            : 1.18.1
pytz             : 2019.3
dateutil         : 2.8.1
pip              : 20.0.2
setuptools       : 40.8.0
Cython           : None
pytest           : None
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : 2.11.1
IPython          : 7.12.0
pandas_datareader: None
bs4              : None
bottleneck       : None
fastparquet      : None
gcsfs            : None
lxml.etree       : None
matplotlib       : 3.1.3
numexpr          : None
odfpy            : None
openpyxl         : 3.0.3
pandas_gbq       : None
pyarrow          : None
pytables         : None
pytest           : None
pyxlsb           : None
s3fs             : None
scipy            : 1.4.1
sqlalchemy       : 1.3.13
tables           : None
tabulate         : None
xarray           : None
xlrd             : None
xlwt             : None
xlsxwriter       : None
numba            : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    RegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions