Skip to content

TST: avoid mutating DataFrame.values in tests (use iloc instead) #51301

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
Feb 11, 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
2 changes: 1 addition & 1 deletion pandas/_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ def _gen_unique_rand(rng, _extra_size):
def makeMissingDataframe(density: float = 0.9, random_state=None) -> DataFrame:
df = makeDataFrame()
i, j = _create_missing_idx(*df.shape, density=density, random_state=random_state)
df.values[i, j] = np.nan
df.iloc[i, j] = np.nan
return df


Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ def test_setitem_fancy_scalar(self, float_frame):
for idx in f.index[::5]:
i = f.index.get_loc(idx)
val = np.random.randn()
expected.values[i, j] = val
expected.iloc[i, j] = val

ix[idx, col] = val
tm.assert_frame_equal(f, expected)
Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/frame/methods/test_cov_corr.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,8 @@ def test_corr_item_cache(self, using_copy_on_write):
_ = df.corr(numeric_only=True)

if using_copy_on_write:
# TODO(CoW) we should disallow this, so `df` doesn't get updated
ser.values[0] = 99
assert df.loc[0, "A"] == 99
ser.iloc[0] = 99
assert df.loc[0, "A"] == 0
else:
# Check that the corr didn't break link between ser and df
ser.values[0] = 99
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/frame/methods/test_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,9 @@ def test_fillna_dataframe(self):
tm.assert_frame_equal(result, expected)

def test_fillna_columns(self):
df = DataFrame(np.random.randn(10, 10))
df.values[:, ::2] = np.nan
arr = np.random.randn(10, 10)
arr[:, ::2] = np.nan
df = DataFrame(arr)

result = df.fillna(method="ffill", axis=1)
expected = df.T.fillna(method="pad").T
Expand Down
9 changes: 5 additions & 4 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2102,13 +2102,14 @@ def test_constructor_frame_copy(self, float_frame):

def test_constructor_ndarray_copy(self, float_frame, using_array_manager):
if not using_array_manager:
df = DataFrame(float_frame.values)
arr = float_frame.values.copy()
df = DataFrame(arr)

float_frame.values[5] = 5
arr[5] = 5
assert (df.values[5] == 5).all()

df = DataFrame(float_frame.values, copy=True)
float_frame.values[6] = 6
df = DataFrame(arr, copy=True)
arr[6] = 6
assert not (df.values[6] == 6).all()
else:
arr = float_frame.values.copy()
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/groupby/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,9 @@ def test_cython_api2():


def test_cython_median():
df = DataFrame(np.random.randn(1000))
df.values[::2] = np.nan
arr = np.random.randn(1000)
arr[::2] = np.nan
df = DataFrame(arr)

labels = np.random.randint(0, 50, size=1000).astype(float)
labels[::17] = np.nan
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexing/multiindex/test_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ def test_int_series_slicing(self, multiindex_year_month_day_dataframe_random_dat

exp = ymd["A"].copy()
s[5:] = 0
exp.values[5:] = 0
exp.iloc[5:] = 0
tm.assert_numpy_array_equal(s.values, exp.values)

result = ymd[5:]
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/io/pytables/test_round_trip.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,8 @@ def test_frame(compression, setup_path):
df = tm.makeDataFrame()

# put in some random NAs
df.values[0, 0] = np.nan
df.values[5, 3] = np.nan
df.iloc[0, 0] = np.nan
df.iloc[5, 3] = np.nan

_check_roundtrip_table(
df, tm.assert_frame_equal, path=setup_path, compression=compression
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 @@ -899,7 +899,7 @@ def expected(self, dtype):
arr = np.arange(5).astype(dtype)
ser = Series(arr)
ser = ser.astype(object)
ser.values[0] = np.timedelta64(4, "ns")
ser.iloc[0] = np.timedelta64(4, "ns")
return ser

@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/methods/test_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_fillna_nat(self):
filled2 = series.fillna(value=series.values[2])

expected = series.copy()
expected.values[3] = expected.values[2]
expected.iloc[3] = expected.iloc[2]

tm.assert_series_equal(filled, expected)
tm.assert_series_equal(filled2, expected)
Expand Down