Skip to content

Commit 341b579

Browse files
TST: avoid mutating DataFrame.values in tests (use iloc instead) (#51301)
1 parent 14fc3de commit 341b579

File tree

10 files changed

+20
-18
lines changed

10 files changed

+20
-18
lines changed

pandas/_testing/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ def _gen_unique_rand(rng, _extra_size):
796796
def makeMissingDataframe(density: float = 0.9, random_state=None) -> DataFrame:
797797
df = makeDataFrame()
798798
i, j = _create_missing_idx(*df.shape, density=density, random_state=random_state)
799-
df.values[i, j] = np.nan
799+
df.iloc[i, j] = np.nan
800800
return df
801801

802802

pandas/tests/frame/indexing/test_indexing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ def test_setitem_fancy_scalar(self, float_frame):
620620
for idx in f.index[::5]:
621621
i = f.index.get_loc(idx)
622622
val = np.random.randn()
623-
expected.values[i, j] = val
623+
expected.iloc[i, j] = val
624624

625625
ix[idx, col] = val
626626
tm.assert_frame_equal(f, expected)

pandas/tests/frame/methods/test_cov_corr.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,8 @@ def test_corr_item_cache(self, using_copy_on_write):
218218
_ = df.corr(numeric_only=True)
219219

220220
if using_copy_on_write:
221-
# TODO(CoW) we should disallow this, so `df` doesn't get updated
222-
ser.values[0] = 99
223-
assert df.loc[0, "A"] == 99
221+
ser.iloc[0] = 99
222+
assert df.loc[0, "A"] == 0
224223
else:
225224
# Check that the corr didn't break link between ser and df
226225
ser.values[0] = 99

pandas/tests/frame/methods/test_fillna.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,9 @@ def test_fillna_dataframe(self):
550550
tm.assert_frame_equal(result, expected)
551551

552552
def test_fillna_columns(self):
553-
df = DataFrame(np.random.randn(10, 10))
554-
df.values[:, ::2] = np.nan
553+
arr = np.random.randn(10, 10)
554+
arr[:, ::2] = np.nan
555+
df = DataFrame(arr)
555556

556557
result = df.fillna(method="ffill", axis=1)
557558
expected = df.T.fillna(method="pad").T

pandas/tests/frame/test_constructors.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,13 +2111,14 @@ def test_constructor_frame_shallow_copy(self, float_frame):
21112111

21122112
def test_constructor_ndarray_copy(self, float_frame, using_array_manager):
21132113
if not using_array_manager:
2114-
df = DataFrame(float_frame.values)
2114+
arr = float_frame.values.copy()
2115+
df = DataFrame(arr)
21152116

2116-
float_frame.values[5] = 5
2117+
arr[5] = 5
21172118
assert (df.values[5] == 5).all()
21182119

2119-
df = DataFrame(float_frame.values, copy=True)
2120-
float_frame.values[6] = 6
2120+
df = DataFrame(arr, copy=True)
2121+
arr[6] = 6
21212122
assert not (df.values[6] == 6).all()
21222123
else:
21232124
arr = float_frame.values.copy()

pandas/tests/groupby/test_function.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,9 @@ def test_cython_api2():
356356

357357

358358
def test_cython_median():
359-
df = DataFrame(np.random.randn(1000))
360-
df.values[::2] = np.nan
359+
arr = np.random.randn(1000)
360+
arr[::2] = np.nan
361+
df = DataFrame(arr)
361362

362363
labels = np.random.randint(0, 50, size=1000).astype(float)
363364
labels[::17] = np.nan

pandas/tests/indexing/multiindex/test_slice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ def test_int_series_slicing(self, multiindex_year_month_day_dataframe_random_dat
750750

751751
exp = ymd["A"].copy()
752752
s[5:] = 0
753-
exp.values[5:] = 0
753+
exp.iloc[5:] = 0
754754
tm.assert_numpy_array_equal(s.values, exp.values)
755755

756756
result = ymd[5:]

pandas/tests/io/pytables/test_round_trip.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,8 @@ def test_frame(compression, setup_path):
372372
df = tm.makeDataFrame()
373373

374374
# put in some random NAs
375-
df.values[0, 0] = np.nan
376-
df.values[5, 3] = np.nan
375+
df.iloc[0, 0] = np.nan
376+
df.iloc[5, 3] = np.nan
377377

378378
_check_roundtrip_table(
379379
df, tm.assert_frame_equal, path=setup_path, compression=compression

pandas/tests/series/indexing/test_setitem.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ def expected(self, dtype):
899899
arr = np.arange(5).astype(dtype)
900900
ser = Series(arr)
901901
ser = ser.astype(object)
902-
ser.values[0] = np.timedelta64(4, "ns")
902+
ser.iloc[0] = np.timedelta64(4, "ns")
903903
return ser
904904

905905
@pytest.fixture

pandas/tests/series/methods/test_fillna.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_fillna_nat(self):
3232
filled2 = series.fillna(value=series.values[2])
3333

3434
expected = series.copy()
35-
expected.values[3] = expected.values[2]
35+
expected.iloc[3] = expected.iloc[2]
3636

3737
tm.assert_series_equal(filled, expected)
3838
tm.assert_series_equal(filled2, expected)

0 commit comments

Comments
 (0)