Skip to content

CLN avoid some upcasting when its not the purpose of the test #50493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pandas/tests/frame/methods/test_equals.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def test_equals(self):
df1["start"] = date_range("2000-1-1", periods=10, freq="T")
df1["end"] = date_range("2000-1-1", periods=10, freq="D")
df1["diff"] = df1["end"] - df1["start"]
df1["bool"] = np.arange(10) % 3 == 0
# Explicitly cast to object, to avoid implicit cast when setting np.nan
df1["bool"] = (np.arange(10) % 3 == 0).astype(object)
df1.loc[::2] = np.nan
df2 = df1.copy()
assert df1["text"].equals(df2["text"])
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/frame/test_query_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,8 @@ def test_date_index_query(self):
def test_date_index_query_with_NaT(self):
engine, parser = self.engine, self.parser
n = 10
df = DataFrame(np.random.randn(n, 3))
# Cast to object to avoid implicit cast when setting entry to pd.NaT below
df = DataFrame(np.random.randn(n, 3)).astype({0: object})
df["dates1"] = date_range("1/1/2012", periods=n)
df["dates3"] = date_range("1/1/2014", periods=n)
df.iloc[0, 0] = pd.NaT
Comment on lines +452 to 455
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here, df[0] is first created of int dtype, and then df.iloc[0, 0] = pd.NaT upcasts it to object. Might as well create it of dtype object in the first place, as the purpose of this test comes in the lines below (df.query(...)

Expand Down Expand Up @@ -808,7 +809,8 @@ def test_date_index_query(self):
def test_date_index_query_with_NaT(self):
engine, parser = self.engine, self.parser
n = 10
df = DataFrame(np.random.randn(n, 3))
# Cast to object to avoid implicit cast when setting entry to pd.NaT below
df = DataFrame(np.random.randn(n, 3)).astype({0: object})
df["dates1"] = date_range("1/1/2012", periods=n)
df["dates3"] = date_range("1/1/2014", periods=n)
df.iloc[0, 0] = pd.NaT
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/frame/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,14 @@ def test_var_std(self, datetime_frame):
def test_numeric_only_flag(self, meth):
# GH 9201
df1 = DataFrame(np.random.randn(5, 3), columns=["foo", "bar", "baz"])
# Cast to object to avoid implicit cast when setting entry to "100" below
df1 = df1.astype({"foo": object})
# set one entry to a number in str format
df1.loc[0, "foo"] = "100"

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

Expand Down
2 changes: 2 additions & 0 deletions pandas/tests/groupby/test_timegrouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def test_groupby_with_timegrouper(self):
"20130901", "20131205", freq="5D", name="Date", inclusive="left"
),
)
# Cast to object to avoid implicit cast when setting entry to "CarlCarlCarl"
expected = expected.astype({"Buyer": object})
expected.iloc[0, 0] = "CarlCarlCarl"
expected.iloc[6, 0] = "CarlCarl"
expected.iloc[18, 0] = "Joe"
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/series/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def test_replace_explicit_none(self):
expected = pd.Series([0, 0, None], dtype=object)
tm.assert_series_equal(result, expected)

df = pd.DataFrame(np.zeros((3, 3)))
# Cast column 2 to object to avoid implicit cast when setting entry to ""
df = pd.DataFrame(np.zeros((3, 3))).astype({2: object})
df.iloc[2, 2] = ""
result = df.replace("", None)
expected = pd.DataFrame(
Expand Down