Skip to content

Commit 3d0a5b3

Browse files
MarcoGorelliYi Wei
authored and
Yi Wei
committed
CLN: avoid upcasting in tests where unnecessary (PDEP-6 precursor) (pandas-dev#53075)
dont upcast when unnecessary Co-authored-by: MarcoGorelli <>
1 parent 336eab1 commit 3d0a5b3

File tree

7 files changed

+14
-5
lines changed

7 files changed

+14
-5
lines changed

pandas/tests/indexing/test_at.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ def test_at_setitem_multiindex(self):
114114

115115
@pytest.mark.parametrize("row", (Timestamp("2019-01-01"), "2019-01-01"))
116116
def test_at_datetime_index(self, row):
117+
# Set float64 dtype to avoid upcast when setting .5
117118
df = DataFrame(
118119
data=[[1] * 2], index=DatetimeIndex(data=["2019-01-01", "2019-01-02"])
119-
)
120+
).astype({0: "float64"})
120121
expected = DataFrame(
121122
data=[[0.5, 1], [1.0, 1]],
122123
index=DatetimeIndex(data=["2019-01-01", "2019-01-02"]),

pandas/tests/indexing/test_iloc.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,8 @@ def test_iloc_setitem_with_scalar_index(self, indexer, value):
719719
# assigning like "df.iloc[0, [0]] = ['Z']" should be evaluated
720720
# elementwisely, not using "setter('A', ['Z'])".
721721

722-
df = DataFrame([[1, 2], [3, 4]], columns=["A", "B"])
722+
# Set object type to avoid upcast when setting "Z"
723+
df = DataFrame([[1, 2], [3, 4]], columns=["A", "B"]).astype({"A": object})
723724
df.iloc[0, indexer] = value
724725
result = df.iloc[0, 0]
725726

pandas/tests/indexing/test_scalar.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def test_mixed_index_at_iat_loc_iloc_dataframe(self):
202202

203203
def test_iat_setter_incompatible_assignment(self):
204204
# GH 23236
205-
result = DataFrame({"a": [0, 1], "b": [4, 5]})
205+
result = DataFrame({"a": [0.0, 1.0], "b": [4, 5]})
206206
result.iat[0, 0] = None
207207
expected = DataFrame({"a": [None, 1], "b": [4, 5]})
208208
tm.assert_frame_equal(result, expected)

pandas/tests/interchange/test_impl.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def test_dataframe(data):
157157
def test_missing_from_masked():
158158
df = pd.DataFrame(
159159
{
160-
"x": np.array([1, 2, 3, 4, 0]),
160+
"x": np.array([1.0, 2.0, 3.0, 4.0, 0.0]),
161161
"y": np.array([1.5, 2.5, 3.5, 4.5, 0]),
162162
"z": np.array([True, False, True, True, True]),
163163
}

pandas/tests/reshape/test_from_dummies.py

+4
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ def test_error_with_prefix_default_category_dict_not_complete(
163163

164164

165165
def test_error_with_prefix_contains_nan(dummies_basic):
166+
# Set float64 dtype to avoid upcast when setting np.nan
167+
dummies_basic["col2_c"] = dummies_basic["col2_c"].astype("float64")
166168
dummies_basic.loc[2, "col2_c"] = np.nan
167169
with pytest.raises(
168170
ValueError, match=r"Dummy DataFrame contains NA value in column: 'col2_c'"
@@ -171,6 +173,8 @@ def test_error_with_prefix_contains_nan(dummies_basic):
171173

172174

173175
def test_error_with_prefix_contains_non_dummies(dummies_basic):
176+
# Set object dtype to avoid upcast when setting "str"
177+
dummies_basic["col2_c"] = dummies_basic["col2_c"].astype(object)
174178
dummies_basic.loc[2, "col2_c"] = "str"
175179
with pytest.raises(TypeError, match=r"Passed DataFrame contains non-dummy data"):
176180
from_dummies(dummies_basic, sep="_")

pandas/tests/series/methods/test_update.py

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def test_update(self, using_copy_on_write):
2525
# GH 3217
2626
df = DataFrame([{"a": 1}, {"a": 3, "b": 2}])
2727
df["c"] = np.nan
28+
# Cast to object to avoid upcast when setting "foo"
29+
df["c"] = df["c"].astype(object)
2830
df_orig = df.copy()
2931

3032
df["c"].update(Series(["foo"], index=[0]))

pandas/tests/series/test_missing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ def test_valid(self, datetime_series):
9191

9292
def test_hasnans_uncached_for_series():
9393
# GH#19700
94-
idx = Index([0, 1])
94+
# set float64 dtype to avoid upcast when setting nan
95+
idx = Index([0, 1], dtype="float64")
9596
assert idx.hasnans is False
9697
assert "hasnans" in idx._cache
9798
ser = idx.to_series()

0 commit comments

Comments
 (0)