Skip to content

CLN: refactor hash_array to resolve circular dependency #53347

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

Merged
merged 6 commits into from
May 24, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions pandas/core/util/hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
from __future__ import annotations

from functools import partial
import itertools
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -254,15 +255,24 @@ def hash_array(
encoding=encoding, hash_key=hash_key, categorize=categorize
)

elif not isinstance(vals, np.ndarray):
if isinstance(vals, np.ndarray):
_hash_ndarray_partial = partial(
_hash_ndarray, encoding=encoding, hash_key=hash_key, categorize=categorize
)
if np.issubdtype(vals.dtype, np.complex128):
# _hash_ndarray only takes 64-bit values, so handle 128-bit by parts
hash_real = _hash_ndarray_partial(np.real(vals))
hash_imag = _hash_ndarray_partial(np.imag(vals))
return hash_real + 23 * hash_imag
else:
return _hash_ndarray_partial(vals)
else:
# GH#42003
raise TypeError(
"hash_array requires np.ndarray or ExtensionArray, not "
f"{type(vals).__name__}. Use hash_pandas_object instead."
)

return _hash_ndarray(vals, encoding, hash_key, categorize)


def _hash_ndarray(
vals: np.ndarray,
Expand All @@ -275,14 +285,9 @@ def _hash_ndarray(
"""
dtype = vals.dtype

# we'll be working with everything as 64-bit values, so handle this
# 128-bit value early
if np.issubdtype(dtype, np.complex128):
return hash_array(np.real(vals)) + 23 * hash_array(np.imag(vals))

# First, turn whatever array this is into unsigned 64-bit ints, if we can
# manage it.
elif dtype == bool:
if dtype == bool:
vals = vals.astype("u8")
elif issubclass(dtype.type, (np.datetime64, np.timedelta64)):
vals = vals.view("i8").astype("u8", copy=False)
Expand Down