Skip to content

Commit 2da7128

Browse files
authored
CLN explicit casts to float to avoid implicit casts (#50540)
* explicit casts to float to avoid implicit casts * move the cast to where its needed Co-authored-by: MarcoGorelli <>
1 parent 0a4ba64 commit 2da7128

File tree

5 files changed

+19
-6
lines changed

5 files changed

+19
-6
lines changed

pandas/tests/frame/indexing/test_where.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,8 @@ def test_where_axis_multiple_dtypes(self):
550550

551551
# DataFrame vs DataFrame
552552
d1 = df.copy().drop(1, axis=0)
553-
expected = df.copy()
553+
# Explicit cast to avoid implicit cast when setting value to np.nan
554+
expected = df.copy().astype("float")
554555
expected.loc[1, :] = np.nan
555556

556557
result = df.where(mask, d1)
@@ -669,7 +670,8 @@ def test_where_categorical_filtering(self):
669670
df["b"] = df["b"].astype("category")
670671

671672
result = df.where(df["a"] > 0)
672-
expected = df.copy()
673+
# Explicitly cast to 'float' to avoid implicit cast when setting np.nan
674+
expected = df.copy().astype({"a": "float"})
673675
expected.loc[0, :] = np.nan
674676

675677
tm.assert_equal(result, expected)

pandas/tests/frame/methods/test_asfreq.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ def test_asfreq_fillvalue(self):
159159

160160
# setup
161161
rng = date_range("1/1/2016", periods=10, freq="2S")
162-
ts = Series(np.arange(len(rng)), index=rng)
162+
# Explicit cast to 'float' to avoid implicit cast when setting None
163+
ts = Series(np.arange(len(rng)), index=rng, dtype="float")
163164
df = DataFrame({"one": ts})
164165

165166
# insert pre-existing missing value

pandas/tests/frame/methods/test_asof.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def date_range_frame():
2929

3030
class TestFrameAsof:
3131
def test_basic(self, date_range_frame):
32-
df = date_range_frame
32+
# Explicitly cast to float to avoid implicit cast when setting np.nan
33+
df = date_range_frame.astype({"A": "float"})
3334
N = 50
3435
df.loc[df.index[15:30], "A"] = np.nan
3536
dates = date_range("1/1/1990", periods=N * 3, freq="25s")
@@ -50,7 +51,8 @@ def test_basic(self, date_range_frame):
5051

5152
def test_subset(self, date_range_frame):
5253
N = 10
53-
df = date_range_frame.iloc[:N].copy()
54+
# explicitly cast to float to avoid implicit upcast when setting to np.nan
55+
df = date_range_frame.iloc[:N].copy().astype({"A": "float"})
5456
df.loc[df.index[4:8], "A"] = np.nan
5557
dates = date_range("1/1/1990", periods=N * 3, freq="25s")
5658

@@ -163,7 +165,7 @@ def test_time_zone_aware_index(self, stamp, expected):
163165
def test_is_copy(self, date_range_frame):
164166
# GH-27357, GH-30784: ensure the result of asof is an actual copy and
165167
# doesn't track the parent dataframe / doesn't give SettingWithCopy warnings
166-
df = date_range_frame
168+
df = date_range_frame.astype({"A": "float"})
167169
N = 50
168170
df.loc[df.index[15:30], "A"] = np.nan
169171
dates = date_range("1/1/1990", periods=N * 3, freq="25s")

pandas/tests/frame/test_stack_unstack.py

+6
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,8 @@ def cast(val):
857857
def test_unstack_nan_index2(self):
858858
# GH7403
859859
df = DataFrame({"A": list("aaaabbbb"), "B": range(8), "C": range(8)})
860+
# Explicit cast to avoid implicit cast when setting to np.NaN
861+
df = df.astype({"B": "float"})
860862
df.iloc[3, 1] = np.NaN
861863
left = df.set_index(["A", "B"]).unstack(0)
862864

@@ -874,6 +876,8 @@ def test_unstack_nan_index2(self):
874876
tm.assert_frame_equal(left, right)
875877

876878
df = DataFrame({"A": list("aaaabbbb"), "B": list(range(4)) * 2, "C": range(8)})
879+
# Explicit cast to avoid implicit cast when setting to np.NaN
880+
df = df.astype({"B": "float"})
877881
df.iloc[2, 1] = np.NaN
878882
left = df.set_index(["A", "B"]).unstack(0)
879883

@@ -886,6 +890,8 @@ def test_unstack_nan_index2(self):
886890
tm.assert_frame_equal(left, right)
887891

888892
df = DataFrame({"A": list("aaaabbbb"), "B": list(range(4)) * 2, "C": range(8)})
893+
# Explicit cast to avoid implicit cast when setting to np.NaN
894+
df = df.astype({"B": "float"})
889895
df.iloc[3, 1] = np.NaN
890896
left = df.set_index(["A", "B"]).unstack(0)
891897

pandas/tests/groupby/test_value_counts.py

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ def seed_df(seed_nans, n, m):
5656
)
5757

5858
if seed_nans:
59+
# Explicitly cast to float to avoid implicit cast when setting nan
60+
frame["3rd"] = frame["3rd"].astype("float")
5961
frame.loc[1::11, "1st"] = np.nan
6062
frame.loc[3::17, "2nd"] = np.nan
6163
frame.loc[7::19, "3rd"] = np.nan

0 commit comments

Comments
 (0)