Skip to content

Commit 23c6b9e

Browse files
fangchenliim-vinicius
authored and
im-vinicius
committed
CLN: refactor hash_array to resolve circular dependency (pandas-dev#53347)
* CLN: refactor hash_array to resolve circular dependency * use recursion to handle all dtype in one function * fix comment and white space
1 parent e16ff8e commit 23c6b9e

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

pandas/core/util/hashing.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def hash_array(
254254
encoding=encoding, hash_key=hash_key, categorize=categorize
255255
)
256256

257-
elif not isinstance(vals, np.ndarray):
257+
if not isinstance(vals, np.ndarray):
258258
# GH#42003
259259
raise TypeError(
260260
"hash_array requires np.ndarray or ExtensionArray, not "
@@ -275,14 +275,15 @@ def _hash_ndarray(
275275
"""
276276
dtype = vals.dtype
277277

278-
# we'll be working with everything as 64-bit values, so handle this
279-
# 128-bit value early
278+
# _hash_ndarray only takes 64-bit values, so handle 128-bit by parts
280279
if np.issubdtype(dtype, np.complex128):
281-
return hash_array(np.real(vals)) + 23 * hash_array(np.imag(vals))
280+
hash_real = _hash_ndarray(vals.real, encoding, hash_key, categorize)
281+
hash_imag = _hash_ndarray(vals.imag, encoding, hash_key, categorize)
282+
return hash_real + 23 * hash_imag
282283

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

0 commit comments

Comments
 (0)