Skip to content

BUG: DatetimeIndex has become unhashable in 1.3.1? #42844

Closed
@yohplala

Description

@yohplala
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Hi,
Following code below was working with pandas 1.2.5.
It raises now an error with pandas 1.3.1.

Code Sample, a copy-pastable example

import pandas as pd
import random

# Right data
ts_open = pd.DatetimeIndex([
                           pd.Timestamp('2021/01/01 00:37'),
                           pd.Timestamp('2021/01/01 00:40'),
                           pd.Timestamp('2021/01/01 01:00'),
                           pd.Timestamp('2021/01/01 03:45'),
                           pd.Timestamp('2021/01/01 03:59'),
                           pd.Timestamp('2021/01/01 05:20')])
length = len(ts_open)
random.seed(1)
volume = random.sample(range(1, length+1), length)
df_smpl = pd.DataFrame({'volume' : volume, 'ts_open': ts_open})

# Left data
ts_full = pd.date_range(start=pd.Timestamp('2021/01/01 00:00'),
                        periods=7, freq='1h')
ts_event = ts_full[:-1]
length_ipr = len(ts_event)
random.seed(2)
volume = random.sample(range(20, length_ipr+20), length_ipr)
df_ipr = pd.DataFrame({'volume' : volume, 'timestamp': ts_event})

at = ts_full[1:]
df_smpl.index = df_smpl['ts_open']

# Test
df_full = pd.merge_asof(df_ipr, df_smpl,
                        left_on=at, right_index=True,
                        allow_exact_matches=False, direction='backward')

Error message:

Traceback (most recent call last):

  File "<ipython-input-152-b5845a330a4b>", line 30, in <module>
    df_full = pd.merge_asof(df_ipr, df_smpl,

  File "/home/yoh/anaconda3/lib/python3.8/site-packages/pandas/core/reshape/merge.py", line 581, in merge_asof
    op = _AsOfMerge(

  File "/home/yoh/anaconda3/lib/python3.8/site-packages/pandas/core/reshape/merge.py", line 1736, in __init__
    _OrderedMerge.__init__(

  File "/home/yoh/anaconda3/lib/python3.8/site-packages/pandas/core/reshape/merge.py", line 1619, in __init__
    _MergeOperation.__init__(

  File "/home/yoh/anaconda3/lib/python3.8/site-packages/pandas/core/reshape/merge.py", line 682, in __init__
    self._validate_specification()

  File "/home/yoh/anaconda3/lib/python3.8/site-packages/pandas/core/reshape/merge.py", line 1782, in _validate_specification
    if left_on in self.left.columns

  File "/home/yoh/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 4572, in __contains__
    hash(key)

TypeError: unhashable type: 'DatetimeIndex'

Output of pd.show_versions()

INSTALLED VERSIONS

commit : c7f7443
python : 3.8.8.final.0
python-bits : 64
OS : Linux
OS-release : 5.8.0-63-generic
Version : #71~20.04.1-Ubuntu SMP Thu Jul 15 17:46:08 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.1
numpy : 1.20.3
pytz : 2021.1
dateutil : 2.8.2
pip : 21.1.3
setuptools : 52.0.0.post20210125
Cython : 0.29.24
pytest : 6.2.4
hypothesis : None
sphinx : 4.0.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.22.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : 2021.07.0
fastparquet : 0.7.0
gcsfs : None
matplotlib : 3.3.4
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 3.0.0
pyxlsb : None
s3fs : None
scipy : 1.6.2
sqlalchemy : None
tables : None
tabulate : 0.8.9
xarray : 0.19.0
xlrd : None
xlwt : None
numba : 0.53.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDatetimeDatetime data dtypeRegressionFunctionality that used to work in a prior pandas versionReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions