Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
andrewcooke opened this issue Feb 13, 2020 · 6 comments · Fixed by #32155
Labels
Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@andrewcooke
Copy link

andrewcooke commented Feb 13, 2020

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
@andrewcooke
Copy link
Author

I just checked and the same error was present in 1.0.0 but not in 0.25.3 (which instead gives a warning about TZ related issues that I think is not directly relevant here?).

@MarcoGorelli MarcoGorelli added the Regression Functionality that used to work in a prior pandas version label Feb 14, 2020
@jorisvandenbossche jorisvandenbossche added this to the 1.0.2 milestone Feb 14, 2020
@jorisvandenbossche
Copy link
Member

This doesn't raise an error anymore on master. But didn't check the 1.0.x branch yet. So we should check that, or otherwise find the commit on master that fixed it (and backport that), and add a test for it.

@TomAugspurger
Copy link
Contributor

Is this fixed by #31511? That wasn't backported to 1.0.x.

@simonjayhawkins
Copy link
Member

yes backporting #31511 will fix this. do we need to add a test?

@andrewcooke
Copy link
Author

Thanks, so this will be in 1.0.2 I gather? :)

@mschroen
Copy link

mschroen commented Feb 25, 2020

yes backporting #31511 will fix this. do we need to add a test?

Can someone please tell how to "backport #31511" with pip? This reindex bug breaks all my workflows and unless 1.0.2 is not online I'd be happy to install a "backported version".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants