Skip to content

Commit ede597b

Browse files
Backport PR #41974: BUG: UInt64Index.where with int64 value (#42036)
Co-authored-by: jbrockmendel <[email protected]>
1 parent 9ac9699 commit ede597b

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

doc/source/whatsnew/v1.3.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,8 @@ Numeric
948948
- Bug in :meth:`Series.count` would result in an ``int32`` result on 32-bit platforms when argument ``level=None`` (:issue:`40908`)
949949
- Bug in :class:`Series` and :class:`DataFrame` reductions with methods ``any`` and ``all`` not returning Boolean results for object data (:issue:`12863`, :issue:`35450`, :issue:`27709`)
950950
- Bug in :meth:`Series.clip` would fail if the Series contains NA values and has nullable int or float as a data type (:issue:`40851`)
951+
- Bug in :meth:`UInt64Index.where` and :meth:`UInt64Index.putmask` with an ``np.int64`` dtype ``other`` incorrectly raising ``TypeError`` (:issue:`41974`)
952+
951953

952954
Conversion
953955
^^^^^^^^^^

pandas/core/indexes/numeric.py

+10
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,16 @@ class UInt64Index(IntegerIndex):
370370
_default_dtype = np.dtype(np.uint64)
371371
_dtype_validation_metadata = (is_unsigned_integer_dtype, "unsigned integer")
372372

373+
def _validate_fill_value(self, value):
374+
# e.g. np.array([1]) we want np.array([1], dtype=np.uint64)
375+
# see test_where_uin64
376+
super()._validate_fill_value(value)
377+
if hasattr(value, "dtype") and is_signed_integer_dtype(value.dtype):
378+
if (value >= 0).all():
379+
return value.astype(self.dtype)
380+
raise TypeError
381+
return value
382+
373383

374384
class Float64Index(NumericIndex):
375385
_index_descr_args = {

pandas/tests/indexes/numeric/test_indexing.py

+13
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,19 @@ def test_where(self, klass, index):
376376
result = index.where(klass(cond))
377377
tm.assert_index_equal(result, expected)
378378

379+
def test_where_uin64(self):
380+
idx = UInt64Index([0, 6, 2])
381+
mask = np.array([False, True, False])
382+
other = np.array([1], dtype=np.int64)
383+
384+
expected = UInt64Index([1, 6, 1])
385+
386+
result = idx.where(mask, other)
387+
tm.assert_index_equal(result, expected)
388+
389+
result = idx.putmask(~mask, other)
390+
tm.assert_index_equal(result, expected)
391+
379392

380393
class TestTake:
381394
@pytest.mark.parametrize("klass", [Float64Index, Int64Index, UInt64Index])

0 commit comments

Comments
 (0)