Skip to content

Commit 3e5836c

Browse files
committed
Tests for take method check against object dtype for boolean inputs.
1 parent 1bdb1f8 commit 3e5836c

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

pandas/core/arrays/numpy_.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -368,16 +368,10 @@ def take(
368368
indices, allow_fill=allow_fill, fill_value=fill_value, axis=axis
369369
)
370370
# See GH#62448.
371-
if self.dtype.numpy_dtype in [
372-
np.uint8,
373-
np.uint16,
374-
np.uint32,
375-
np.uint64,
376-
np.int8,
377-
np.int16,
378-
np.int32,
379-
np.int64,
380-
]:
371+
# TODO: Not all Pandas extension dtypes have an underlying Numpy dtype.
372+
# I will need to handle the case where self.dtype doesn't have this
373+
# attribute.
374+
if self.dtype.kind in "iub":
381375
return type(self)(result, copy=False)
382376

383377
return result

pandas/tests/arrays/numpy_/test_numpy.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -325,31 +325,56 @@ def test_factorize_unsigned():
325325
tm.assert_extension_array_equal(res_unique, NumpyExtensionArray(exp_unique))
326326

327327

328-
# TODO: Add the smaller width dtypes to the parameter sets of these tests.
329328
@pytest.mark.parametrize(
330329
"dtype",
331-
[np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int16, np.int32, np.int64],
330+
[
331+
np.bool,
332+
np.uint8,
333+
np.uint16,
334+
np.uint32,
335+
np.uint64,
336+
np.int8,
337+
np.int16,
338+
np.int32,
339+
np.int64,
340+
],
332341
)
333342
def test_take_assigns_floating_point_dtype(dtype):
334343
# GH#62448.
335-
array = NumpyExtensionArray(np.array([1, 2, 3], dtype=dtype))
344+
if dtype == np.bool:
345+
array = NumpyExtensionArray(np.array([False, True, False], dtype=dtype))
346+
expected = np.dtype(object)
347+
else:
348+
array = NumpyExtensionArray(np.array([1, 2, 3], dtype=dtype))
349+
expected = np.float64
336350

337351
result = array.take([-1], allow_fill=True)
338-
339-
assert result.dtype.numpy_dtype == np.float64
352+
assert result.dtype.numpy_dtype == expected
340353

341354
result = array.take([-1], allow_fill=True, fill_value=5.0)
342-
343-
assert result.dtype.numpy_dtype == np.float64
355+
assert result.dtype.numpy_dtype == expected
344356

345357

346358
@pytest.mark.parametrize(
347359
"dtype",
348-
[np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int16, np.int32, np.int64],
360+
[
361+
np.bool,
362+
np.uint8,
363+
np.uint16,
364+
np.uint32,
365+
np.uint64,
366+
np.int8,
367+
np.int16,
368+
np.int32,
369+
np.int64,
370+
],
349371
)
350372
def test_take_assigns_integer_dtype_when_fill_disallowed(dtype):
351373
# GH#62448.
352-
array = NumpyExtensionArray(np.array([1, 2, 3], dtype=dtype))
374+
if dtype == np.bool:
375+
array = NumpyExtensionArray(np.array([False, True, False], dtype=dtype))
376+
else:
377+
array = NumpyExtensionArray(np.array([1, 2, 3], dtype=dtype))
353378

354379
result = array.take([-1], allow_fill=False)
355380

0 commit comments

Comments
 (0)