Skip to content

Commit 14e7c4a

Browse files
authored
CLN: avoid upcasting in tests where unnecessary (PDEP-6 precursor) (#53104)
* remove more upcasts * 🎨 --------- Co-authored-by: MarcoGorelli <>
1 parent 088c18d commit 14e7c4a

File tree

5 files changed

+16
-12
lines changed

5 files changed

+16
-12
lines changed

pandas/tests/frame/indexing/test_where.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -328,15 +328,14 @@ def test_where_bug_mixed(self, any_signed_int_numpy_dtype):
328328
)
329329

330330
expected = DataFrame(
331-
{"a": [np.nan, np.nan, 3.0, 4.0], "b": [4.0, 3.0, np.nan, np.nan]},
332-
dtype="float64",
333-
)
331+
{"a": [-1, -1, 3, 4], "b": [4.0, 3.0, -1, -1]},
332+
).astype({"a": any_signed_int_numpy_dtype, "b": "float64"})
334333

335-
result = df.where(df > 2, np.nan)
334+
result = df.where(df > 2, -1)
336335
tm.assert_frame_equal(result, expected)
337336

338337
result = df.copy()
339-
return_value = result.where(result > 2, np.nan, inplace=True)
338+
return_value = result.where(result > 2, -1, inplace=True)
340339
assert return_value is None
341340
tm.assert_frame_equal(result, expected)
342341

@@ -1028,7 +1027,7 @@ def test_where_int_overflow(replacement):
10281027

10291028
def test_where_inplace_no_other():
10301029
# GH#51685
1031-
df = DataFrame({"a": [1, 2], "b": ["x", "y"]})
1030+
df = DataFrame({"a": [1.0, 2.0], "b": ["x", "y"]})
10321031
cond = DataFrame({"a": [True, False], "b": [False, True]})
10331032
df.where(cond, inplace=True)
10341033
expected = DataFrame({"a": [1, np.nan], "b": [np.nan, "y"]})

pandas/tests/indexing/test_indexing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -429,14 +429,15 @@ def test_set_index_nan(self):
429429

430430
def test_multi_assign(self):
431431
# GH 3626, an assignment of a sub-df to a df
432+
# set float64 to avoid upcast when setting nan
432433
df = DataFrame(
433434
{
434435
"FC": ["a", "b", "a", "b", "a", "b"],
435436
"PF": [0, 0, 0, 0, 1, 1],
436437
"col1": list(range(6)),
437438
"col2": list(range(6, 12)),
438439
}
439-
)
440+
).astype({"col2": "float64"})
440441
df.iloc[1, 0] = np.nan
441442
df2 = df.copy()
442443

pandas/tests/indexing/test_loc.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,8 @@ def test_loc_setitem_with_scalar_index(self, indexer, value):
863863
# assigning like "df.loc[0, ['A']] = ['Z']" should be evaluated
864864
# elementwisely, not using "setter('A', ['Z'])".
865865

866-
df = DataFrame([[1, 2], [3, 4]], columns=["A", "B"])
866+
# Set object dtype to avoid upcast when setting 'Z'
867+
df = DataFrame([[1, 2], [3, 4]], columns=["A", "B"]).astype({"A": object})
867868
df.loc[0, indexer] = value
868869
result = df.loc[0, "A"]
869870

@@ -1524,7 +1525,8 @@ def test_loc_setitem_td64_non_nano(self):
15241525

15251526
def test_loc_setitem_2d_to_1d_raises(self):
15261527
data = np.random.randn(2, 2)
1527-
ser = Series(range(2))
1528+
# float64 dtype to avoid upcast when trying to set float data
1529+
ser = Series(range(2), dtype="float64")
15281530

15291531
msg = "|".join(
15301532
[

pandas/tests/interchange/test_impl.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def test_missing_from_masked():
171171
{
172172
"x": np.array([1.0, 2.0, 3.0, 4.0, 0.0]),
173173
"y": np.array([1.5, 2.5, 3.5, 4.5, 0]),
174-
"z": np.array([True, False, True, True, True]),
174+
"z": np.array([1.0, 0.0, 1.0, 1.0, 1.0]),
175175
}
176176
)
177177

pandas/tests/series/indexing/test_setitem.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ def test_setitem_multiindex_empty_slice(self):
6161

6262
def test_setitem_with_string_index(self):
6363
# GH#23451
64-
ser = Series([1, 2, 3], index=["Date", "b", "other"])
64+
# Set object dtype to avoid upcast when setting date.today()
65+
ser = Series([1, 2, 3], index=["Date", "b", "other"], dtype=object)
6566
ser["Date"] = date.today()
6667
assert ser.Date == date.today()
6768
assert ser["Date"] == date.today()
@@ -459,7 +460,8 @@ def test_setitem_callable_other(self):
459460
# GH#13299
460461
inc = lambda x: x + 1
461462

462-
ser = Series([1, 2, -1, 4])
463+
# set object dtype to avoid upcast when setting inc
464+
ser = Series([1, 2, -1, 4], dtype=object)
463465
ser[ser < 0] = inc
464466

465467
expected = Series([1, 2, inc, 4])

0 commit comments

Comments
 (0)