Skip to content

TST: avoid chained assignment in tests outside of specific tests on chaining #49474

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
2 changes: 1 addition & 1 deletion pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def test_apply_yield_list(float_frame):


def test_apply_reduce_Series(float_frame):
float_frame["A"].iloc[::2] = np.nan
float_frame.iloc[::2, float_frame.columns.get_loc("A")] = np.nan
expected = float_frame.mean(1)
result = float_frame.apply(np.mean, axis=1)
tm.assert_series_equal(result, expected)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ def test_setitem_always_copy(self, float_frame):
s = float_frame["A"].copy()
float_frame["E"] = s

float_frame["E"][5:10] = np.nan
float_frame.iloc[5:10, float_frame.columns.get_loc("E")] = np.nan
assert notna(s[5:10]).all()

@pytest.mark.parametrize("consolidate", [True, False])
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/frame/methods/test_cov_corr.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ class TestDataFrameCorr:
@pytest.mark.parametrize("method", ["pearson", "kendall", "spearman"])
@td.skip_if_no_scipy
def test_corr_scipy_method(self, float_frame, method):
float_frame["A"][:5] = np.nan
float_frame["B"][5:10] = np.nan
float_frame["A"][:10] = float_frame["A"][10:20]
float_frame.loc[float_frame.index[:5], "A"] = np.nan
float_frame.loc[float_frame.index[5:10], "B"] = np.nan
float_frame.loc[float_frame.index[:10], "A"] = float_frame["A"][10:20]

correls = float_frame.corr(method=method)
expected = float_frame["A"].corr(float_frame["C"], method=method)
Expand Down
16 changes: 8 additions & 8 deletions pandas/tests/frame/methods/test_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,16 +392,16 @@ def test_fillna_datetime_columns(self):
tm.assert_frame_equal(result, expected)

def test_ffill(self, datetime_frame):
datetime_frame["A"][:5] = np.nan
datetime_frame["A"][-5:] = np.nan
datetime_frame.loc[datetime_frame.index[:5], "A"] = np.nan
datetime_frame.loc[datetime_frame.index[-5:], "A"] = np.nan

tm.assert_frame_equal(
datetime_frame.ffill(), datetime_frame.fillna(method="ffill")
)

def test_bfill(self, datetime_frame):
datetime_frame["A"][:5] = np.nan
datetime_frame["A"][-5:] = np.nan
datetime_frame.loc[datetime_frame.index[:5], "A"] = np.nan
datetime_frame.loc[datetime_frame.index[-5:], "A"] = np.nan

tm.assert_frame_equal(
datetime_frame.bfill(), datetime_frame.fillna(method="bfill")
Expand Down Expand Up @@ -467,8 +467,8 @@ def test_fillna_integer_limit(self, type):

def test_fillna_inplace(self):
df = DataFrame(np.random.randn(10, 4))
df[1][:4] = np.nan
df[3][-4:] = np.nan
df.loc[:4, 1] = np.nan
df.loc[-4:, 3] = np.nan

expected = df.fillna(value=0)
assert expected is not df
Expand All @@ -479,8 +479,8 @@ def test_fillna_inplace(self):
expected = df.fillna(value={0: 0}, inplace=True)
assert expected is None

df[1][:4] = np.nan
df[3][-4:] = np.nan
df.loc[:4, 1] = np.nan
df.loc[-4:, 3] = np.nan
expected = df.fillna(method="ffill")
assert expected is not df

Expand Down
16 changes: 8 additions & 8 deletions pandas/tests/frame/methods/test_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ def test_rank(self, float_frame):
import scipy.stats # noqa:F401
from scipy.stats import rankdata

float_frame["A"][::2] = np.nan
float_frame["B"][::3] = np.nan
float_frame["C"][::4] = np.nan
float_frame["D"][::5] = np.nan
float_frame.loc[::2, "A"] = np.nan
float_frame.loc[::3, "B"] = np.nan
float_frame.loc[::4, "C"] = np.nan
float_frame.loc[::5, "D"] = np.nan

ranks0 = float_frame.rank()
ranks1 = float_frame.rank(1)
Expand Down Expand Up @@ -148,10 +148,10 @@ def test_rank_na_option(self, float_frame):
import scipy.stats # noqa:F401
from scipy.stats import rankdata

float_frame["A"][::2] = np.nan
float_frame["B"][::3] = np.nan
float_frame["C"][::4] = np.nan
float_frame["D"][::5] = np.nan
float_frame.loc[::2, "A"] = np.nan
float_frame.loc[::3, "B"] = np.nan
float_frame.loc[::4, "C"] = np.nan
float_frame.loc[::5, "D"] = np.nan

# bottom
ranks0 = float_frame.rank(na_option="bottom")
Expand Down
24 changes: 12 additions & 12 deletions pandas/tests/frame/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def mix_abc() -> dict[str, list[float | str]]:

class TestDataFrameReplace:
def test_replace_inplace(self, datetime_frame, float_string_frame):
datetime_frame["A"][:5] = np.nan
datetime_frame["A"][-5:] = np.nan
datetime_frame.loc[datetime_frame.index[:5], "A"] = np.nan
datetime_frame.loc[datetime_frame.index[-5:], "A"] = np.nan

tsframe = datetime_frame.copy()
return_value = tsframe.replace(np.nan, 0, inplace=True)
Expand Down Expand Up @@ -420,16 +420,16 @@ def test_regex_replace_string_types(
tm.assert_equal(result, expected)

def test_replace(self, datetime_frame):
datetime_frame["A"][:5] = np.nan
datetime_frame["A"][-5:] = np.nan
datetime_frame.loc[datetime_frame.index[:5], "A"] = np.nan
datetime_frame.loc[datetime_frame.index[-5:], "A"] = np.nan

zero_filled = datetime_frame.replace(np.nan, -1e8)
tm.assert_frame_equal(zero_filled, datetime_frame.fillna(-1e8))
tm.assert_frame_equal(zero_filled.replace(-1e8, np.nan), datetime_frame)

datetime_frame["A"][:5] = np.nan
datetime_frame["A"][-5:] = np.nan
datetime_frame["B"][:5] = -1e8
datetime_frame.loc[datetime_frame.index[:5], "A"] = np.nan
datetime_frame.loc[datetime_frame.index[-5:], "A"] = np.nan
datetime_frame.loc[datetime_frame.index[:5], "B"] = -1e8

# empty
df = DataFrame(index=["a", "b"])
Expand Down Expand Up @@ -716,16 +716,16 @@ def test_replace_for_new_dtypes(self, datetime_frame):

# dtypes
tsframe = datetime_frame.copy().astype(np.float32)
tsframe["A"][:5] = np.nan
tsframe["A"][-5:] = np.nan
tsframe.loc[tsframe.index[:5], "A"] = np.nan
tsframe.loc[tsframe.index[-5:], "A"] = np.nan

zero_filled = tsframe.replace(np.nan, -1e8)
tm.assert_frame_equal(zero_filled, tsframe.fillna(-1e8))
tm.assert_frame_equal(zero_filled.replace(-1e8, np.nan), tsframe)

tsframe["A"][:5] = np.nan
tsframe["A"][-5:] = np.nan
tsframe["B"][:5] = -1e8
tsframe.loc[tsframe.index[:5], "A"] = np.nan
tsframe.loc[tsframe.index[-5:], "A"] = np.nan
tsframe.loc[tsframe.index[:5], "B"] = -1e8

b = tsframe["B"]
b[b == -1e8] = np.nan
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/methods/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def read_csv(self, path, **kwargs):
def test_to_csv_from_csv1(self, float_frame, datetime_frame):

with tm.ensure_clean("__tmp_to_csv_from_csv1__") as path:
float_frame["A"][:5] = np.nan
float_frame.iloc[:5, float_frame.columns.get_loc("A")] = np.nan

float_frame.to_csv(path)
float_frame.to_csv(path, columns=["A", "B"])
Expand Down
7 changes: 5 additions & 2 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,8 @@ def test_constructor_defaultdict(self, float_frame):
from collections import defaultdict

data = {}
float_frame["B"][:10] = np.nan
float_frame.loc[: float_frame.index[10], "B"] = np.nan

for k, v in float_frame.items():
dct = defaultdict(dict)
dct.update(v.to_dict())
Expand Down Expand Up @@ -2203,7 +2204,9 @@ def test_constructor_series_copy(self, float_frame):
series = float_frame._series

df = DataFrame({"A": series["A"]}, copy=True)
df["A"][:] = 5
# TODO can be replaced with `df.loc[:, "A"] = 5` after deprecation about
# inplace mutation is enforced
df.loc[df.index[0] : df.index[-1], "A"] = 5

assert not (series["A"] == 5).all()

Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def test_excel_writer_context_manager(self, frame, path):

def test_roundtrip(self, frame, path):
frame = frame.copy()
frame["A"][:5] = np.nan
frame.iloc[:5, frame.columns.get_loc("A")] = np.nan

frame.to_excel(path, "test1")
frame.to_excel(path, "test1", columns=["A", "B"])
Expand Down Expand Up @@ -444,7 +444,7 @@ def test_ts_frame(self, tsframe, path):

def test_basics_with_nan(self, frame, path):
frame = frame.copy()
frame["A"][:5] = np.nan
frame.iloc[:5, frame.columns.get_loc("A")] = np.nan
frame.to_excel(path, "test1")
frame.to_excel(path, "test1", columns=["A", "B"])
frame.to_excel(path, "test1", header=False)
Expand Down Expand Up @@ -508,7 +508,7 @@ def test_sheets(self, frame, tsframe, path):
tsframe.index = index

frame = frame.copy()
frame["A"][:5] = np.nan
frame.iloc[:5, frame.columns.get_loc("A")] = np.nan

frame.to_excel(path, "test1")
frame.to_excel(path, "test1", columns=["A", "B"])
Expand All @@ -530,7 +530,7 @@ def test_sheets(self, frame, tsframe, path):

def test_colaliases(self, frame, path):
frame = frame.copy()
frame["A"][:5] = np.nan
frame.iloc[:5, frame.columns.get_loc("A")] = np.nan

frame.to_excel(path, "test1")
frame.to_excel(path, "test1", columns=["A", "B"])
Expand All @@ -548,7 +548,7 @@ def test_colaliases(self, frame, path):

def test_roundtrip_indexlabels(self, merge_cells, frame, path):
frame = frame.copy()
frame["A"][:5] = np.nan
frame.iloc[:5, frame.columns.get_loc("A")] = np.nan

frame.to_excel(path, "test1")
frame.to_excel(path, "test1", columns=["A", "B"])
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/methods/test_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_rank(self, datetime_series):
from scipy.stats import rankdata

datetime_series[::2] = np.nan
datetime_series[:10][::3] = 4.0
datetime_series[:10:3] = 4.0

ranks = datetime_series.rank()
oranks = datetime_series.astype("O").rank()
Expand Down