bug: merge_asof with tz-aware datetime "by" parameter raises · Issue #21184 · pandas-dev/pandas (original) (raw)

Code Sample

import pandas as pd

left = pd.DataFrame({'by_col': pd.DatetimeIndex(['2018-01-01']).tz_localize('UTC'), 'on_col': [2], 'values': ['a']}) right = pd.DataFrame({'by_col': pd.DatetimeIndex(['2018-01-01']).tz_localize('UTC'), 'on_col': [1], 'values': ['b']})

merged = pd.merge_asof(left, right, by='by_col', on='on_col')

Error traceback

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Hamb\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\reshape\merge.py", line 478, in merge_asof
    return op.get_result()
  File "C:\Users\Hamb\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\reshape\merge.py", line 1163, in get_result
    join_index, left_indexer, right_indexer = self._get_join_info()
  File "C:\Users\Hamb\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\reshape\merge.py", line 776, in _get_join_info
    right_indexer) = self._get_join_indexers()
  File "C:\Users\Hamb\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\reshape\merge.py", line 1437, in _get_join_indexers
    tolerance)
TypeError: Argument 'left_by_values' has incorrect type (expected numpy.ndarray, got Index)

Problem description

Function pandas.merge_asof raises when "by" parameter is provided a column of tz-aware datetime type.
Note that the same code with tz-naive datetimes works :

import pandas as pd

left = pd.DataFrame({'by_col': pd.DatetimeIndex(['2018-01-01']), 'on_col': [2], 'values': ['a']}) right = pd.DataFrame({'by_col': pd.DatetimeIndex(['2018-01-01']), 'on_col': [1], 'values': ['b']})

merged = pd.merge_asof(left, right, by='by_col', on='on_col') print(merged)

outputs :

    by_col  on_col values_x values_y
0 2018-01-01       2        a        b

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.4.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: FR
LOCALE: None.None
pandas: 0.23.0
pytest: None
pip: 10.0.1
setuptools: 38.5.1
Cython: None
numpy: 1.14.1
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2018.3
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: 0.9999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None