Skip to content

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

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 3 commits into from
May 4, 2023
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
4 changes: 2 additions & 2 deletions pandas/tests/arrays/masked_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ def _compare_other(self, data, op, other):
ser = pd.Series(data)
result = op(ser, other)

expected = op(pd.Series(data._data), other)
# Set nullable dtype here to avoid upcasting when setting to pd.NA below
expected = op(pd.Series(data._data), other).astype("boolean")

# fill the nan locations
expected[data._mask] = pd.NA
expected = expected.astype("boolean")

tm.assert_series_equal(result, expected)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/indexing/test_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def test_mask_return_dtype():

def test_mask_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.mask(cond, inplace=True)
expected = DataFrame({"a": [np.nan, 2], "b": ["x", np.nan]})
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,8 @@ def test_setitem_other_callable(self):
def inc(x):
return x + 1

df = DataFrame([[-1, 1], [1, -1]])
# Set dtype object straight away to avoid upcast when setting inc below
df = DataFrame([[-1, 1], [1, -1]], dtype=object)
df[df > 0] = inc

expected = DataFrame([[-1, inc], [inc, -1]])
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/frame/methods/test_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ def test_missing(self, date_range_frame):
# GH 15118
# no match found - `where` value before earliest date in index
N = 10
df = date_range_frame.iloc[:N].copy()
# Cast to 'float64' to avoid upcast when introducing nan in df.asof
df = date_range_frame.iloc[:N].copy().astype("float64")

result = df.asof("1989-12-31")

Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/frame/methods/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ def test_map_na_ignore(float_frame):
strlen_frame_na_ignore = float_frame_with_na.map(
lambda x: len(str(x)), na_action="ignore"
)
strlen_frame_with_na = strlen_frame.copy()
# Set float64 type to avoid upcast when setting NA below
strlen_frame_with_na = strlen_frame.copy().astype("float64")
strlen_frame_with_na[mask] = pd.NA
tm.assert_frame_equal(strlen_frame_na_ignore, strlen_frame_with_na)

Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/groupby/test_counting.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,10 @@ def test_count_object():

def test_count_cross_type():
# GH8169
# Set float64 dtype to avoid upcast when setting nan below
vals = np.hstack(
(np.random.randint(0, 5, (100, 2)), np.random.randint(0, 2, (100, 2)))
)
).astype("float64")

df = DataFrame(vals, columns=["a", "b", "c", "d"])
df[df == 2] = np.nan
Expand Down
15 changes: 10 additions & 5 deletions pandas/tests/groupby/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,8 @@ def test_filter_and_transform_with_non_unique_int_index():
tm.assert_frame_equal(actual, expected)

actual = grouped_df.filter(lambda x: len(x) > 1, dropna=False)
expected = df.copy()
# Cast to avoid upcast when setting nan below
expected = df.copy().astype("float64")
expected.iloc[[0, 3, 5, 6]] = np.nan
tm.assert_frame_equal(actual, expected)

Expand Down Expand Up @@ -405,7 +406,8 @@ def test_filter_and_transform_with_multiple_non_unique_int_index():
tm.assert_frame_equal(actual, expected)

actual = grouped_df.filter(lambda x: len(x) > 1, dropna=False)
expected = df.copy()
# Cast to avoid upcast when setting nan below
expected = df.copy().astype("float64")
expected.iloc[[0, 3, 5, 6]] = np.nan
tm.assert_frame_equal(actual, expected)

Expand Down Expand Up @@ -447,7 +449,8 @@ def test_filter_and_transform_with_non_unique_float_index():
tm.assert_frame_equal(actual, expected)

actual = grouped_df.filter(lambda x: len(x) > 1, dropna=False)
expected = df.copy()
# Cast to avoid upcast when setting nan below
expected = df.copy().astype("float64")
expected.iloc[[0, 3, 5, 6]] = np.nan
tm.assert_frame_equal(actual, expected)

Expand Down Expand Up @@ -492,7 +495,8 @@ def test_filter_and_transform_with_non_unique_timestamp_index():
tm.assert_frame_equal(actual, expected)

actual = grouped_df.filter(lambda x: len(x) > 1, dropna=False)
expected = df.copy()
# Cast to avoid upcast when setting nan below
expected = df.copy().astype("float64")
expected.iloc[[0, 3, 5, 6]] = np.nan
tm.assert_frame_equal(actual, expected)

Expand Down Expand Up @@ -534,7 +538,8 @@ def test_filter_and_transform_with_non_unique_string_index():
tm.assert_frame_equal(actual, expected)

actual = grouped_df.filter(lambda x: len(x) > 1, dropna=False)
expected = df.copy()
# Cast to avoid upcast when setting nan below
expected = df.copy().astype("float64")
expected.iloc[[0, 3, 5, 6]] = np.nan
tm.assert_frame_equal(actual, expected)

Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ def f_1(grp):
return grp.iloc[0]

result = df.groupby("A").apply(f_1)[["B"]]
e = expected.copy()
# Cast to avoid upcast when setting nan below
e = expected.copy().astype("float64")
e.loc["Tiger"] = np.nan
tm.assert_frame_equal(result, e)

Expand Down