Skip to content

Commit 1840423

Browse files
authored
CLN explicit float casts in tests (#50733)
explicit float casts Co-authored-by: MarcoGorelli <>
1 parent d010c4a commit 1840423

File tree

7 files changed

+22
-8
lines changed

7 files changed

+22
-8
lines changed

pandas/tests/frame/test_arithmetic.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,15 +1936,19 @@ def test_dataframe_blockwise_slicelike():
19361936
# GH#34367
19371937
arr = np.random.randint(0, 1000, (100, 10))
19381938
df1 = DataFrame(arr)
1939-
df2 = df1.copy()
1939+
# Explicit cast to float to avoid implicit cast when setting nan
1940+
df2 = df1.copy().astype({1: "float", 3: "float", 7: "float"})
19401941
df2.iloc[0, [1, 3, 7]] = np.nan
19411942

1942-
df3 = df1.copy()
1943+
# Explicit cast to float to avoid implicit cast when setting nan
1944+
df3 = df1.copy().astype({5: "float"})
19431945
df3.iloc[0, [5]] = np.nan
19441946

1945-
df4 = df1.copy()
1947+
# Explicit cast to float to avoid implicit cast when setting nan
1948+
df4 = df1.copy().astype({2: "float", 3: "float", 4: "float"})
19461949
df4.iloc[0, np.arange(2, 5)] = np.nan
1947-
df5 = df1.copy()
1950+
# Explicit cast to float to avoid implicit cast when setting nan
1951+
df5 = df1.copy().astype({4: "float", 5: "float", 6: "float"})
19481952
df5.iloc[0, np.arange(4, 7)] = np.nan
19491953

19501954
for left, right in [(df1, df2), (df2, df3), (df4, df5)]:

pandas/tests/groupby/test_function.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,8 @@ def test_cummin(dtypes_for_minmax):
830830
tm.assert_frame_equal(result, expected, check_exact=True)
831831

832832
# Test nan in some values
833+
# Explicit cast to float to avoid implicit cast when setting nan
834+
base_df = base_df.astype({"B": "float"})
833835
base_df.loc[[0, 2, 4, 6], "B"] = np.nan
834836
expected = DataFrame({"B": [np.nan, 4, np.nan, 2, np.nan, 3, np.nan, 1]})
835837
result = base_df.groupby("A").cummin()
@@ -895,6 +897,8 @@ def test_cummax(dtypes_for_minmax):
895897
tm.assert_frame_equal(result, expected)
896898

897899
# Test nan in some values
900+
# Explicit cast to float to avoid implicit cast when setting nan
901+
base_df = base_df.astype({"B": "float"})
898902
base_df.loc[[0, 2, 4, 6], "B"] = np.nan
899903
expected = DataFrame({"B": [np.nan, 4, np.nan, 4, np.nan, 3, np.nan, 3]})
900904
result = base_df.groupby("A").cummax()

pandas/tests/io/formats/test_format.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ def test_eng_float_formatter(self, float_frame):
193193
)
194194
def test_show_counts(self, row, columns, show_counts, result):
195195

196-
df = DataFrame(1, columns=range(10), index=range(10))
196+
# Explicit cast to float to avoid implicit cast when setting nan
197+
df = DataFrame(1, columns=range(10), index=range(10)).astype({1: "float"})
197198
df.iloc[1, 1] = np.nan
198199

199200
with option_context(

pandas/tests/resample/test_base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ def test_asfreq_fill_value(series, create_index):
6868
expected = ser.reindex(new_index)
6969
tm.assert_series_equal(result, expected)
7070

71-
frame = ser.to_frame("value")
71+
# Explicit cast to float to avoid implicit cast when setting None
72+
frame = ser.astype("float").to_frame("value")
7273
frame.iloc[1] = None
7374
result = frame.resample("1H").asfreq(fill_value=4.0)
7475
new_index = create_index(frame.index[0], frame.index[-1], freq="1H")

pandas/tests/reshape/merge/test_multi.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ def run_asserts(left, right, sort):
124124

125125
lc = list(map(chr, np.arange(ord("a"), ord("z") + 1)))
126126
left = DataFrame(np.random.choice(lc, (5000, 2)), columns=["1st", "3rd"])
127-
left.insert(1, "2nd", np.random.randint(0, 1000, len(left)))
127+
# Explicit cast to float to avoid implicit cast when setting nan
128+
left.insert(1, "2nd", np.random.randint(0, 1000, len(left)).astype("float"))
128129

129130
i = np.random.permutation(len(left))
130131
right = left.iloc[i].copy()

pandas/tests/window/test_rolling.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,8 @@ def test_closed_uneven():
433433
def test_closed_min_max_minp(func, closed, expected):
434434
# see gh-21704
435435
ser = Series(data=np.arange(10), index=date_range("2000", periods=10))
436+
# Explicit cast to float to avoid implicit cast when setting nan
437+
ser = ser.astype("float")
436438
ser[ser.index[-3:]] = np.nan
437439
result = getattr(ser.rolling("3D", min_periods=2, closed=closed), func)()
438440
expected = Series(expected, index=ser.index)

pandas/tests/window/test_timeseries_window.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,8 @@ def test_ragged_max(self, ragged):
579579
def test_freqs_ops(self, freq, op, result_data):
580580
# GH 21096
581581
index = date_range(start="2018-1-1 01:00:00", freq=f"1{freq}", periods=10)
582-
s = Series(data=0, index=index)
582+
# Explicit cast to float to avoid implicit cast when setting nan
583+
s = Series(data=0, index=index, dtype="float")
583584
s.iloc[1] = np.nan
584585
s.iloc[-1] = 2
585586
result = getattr(s.rolling(window=f"10{freq}"), op)()

0 commit comments

Comments
 (0)