Skip to content

Commit da1cb56

Browse files
jorisvandenbosschenoatamir
authored andcommitted
TST: avoid chained assignment in tests outside of specific tests on chaining (pandas-dev#49474)
* TST: avoid chained assignment in tests outside of specific tests on chaining * update to use .loc[index[slice], ..] pattern
1 parent 7b9de35 commit da1cb56

File tree

10 files changed

+45
-42
lines changed

10 files changed

+45
-42
lines changed

pandas/tests/apply/test_frame_apply.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def test_apply_yield_list(float_frame):
347347

348348

349349
def test_apply_reduce_Series(float_frame):
350-
float_frame["A"].iloc[::2] = np.nan
350+
float_frame.iloc[::2, float_frame.columns.get_loc("A")] = np.nan
351351
expected = float_frame.mean(1)
352352
result = float_frame.apply(np.mean, axis=1)
353353
tm.assert_series_equal(result, expected)

pandas/tests/frame/indexing/test_setitem.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ def test_setitem_always_copy(self, float_frame):
11321132
s = float_frame["A"].copy()
11331133
float_frame["E"] = s
11341134

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

11381138
@pytest.mark.parametrize("consolidate", [True, False])

pandas/tests/frame/methods/test_cov_corr.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ class TestDataFrameCorr:
107107
@pytest.mark.parametrize("method", ["pearson", "kendall", "spearman"])
108108
@td.skip_if_no_scipy
109109
def test_corr_scipy_method(self, float_frame, method):
110-
float_frame["A"][:5] = np.nan
111-
float_frame["B"][5:10] = np.nan
112-
float_frame["A"][:10] = float_frame["A"][10:20]
110+
float_frame.loc[float_frame.index[:5], "A"] = np.nan
111+
float_frame.loc[float_frame.index[5:10], "B"] = np.nan
112+
float_frame.loc[float_frame.index[:10], "A"] = float_frame["A"][10:20]
113113

114114
correls = float_frame.corr(method=method)
115115
expected = float_frame["A"].corr(float_frame["C"], method=method)

pandas/tests/frame/methods/test_fillna.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -392,16 +392,16 @@ def test_fillna_datetime_columns(self):
392392
tm.assert_frame_equal(result, expected)
393393

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

398398
tm.assert_frame_equal(
399399
datetime_frame.ffill(), datetime_frame.fillna(method="ffill")
400400
)
401401

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

406406
tm.assert_frame_equal(
407407
datetime_frame.bfill(), datetime_frame.fillna(method="bfill")
@@ -467,8 +467,8 @@ def test_fillna_integer_limit(self, type):
467467

468468
def test_fillna_inplace(self):
469469
df = DataFrame(np.random.randn(10, 4))
470-
df[1][:4] = np.nan
471-
df[3][-4:] = np.nan
470+
df.loc[:4, 1] = np.nan
471+
df.loc[-4:, 3] = np.nan
472472

473473
expected = df.fillna(value=0)
474474
assert expected is not df
@@ -479,8 +479,8 @@ def test_fillna_inplace(self):
479479
expected = df.fillna(value={0: 0}, inplace=True)
480480
assert expected is None
481481

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

pandas/tests/frame/methods/test_rank.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ def test_rank(self, float_frame):
4343
import scipy.stats # noqa:F401
4444
from scipy.stats import rankdata
4545

46-
float_frame["A"][::2] = np.nan
47-
float_frame["B"][::3] = np.nan
48-
float_frame["C"][::4] = np.nan
49-
float_frame["D"][::5] = np.nan
46+
float_frame.loc[::2, "A"] = np.nan
47+
float_frame.loc[::3, "B"] = np.nan
48+
float_frame.loc[::4, "C"] = np.nan
49+
float_frame.loc[::5, "D"] = np.nan
5050

5151
ranks0 = float_frame.rank()
5252
ranks1 = float_frame.rank(1)
@@ -148,10 +148,10 @@ def test_rank_na_option(self, float_frame):
148148
import scipy.stats # noqa:F401
149149
from scipy.stats import rankdata
150150

151-
float_frame["A"][::2] = np.nan
152-
float_frame["B"][::3] = np.nan
153-
float_frame["C"][::4] = np.nan
154-
float_frame["D"][::5] = np.nan
151+
float_frame.loc[::2, "A"] = np.nan
152+
float_frame.loc[::3, "B"] = np.nan
153+
float_frame.loc[::4, "C"] = np.nan
154+
float_frame.loc[::5, "D"] = np.nan
155155

156156
# bottom
157157
ranks0 = float_frame.rank(na_option="bottom")

pandas/tests/frame/methods/test_replace.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ def mix_abc() -> dict[str, list[float | str]]:
2929

3030
class TestDataFrameReplace:
3131
def test_replace_inplace(self, datetime_frame, float_string_frame):
32-
datetime_frame["A"][:5] = np.nan
33-
datetime_frame["A"][-5:] = np.nan
32+
datetime_frame.loc[datetime_frame.index[:5], "A"] = np.nan
33+
datetime_frame.loc[datetime_frame.index[-5:], "A"] = np.nan
3434

3535
tsframe = datetime_frame.copy()
3636
return_value = tsframe.replace(np.nan, 0, inplace=True)
@@ -420,16 +420,16 @@ def test_regex_replace_string_types(
420420
tm.assert_equal(result, expected)
421421

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

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

430-
datetime_frame["A"][:5] = np.nan
431-
datetime_frame["A"][-5:] = np.nan
432-
datetime_frame["B"][:5] = -1e8
430+
datetime_frame.loc[datetime_frame.index[:5], "A"] = np.nan
431+
datetime_frame.loc[datetime_frame.index[-5:], "A"] = np.nan
432+
datetime_frame.loc[datetime_frame.index[:5], "B"] = -1e8
433433

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

717717
# dtypes
718718
tsframe = datetime_frame.copy().astype(np.float32)
719-
tsframe["A"][:5] = np.nan
720-
tsframe["A"][-5:] = np.nan
719+
tsframe.loc[tsframe.index[:5], "A"] = np.nan
720+
tsframe.loc[tsframe.index[-5:], "A"] = np.nan
721721

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

726-
tsframe["A"][:5] = np.nan
727-
tsframe["A"][-5:] = np.nan
728-
tsframe["B"][:5] = -1e8
726+
tsframe.loc[tsframe.index[:5], "A"] = np.nan
727+
tsframe.loc[tsframe.index[-5:], "A"] = np.nan
728+
tsframe.loc[tsframe.index[:5], "B"] = -1e8
729729

730730
b = tsframe["B"]
731731
b[b == -1e8] = np.nan

pandas/tests/frame/methods/test_to_csv.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def read_csv(self, path, **kwargs):
3535
def test_to_csv_from_csv1(self, float_frame, datetime_frame):
3636

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

4040
float_frame.to_csv(path)
4141
float_frame.to_csv(path, columns=["A", "B"])

pandas/tests/frame/test_constructors.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,8 @@ def test_constructor_defaultdict(self, float_frame):
718718
from collections import defaultdict
719719

720720
data = {}
721-
float_frame["B"][:10] = np.nan
721+
float_frame.loc[: float_frame.index[10], "B"] = np.nan
722+
722723
for k, v in float_frame.items():
723724
dct = defaultdict(dict)
724725
dct.update(v.to_dict())
@@ -2203,7 +2204,9 @@ def test_constructor_series_copy(self, float_frame):
22032204
series = float_frame._series
22042205

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

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

pandas/tests/io/excel/test_writers.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def test_excel_writer_context_manager(self, frame, path):
374374

375375
def test_roundtrip(self, frame, path):
376376
frame = frame.copy()
377-
frame["A"][:5] = np.nan
377+
frame.iloc[:5, frame.columns.get_loc("A")] = np.nan
378378

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

445445
def test_basics_with_nan(self, frame, path):
446446
frame = frame.copy()
447-
frame["A"][:5] = np.nan
447+
frame.iloc[:5, frame.columns.get_loc("A")] = np.nan
448448
frame.to_excel(path, "test1")
449449
frame.to_excel(path, "test1", columns=["A", "B"])
450450
frame.to_excel(path, "test1", header=False)
@@ -508,7 +508,7 @@ def test_sheets(self, frame, tsframe, path):
508508
tsframe.index = index
509509

510510
frame = frame.copy()
511-
frame["A"][:5] = np.nan
511+
frame.iloc[:5, frame.columns.get_loc("A")] = np.nan
512512

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

531531
def test_colaliases(self, frame, path):
532532
frame = frame.copy()
533-
frame["A"][:5] = np.nan
533+
frame.iloc[:5, frame.columns.get_loc("A")] = np.nan
534534

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

549549
def test_roundtrip_indexlabels(self, merge_cells, frame, path):
550550
frame = frame.copy()
551-
frame["A"][:5] = np.nan
551+
frame.iloc[:5, frame.columns.get_loc("A")] = np.nan
552552

553553
frame.to_excel(path, "test1")
554554
frame.to_excel(path, "test1", columns=["A", "B"])

pandas/tests/series/methods/test_rank.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test_rank(self, datetime_series):
4444
from scipy.stats import rankdata
4545

4646
datetime_series[::2] = np.nan
47-
datetime_series[:10][::3] = 4.0
47+
datetime_series[:10:3] = 4.0
4848

4949
ranks = datetime_series.rank()
5050
oranks = datetime_series.astype("O").rank()

0 commit comments

Comments
 (0)