From 60483ab2ab7c3a95ac776d5c6b73b5c3fc7962cb Mon Sep 17 00:00:00 2001 From: Li Date: Thu, 13 Nov 2025 07:00:17 +0000 Subject: [PATCH] address issue #52965: add tests --- pandas/tests/indexing/test_iloc.py | 16 ++++++++++++++++ pandas/tests/indexing/test_loc.py | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index ddb58ecbfa6f3..290a0c3205898 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -62,6 +62,22 @@ def test_iloc_getitem_int_and_list_int(self, key, frame_or_series, index, reques # array of ints (GH5006), make sure that a single indexer is returning # the correct type + @pytest.mark.parametrize( + "original_type, new_type", + [ + (np.uint16, np.uint8), + (np.int32, np.int16), + (np.int64, np.uint64), + (np.int64, np.int16), + ], + ) + def test_iloc_assign_preserve_dtype(self, original_type, new_type): + s = Series([10, 20, 30], index=list("abc"), dtype=original_type) + s.iloc[0:2] = np.array([100, 200], dtype=new_type) + + expected = original_type + assert s.dtype == expected + class TestiLocBaseIndependent: """Tests Independent Of Base Class""" diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 8d59b0c026e0c..a2b79aff01294 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -277,6 +277,22 @@ def test_loc_empty_slice_assignment_with_datetime(self, data): df.loc[mask] = df tm.assert_frame_equal(df, expected) + @pytest.mark.parametrize( + "original_type, new_type", + [ + (np.uint16, np.uint8), + (np.int32, np.int16), + (np.int64, np.uint64), + (np.int64, np.int16), + ], + ) + def test_loc_assign_preserve_dtype(self, original_type, new_type): + s = Series([10, 20, 30], index=list("abc"), dtype=original_type) + s.loc["a":"b"] = np.array([100, 200], dtype=new_type) + + expected = original_type + assert s.dtype == expected + class TestLocBaseIndependent: # Tests for loc that do not depend on subclassing Base