diff options
author | Miles Cranmer <miles.cranmer@gmail.com> | 2022-06-29 17:49:50 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-29 14:49:50 -0700 |
commit | f9bed20bffd88bce06dbc8be200179edfe7580a4 (patch) | |
tree | 811c7302e39860f5731f315882f09631ff9b550e /numpy/lib/arraysetops.py | |
parent | 10f0e0ad0f8b5bbc58cbc45623cc7f4f80eaba3b (diff) | |
download | numpy-f9bed20bffd88bce06dbc8be200179edfe7580a4.tar.gz |
BUG: Fix numpy.isin for timedelta dtype (#21860)
This PR fixes the issue discussed on #12065 and #21843 where 'timedelta64' was noted to be a subtype of numpy.integer. This in principle should detect any cases where int(np.min(ar2)) fails. This PR also adds unittests for these.
* TST: Create in1d test for timedelta input
* MAINT: fix in1d for timedelta input
* TST: in1d raise ValueError for timedelta input
* MAINT: Clean up type checking for isin kind="table"
* TST: Add test for mixed boolean/integer in1d
* MAINT: Increase readability of in1d type checking
* STY: Apply small code style tweaks
This is probably really mainly my personal opinion...
Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>
Diffstat (limited to 'numpy/lib/arraysetops.py')
-rw-r--r-- | numpy/lib/arraysetops.py | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py index fa332e7fd..cf5f47a82 100644 --- a/numpy/lib/arraysetops.py +++ b/numpy/lib/arraysetops.py @@ -621,28 +621,28 @@ def in1d(ar1, ar2, assume_unique=False, invert=False, *, kind=None): # Ensure that iteration through object arrays yields size-1 arrays if ar2.dtype == object: ar2 = ar2.reshape(-1, 1) - # Convert booleans to uint8 so we can use the fast integer algorithm - if ar1.dtype == bool: - ar1 = ar1 + np.uint8(0) - if ar2.dtype == bool: - ar2 = ar2 + np.uint8(0) - - # Check if we can use a fast integer algorithm: - integer_arrays = (np.issubdtype(ar1.dtype, np.integer) and - np.issubdtype(ar2.dtype, np.integer)) if kind not in {None, 'sort', 'table'}: raise ValueError( f"Invalid kind: '{kind}'. Please use None, 'sort' or 'table'.") - if integer_arrays and kind in {None, 'table'}: + # Can use the table method if all arrays are integers or boolean: + is_int_arrays = all(ar.dtype.kind in ("u", "i", "b") for ar in (ar1, ar2)) + use_table_method = is_int_arrays and kind in {None, 'table'} + if use_table_method: if ar2.size == 0: if invert: return np.ones_like(ar1, dtype=bool) else: return np.zeros_like(ar1, dtype=bool) + # Convert booleans to uint8 so we can use the fast integer algorithm + if ar1.dtype == bool: + ar1 = ar1.astype(np.uint8) + if ar2.dtype == bool: + ar2 = ar2.astype(np.uint8) + ar2_min = np.min(ar2) ar2_max = np.max(ar2) |