Skip to content

TST: clean-up various tests avoiding CoW issues #55861

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 1 commit into from
Nov 7, 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/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,11 +747,11 @@ def test_getitem_setitem_float_labels(self, using_array_manager):
expected = df.iloc[0:2]
tm.assert_frame_equal(result, expected)

df.loc[1:2] = 0
expected = df.iloc[0:2]
msg = r"The behavior of obj\[i:j\] with a float-dtype index"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = df[1:2]
assert (result == 0).all().all()
tm.assert_frame_equal(result, expected)
Comment on lines -750 to +754
Copy link
Member Author

Choose a reason for hiding this comment

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

No idea why we are setting values to 0 to then check that the indexing result also has values of 0. Can also use a plain assert_frame_equal, which seems simpler.


# #2727
index = Index([1.0, 2.5, 3.5, 4.5, 5.0])
Expand Down
6 changes: 2 additions & 4 deletions pandas/tests/frame/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,8 @@ def test_with_datetimelikes(self):

def test_deepcopy(self, float_frame):
cp = deepcopy(float_frame)
series = cp["A"]
series[:] = 10
for idx, value in series.items():
assert float_frame["A"][idx] != value
cp.loc[0, "A"] = 10
assert not float_frame.equals(cp)
Comment on lines -220 to +221
Copy link
Member Author

Choose a reason for hiding this comment

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

This seems a tedious way to check that cp is an actual copy (getting a column, mutating that column, and then iterating through column values), while we can also use a simpler direct mutation and single assert


def test_inplace_return_self(self):
# GH 1893
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/frame/test_nonunique_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,15 @@ def test_set_value_by_index(self):

df = DataFrame(np.arange(9).reshape(3, 3).T)
df.columns = list("AAA")
expected = df.iloc[:, 2]
expected = df.iloc[:, 2].copy()
Copy link
Member Author

Choose a reason for hiding this comment

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

Copying those "expected" frames avoids any potential issues where expected might be modified(or not) through what is actually being tested


with tm.assert_produces_warning(warn, match=msg):
df.iloc[:, 0] = 3
tm.assert_series_equal(df.iloc[:, 2], expected)

df = DataFrame(np.arange(9).reshape(3, 3).T)
df.columns = [2, float(2), str(2)]
expected = df.iloc[:, 1]
expected = df.iloc[:, 1].copy()

with tm.assert_produces_warning(warn, match=msg):
df.iloc[:, 0] = 3
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexing/test_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_frame_at_with_duplicate_axes(self):
df = DataFrame(arr, columns=["A", "A"])

result = df.at[0, "A"]
expected = df.iloc[0]
expected = df.iloc[0].copy()

tm.assert_series_equal(result, expected)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1746,7 +1746,7 @@ def test_setitem_with_bool_indexer():
# GH#42530

df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
result = df.pop("b")
result = df.pop("b").copy()
result[[True, False, False]] = 9
expected = Series(data=[9, 5, 6], name="b")
tm.assert_series_equal(result, expected)
Expand Down