Skip to content
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

CLN: avoid upcasting in tests where unnecessary (PDEP-6 precursor) #53104

Merged
merged 2 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 5 additions & 6 deletions pandas/tests/frame/indexing/test_where.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,14 @@ def test_where_bug_mixed(self, any_signed_int_numpy_dtype):
)

expected = DataFrame(
{"a": [np.nan, np.nan, 3.0, 4.0], "b": [4.0, 3.0, np.nan, np.nan]},
Copy link
Member Author

Choose a reason for hiding this comment

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

changing this test to fill with -1 instead of with nan

Copy link
Member

Choose a reason for hiding this comment

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

just checking: you looked at the original issue and confirmed this still tests the relevant bug?

Copy link
Member Author

@MarcoGorelli MarcoGorelli May 5, 2023

Choose a reason for hiding this comment

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

yeah the original issue is just about having mixed dtypes #2793 , I've preserved that but have just updated the fill value to one which won't upcast


I think I'm scraping the barrel here with these precursors, there's probably not much more to factor out which doesn't intentionally test upcasting behaviour

dtype="float64",
)
{"a": [-1, -1, 3, 4], "b": [4.0, 3.0, -1, -1]},
).astype({"a": any_signed_int_numpy_dtype, "b": "float64"})

result = df.where(df > 2, np.nan)
result = df.where(df > 2, -1)
tm.assert_frame_equal(result, expected)

result = df.copy()
return_value = result.where(result > 2, np.nan, inplace=True)
return_value = result.where(result > 2, -1, inplace=True)
assert return_value is None
tm.assert_frame_equal(result, expected)

Expand Down Expand Up @@ -1028,7 +1027,7 @@ def test_where_int_overflow(replacement):

def test_where_inplace_no_other():
# GH#51685
df = DataFrame({"a": [1, 2], "b": ["x", "y"]})
df = DataFrame({"a": [1.0, 2.0], "b": ["x", "y"]})
cond = DataFrame({"a": [True, False], "b": [False, True]})
df.where(cond, inplace=True)
expected = DataFrame({"a": [1, np.nan], "b": [np.nan, "y"]})
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,9 @@ def test_multi_assign(self):
"col1": list(range(6)),
"col2": list(range(6, 12)),
}
)
).astype(
{"col2": "float64"}
) # set float64 to avoid upcast when setting nan
df.iloc[1, 0] = np.nan
df2 = df.copy()

Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,8 @@ def test_loc_setitem_with_scalar_index(self, indexer, value):
# assigning like "df.loc[0, ['A']] = ['Z']" should be evaluated
# elementwisely, not using "setter('A', ['Z'])".

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

Expand Down Expand Up @@ -1524,7 +1525,8 @@ def test_loc_setitem_td64_non_nano(self):

def test_loc_setitem_2d_to_1d_raises(self):
data = np.random.randn(2, 2)
ser = Series(range(2))
# float64 dtype to avoid upcast when trying to set float data
ser = Series(range(2), dtype="float64")

msg = "|".join(
[
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/interchange/test_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def test_missing_from_masked():
{
"x": np.array([1.0, 2.0, 3.0, 4.0, 0.0]),
"y": np.array([1.5, 2.5, 3.5, 4.5, 0]),
"z": np.array([True, False, True, True, True]),
"z": np.array([1.0, 0.0, 1.0, 1.0, 1.0]),
}
)

Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/series/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def test_setitem_multiindex_empty_slice(self):

def test_setitem_with_string_index(self):
# GH#23451
ser = Series([1, 2, 3], index=["Date", "b", "other"])
# Set object dtype to avoid upcast when setting date.today()
ser = Series([1, 2, 3], index=["Date", "b", "other"], dtype=object)
ser["Date"] = date.today()
assert ser.Date == date.today()
assert ser["Date"] == date.today()
Expand Down Expand Up @@ -459,7 +460,8 @@ def test_setitem_callable_other(self):
# GH#13299
inc = lambda x: x + 1

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

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