Skip to content

Commit 5641979

Browse files
authored
TST: Reduce deep copies in the test suite (#62970)
1 parent ebbd16c commit 5641979

File tree

7 files changed

+15
-21
lines changed

7 files changed

+15
-21
lines changed

pandas/conftest.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ def index(request):
722722
- ...
723723
"""
724724
# copy to avoid mutation, e.g. setting .name
725-
return indices_dict[request.param].copy()
725+
return indices_dict[request.param].copy(deep=False)
726726

727727

728728
@pytest.fixture(
@@ -735,7 +735,7 @@ def index_flat(request):
735735
index fixture, but excluding MultiIndex cases.
736736
"""
737737
key = request.param
738-
return indices_dict[key].copy()
738+
return indices_dict[key].copy(deep=False)
739739

740740

741741
@pytest.fixture(
@@ -758,18 +758,15 @@ def index_with_missing(request):
758758
759759
MultiIndex is excluded because isna() is not defined for MultiIndex.
760760
"""
761-
762-
# GH 35538. Use deep copy to avoid illusive bug on np-dev
763-
# GHA pipeline that writes into indices_dict despite copy
764-
ind = indices_dict[request.param].copy(deep=True)
765-
vals = ind.values.copy()
761+
ind = indices_dict[request.param]
766762
if request.param in ["tuples", "mi-with-dt64tz-level", "multi"]:
767763
# For setting missing values in the top level of MultiIndex
768764
vals = ind.tolist()
769765
vals[0] = (None,) + vals[0][1:]
770766
vals[-1] = (None,) + vals[-1][1:]
771767
return MultiIndex.from_tuples(vals)
772768
else:
769+
vals = ind.values.copy()
773770
vals[0] = None
774771
vals[-1] = None
775772
return type(ind)(vals)
@@ -850,7 +847,7 @@ def index_or_series_obj(request):
850847
Fixture for tests on indexes, series and series with a narrow dtype
851848
copy to avoid mutation, e.g. setting .name
852849
"""
853-
return _index_or_series_objs[request.param].copy(deep=True)
850+
return _index_or_series_objs[request.param].copy(deep=False)
854851

855852

856853
_typ_objects_series = {
@@ -873,7 +870,7 @@ def index_or_series_memory_obj(request):
873870
series with empty objects type
874871
copy to avoid mutation, e.g. setting .name
875872
"""
876-
return _index_or_series_memory_objs[request.param].copy(deep=True)
873+
return _index_or_series_memory_objs[request.param].copy(deep=False)
877874

878875

879876
# ----------------------------------------------------------------

pandas/tests/arrays/interval/test_interval.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,7 @@ def test_shift_datetime(self):
115115
class TestSetitem:
116116
def test_set_na(self, left_right_dtypes):
117117
left, right = left_right_dtypes
118-
left = left.copy(deep=True)
119-
right = right.copy(deep=True)
120-
result = IntervalArray.from_arrays(left, right)
118+
result = IntervalArray.from_arrays(left, right, copy=True)
121119

122120
if result.dtype.subtype.kind not in ["m", "M"]:
123121
msg = "'value' should be an interval type, got <.*NaTType'> instead."
@@ -168,8 +166,6 @@ def test_setitem_mismatched_closed(self):
168166
class TestReductions:
169167
def test_min_max_invalid_axis(self, left_right_dtypes):
170168
left, right = left_right_dtypes
171-
left = left.copy(deep=True)
172-
right = right.copy(deep=True)
173169
arr = IntervalArray.from_arrays(left, right)
174170

175171
msg = "`axis` must be fewer than the number of dimensions"
@@ -188,8 +184,6 @@ def test_min_max_invalid_axis(self, left_right_dtypes):
188184
def test_min_max(self, left_right_dtypes, index_or_series_or_array):
189185
# GH#44746
190186
left, right = left_right_dtypes
191-
left = left.copy(deep=True)
192-
right = right.copy(deep=True)
193187
arr = IntervalArray.from_arrays(left, right)
194188

195189
# The expected results below are only valid if monotonic

pandas/tests/base/test_fillna.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def test_fillna_null(null_obj, index_or_series_obj):
4444
elif isinstance(obj, MultiIndex):
4545
pytest.skip(f"MultiIndex can't hold '{null_obj}'")
4646

47+
obj = obj.copy(deep=True)
4748
values = obj._values
4849
fill_value = values[0]
4950
expected = values.copy()

pandas/tests/base/test_unique.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def test_unique_null(null_obj, index_or_series_obj, using_nan_is_na):
4646
):
4747
pytest.skip("NaN is not a valid NA for this dtype.")
4848

49+
obj = obj.copy(deep=True)
4950
values = obj._values
5051
values[0:2] = null_obj
5152

@@ -87,6 +88,7 @@ def test_nunique_null(null_obj, index_or_series_obj):
8788
elif isinstance(obj, pd.MultiIndex):
8889
pytest.skip(f"MultiIndex can't hold '{null_obj}'")
8990

91+
obj = obj.copy(deep=True)
9092
values = obj._values
9193
values[0:2] = null_obj
9294

pandas/tests/base/test_value_counts.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ def test_value_counts(index_or_series_obj):
5555
@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning")
5656
def test_value_counts_null(null_obj, index_or_series_obj):
5757
orig = index_or_series_obj
58-
obj = orig.copy()
5958

60-
if not allow_na_ops(obj):
59+
if not allow_na_ops(orig):
6160
pytest.skip("type doesn't allow for NA operations")
62-
elif len(obj) < 1:
61+
elif len(orig) < 1:
6362
pytest.skip("Test doesn't make sense on empty data")
6463
elif isinstance(orig, MultiIndex):
6564
pytest.skip(f"MultiIndex can't hold '{null_obj}'")
6665

66+
obj = orig.copy(deep=True)
6767
values = obj._values
6868
values[0:2] = null_obj
6969

pandas/tests/copy_view/test_methods.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ def test_to_frame():
560560
],
561561
ids=["shallow-copy", "reset_index", "rename", "select_dtypes"],
562562
)
563-
def test_chained_methods(request, method, idx):
563+
def test_chained_methods(method, idx):
564564
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
565565
df_orig = df.copy()
566566

pandas/tests/indexes/test_common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def test_unique(self, index_flat):
231231
if not index._can_hold_na:
232232
pytest.skip("Skip na-check if index cannot hold na")
233233

234-
vals = index._values[[0] * 5]
234+
vals = index._values.copy()[[0] * 5]
235235
vals[0] = np.nan
236236

237237
vals_unique = vals[:2]

0 commit comments

Comments
 (0)