Skip to content

Commit 98efdc8

Browse files
authored
CLN avoid some upcasting when its not the purpose of the test (#50493)
* avoid some upcasting when its not the purpose of the test * add comments * add one more explicit upcast Co-authored-by: MarcoGorelli <>
1 parent a28cadb commit 98efdc8

File tree

5 files changed

+14
-4
lines changed

5 files changed

+14
-4
lines changed

pandas/tests/frame/methods/test_equals.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def test_equals(self):
3636
df1["start"] = date_range("2000-1-1", periods=10, freq="T")
3737
df1["end"] = date_range("2000-1-1", periods=10, freq="D")
3838
df1["diff"] = df1["end"] - df1["start"]
39-
df1["bool"] = np.arange(10) % 3 == 0
39+
# Explicitly cast to object, to avoid implicit cast when setting np.nan
40+
df1["bool"] = (np.arange(10) % 3 == 0).astype(object)
4041
df1.loc[::2] = np.nan
4142
df2 = df1.copy()
4243
assert df1["text"].equals(df2["text"])

pandas/tests/frame/test_query_eval.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,8 @@ def test_date_index_query(self):
448448
def test_date_index_query_with_NaT(self):
449449
engine, parser = self.engine, self.parser
450450
n = 10
451-
df = DataFrame(np.random.randn(n, 3))
451+
# Cast to object to avoid implicit cast when setting entry to pd.NaT below
452+
df = DataFrame(np.random.randn(n, 3)).astype({0: object})
452453
df["dates1"] = date_range("1/1/2012", periods=n)
453454
df["dates3"] = date_range("1/1/2014", periods=n)
454455
df.iloc[0, 0] = pd.NaT
@@ -808,7 +809,8 @@ def test_date_index_query(self):
808809
def test_date_index_query_with_NaT(self):
809810
engine, parser = self.engine, self.parser
810811
n = 10
811-
df = DataFrame(np.random.randn(n, 3))
812+
# Cast to object to avoid implicit cast when setting entry to pd.NaT below
813+
df = DataFrame(np.random.randn(n, 3)).astype({0: object})
812814
df["dates1"] = date_range("1/1/2012", periods=n)
813815
df["dates3"] = date_range("1/1/2014", periods=n)
814816
df.iloc[0, 0] = pd.NaT

pandas/tests/frame/test_reductions.py

+4
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,14 @@ def test_var_std(self, datetime_frame):
449449
def test_numeric_only_flag(self, meth):
450450
# GH 9201
451451
df1 = DataFrame(np.random.randn(5, 3), columns=["foo", "bar", "baz"])
452+
# Cast to object to avoid implicit cast when setting entry to "100" below
453+
df1 = df1.astype({"foo": object})
452454
# set one entry to a number in str format
453455
df1.loc[0, "foo"] = "100"
454456

455457
df2 = DataFrame(np.random.randn(5, 3), columns=["foo", "bar", "baz"])
458+
# Cast to object to avoid implicit cast when setting entry to "a" below
459+
df2 = df2.astype({"foo": object})
456460
# set one entry to a non-number str
457461
df2.loc[0, "foo"] = "a"
458462

pandas/tests/groupby/test_timegrouper.py

+2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ def test_groupby_with_timegrouper(self):
103103
"20130901", "20131205", freq="5D", name="Date", inclusive="left"
104104
),
105105
)
106+
# Cast to object to avoid implicit cast when setting entry to "CarlCarlCarl"
107+
expected = expected.astype({"Buyer": object})
106108
expected.iloc[0, 0] = "CarlCarlCarl"
107109
expected.iloc[6, 0] = "CarlCarl"
108110
expected.iloc[18, 0] = "Joe"

pandas/tests/series/methods/test_replace.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ def test_replace_explicit_none(self):
1616
expected = pd.Series([0, 0, None], dtype=object)
1717
tm.assert_series_equal(result, expected)
1818

19-
df = pd.DataFrame(np.zeros((3, 3)))
19+
# Cast column 2 to object to avoid implicit cast when setting entry to ""
20+
df = pd.DataFrame(np.zeros((3, 3))).astype({2: object})
2021
df.iloc[2, 2] = ""
2122
result = df.replace("", None)
2223
expected = pd.DataFrame(

0 commit comments

Comments
 (0)