diff --git a/pandas/tests/frame/indexing/test_where.py b/pandas/tests/frame/indexing/test_where.py index f65f3a311b403..e37c881472b65 100644 --- a/pandas/tests/frame/indexing/test_where.py +++ b/pandas/tests/frame/indexing/test_where.py @@ -550,7 +550,8 @@ def test_where_axis_multiple_dtypes(self): # DataFrame vs DataFrame d1 = df.copy().drop(1, axis=0) - expected = df.copy() + # Explicit cast to avoid implicit cast when setting value to np.nan + expected = df.copy().astype("float") expected.loc[1, :] = np.nan result = df.where(mask, d1) @@ -669,7 +670,8 @@ def test_where_categorical_filtering(self): df["b"] = df["b"].astype("category") result = df.where(df["a"] > 0) - expected = df.copy() + # Explicitly cast to 'float' to avoid implicit cast when setting np.nan + expected = df.copy().astype({"a": "float"}) expected.loc[0, :] = np.nan tm.assert_equal(result, expected) diff --git a/pandas/tests/frame/methods/test_asfreq.py b/pandas/tests/frame/methods/test_asfreq.py index 07eacb5e89e3a..5b3e1614e1ada 100644 --- a/pandas/tests/frame/methods/test_asfreq.py +++ b/pandas/tests/frame/methods/test_asfreq.py @@ -159,7 +159,8 @@ def test_asfreq_fillvalue(self): # setup rng = date_range("1/1/2016", periods=10, freq="2S") - ts = Series(np.arange(len(rng)), index=rng) + # Explicit cast to 'float' to avoid implicit cast when setting None + ts = Series(np.arange(len(rng)), index=rng, dtype="float") df = DataFrame({"one": ts}) # insert pre-existing missing value diff --git a/pandas/tests/frame/methods/test_asof.py b/pandas/tests/frame/methods/test_asof.py index 0b27fe591f794..a08f8bf5f502e 100644 --- a/pandas/tests/frame/methods/test_asof.py +++ b/pandas/tests/frame/methods/test_asof.py @@ -29,7 +29,8 @@ def date_range_frame(): class TestFrameAsof: def test_basic(self, date_range_frame): - df = date_range_frame + # Explicitly cast to float to avoid implicit cast when setting np.nan + df = date_range_frame.astype({"A": "float"}) N = 50 df.loc[df.index[15:30], "A"] = np.nan dates = date_range("1/1/1990", periods=N * 3, freq="25s") @@ -50,7 +51,8 @@ def test_basic(self, date_range_frame): def test_subset(self, date_range_frame): N = 10 - df = date_range_frame.iloc[:N].copy() + # explicitly cast to float to avoid implicit upcast when setting to np.nan + df = date_range_frame.iloc[:N].copy().astype({"A": "float"}) df.loc[df.index[4:8], "A"] = np.nan dates = date_range("1/1/1990", periods=N * 3, freq="25s") @@ -163,7 +165,7 @@ def test_time_zone_aware_index(self, stamp, expected): def test_is_copy(self, date_range_frame): # GH-27357, GH-30784: ensure the result of asof is an actual copy and # doesn't track the parent dataframe / doesn't give SettingWithCopy warnings - df = date_range_frame + df = date_range_frame.astype({"A": "float"}) N = 50 df.loc[df.index[15:30], "A"] = np.nan dates = date_range("1/1/1990", periods=N * 3, freq="25s") diff --git a/pandas/tests/frame/test_stack_unstack.py b/pandas/tests/frame/test_stack_unstack.py index 1b6b158cc61f5..2be2a052401ed 100644 --- a/pandas/tests/frame/test_stack_unstack.py +++ b/pandas/tests/frame/test_stack_unstack.py @@ -857,6 +857,8 @@ def cast(val): def test_unstack_nan_index2(self): # GH7403 df = DataFrame({"A": list("aaaabbbb"), "B": range(8), "C": range(8)}) + # Explicit cast to avoid implicit cast when setting to np.NaN + df = df.astype({"B": "float"}) df.iloc[3, 1] = np.NaN left = df.set_index(["A", "B"]).unstack(0) @@ -874,6 +876,8 @@ def test_unstack_nan_index2(self): tm.assert_frame_equal(left, right) df = DataFrame({"A": list("aaaabbbb"), "B": list(range(4)) * 2, "C": range(8)}) + # Explicit cast to avoid implicit cast when setting to np.NaN + df = df.astype({"B": "float"}) df.iloc[2, 1] = np.NaN left = df.set_index(["A", "B"]).unstack(0) @@ -886,6 +890,8 @@ def test_unstack_nan_index2(self): tm.assert_frame_equal(left, right) df = DataFrame({"A": list("aaaabbbb"), "B": list(range(4)) * 2, "C": range(8)}) + # Explicit cast to avoid implicit cast when setting to np.NaN + df = df.astype({"B": "float"}) df.iloc[3, 1] = np.NaN left = df.set_index(["A", "B"]).unstack(0) diff --git a/pandas/tests/groupby/test_value_counts.py b/pandas/tests/groupby/test_value_counts.py index 577a72d3f5090..9ce8fea30e3b3 100644 --- a/pandas/tests/groupby/test_value_counts.py +++ b/pandas/tests/groupby/test_value_counts.py @@ -56,6 +56,8 @@ def seed_df(seed_nans, n, m): ) if seed_nans: + # Explicitly cast to float to avoid implicit cast when setting nan + frame["3rd"] = frame["3rd"].astype("float") frame.loc[1::11, "1st"] = np.nan frame.loc[3::17, "2nd"] = np.nan frame.loc[7::19, "3rd"] = np.nan