Skip to content

Commit b387da8

Browse files
jbrockmendeljorisvandenbossche
authored andcommitted
API: remove deep keyword from EA.copy (#27083)
1 parent 27f9d05 commit b387da8

File tree

19 files changed

+49
-51
lines changed

19 files changed

+49
-51
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,7 @@ ExtensionArray
805805

806806
- Bug in :func:`factorize` when passing an ``ExtensionArray`` with a custom ``na_sentinel`` (:issue:`25696`).
807807
- :meth:`Series.count` miscounts NA values in ExtensionArrays (:issue:`26835`)
808+
- Keyword argument ``deep`` has been removed from :method:`ExtensionArray.copy` (:issue:`27083`)
808809

809810
Other
810811
^^^^^

pandas/core/arrays/base.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -820,15 +820,10 @@ def take(self, indices, allow_fill=False, fill_value=None):
820820
# pandas.api.extensions.take
821821
raise AbstractMethodError(self)
822822

823-
def copy(self, deep: bool = False) -> ABCExtensionArray:
823+
def copy(self) -> ABCExtensionArray:
824824
"""
825825
Return a copy of the array.
826826
827-
Parameters
828-
----------
829-
deep : bool, default False
830-
Also copy the underlying data backing this array.
831-
832827
Returns
833828
-------
834829
ExtensionArray

pandas/core/arrays/datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ def _concat_same_type(cls, to_concat):
605605
values = np.concatenate([x.asi8 for x in to_concat])
606606
return cls(values, dtype=dtype)
607607

608-
def copy(self, deep=False):
608+
def copy(self):
609609
values = self.asi8.copy()
610610
return type(self)._simple_new(values, dtype=self.dtype, freq=self.freq)
611611

pandas/core/arrays/integer.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import copy
21
import sys
32
from typing import Type
43
import warnings
@@ -375,14 +374,10 @@ def take(self, indexer, allow_fill=False, fill_value=None):
375374

376375
return type(self)(result, mask, copy=False)
377376

378-
def copy(self, deep=False):
377+
def copy(self):
379378
data, mask = self._data, self._mask
380-
if deep:
381-
data = copy.deepcopy(data)
382-
mask = copy.deepcopy(mask)
383-
else:
384-
data = data.copy()
385-
mask = mask.copy()
379+
data = data.copy()
380+
mask = mask.copy()
386381
return type(self)(data, mask, copy=False)
387382

388383
def __setitem__(self, key, value):

pandas/core/arrays/interval.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -680,21 +680,16 @@ def _shallow_copy(self, left=None, right=None, closed=None):
680680
return self._simple_new(
681681
left, right, closed=closed, verify_integrity=False)
682682

683-
def copy(self, deep=False):
683+
def copy(self):
684684
"""
685685
Return a copy of the array.
686686
687-
Parameters
688-
----------
689-
deep : bool, default False
690-
Also copy the underlying data backing this array.
691-
692687
Returns
693688
-------
694689
IntervalArray
695690
"""
696-
left = self.left.copy(deep=True) if deep else self.left
697-
right = self.right.copy(deep=True) if deep else self.right
691+
left = self.left.copy(deep=True)
692+
right = self.right.copy(deep=True)
698693
closed = self.closed
699694
# TODO: Could skip verify_integrity here.
700695
return type(self).from_arrays(left, right, closed=closed)

pandas/core/arrays/numpy_.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def take(self, indices, allow_fill=False, fill_value=None):
285285
fill_value=fill_value)
286286
return type(self)(result)
287287

288-
def copy(self, deep=False):
288+
def copy(self):
289289
return type(self)(self._ndarray.copy())
290290

291291
def _values_for_argsort(self):

pandas/core/arrays/sparse.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -1262,12 +1262,8 @@ def searchsorted(self, v, side="left", sorter=None):
12621262
v, side, sorter
12631263
)
12641264

1265-
def copy(self, deep=False):
1266-
if deep:
1267-
values = self.sp_values.copy()
1268-
else:
1269-
values = self.sp_values
1270-
1265+
def copy(self):
1266+
values = self.sp_values.copy()
12711267
return self._simple_new(values, self.sp_index, self.dtype)
12721268

12731269
@classmethod

pandas/core/indexes/interval.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,9 @@ def __reduce__(self):
429429

430430
@Appender(_index_shared_docs['copy'])
431431
def copy(self, deep=False, name=None):
432-
array = self._data.copy(deep=deep)
432+
array = self._data
433+
if deep:
434+
array = array.copy()
433435
attributes = self._get_attributes_dict()
434436
if name is not None:
435437
attributes.update(name=name)

pandas/core/internals/blocks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2212,7 +2212,7 @@ def copy(self, deep=True):
22122212
""" copy constructor """
22132213
values = self.values
22142214
if deep:
2215-
values = values.copy(deep=True)
2215+
values = values.copy()
22162216
return self.make_block_same_class(values)
22172217

22182218
def get_values(self, dtype=None):

pandas/core/internals/construction.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,10 @@ def init_dict(data, index, columns, dtype=None):
218218
arrays = (com.maybe_iterable_to_list(data[k]) for k in keys)
219219
# GH#24096 need copy to be deep for datetime64tz case
220220
# TODO: See if we can avoid these copies
221+
arrays = [arr if not isinstance(arr, ABCIndexClass) else arr._data
222+
for arr in arrays]
221223
arrays = [arr if not is_datetime64tz_dtype(arr) else
222-
arr.copy(deep=True) for arr in arrays]
224+
arr.copy() for arr in arrays]
223225
return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
224226

225227

pandas/core/sparse/series.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,9 @@ def copy(self, deep=True):
450450
"""
451451
# TODO: https://github.com/pandas-dev/pandas/issues/22314
452452
# We skip the block manager till that is resolved.
453-
new_data = self.values.copy(deep=deep)
453+
new_data = self.values
454+
if deep:
455+
new_data = new_data.copy()
454456
return self._constructor(new_data, sparse_index=self.sp_index,
455457
fill_value=self.fill_value,
456458
index=self.index.copy(),

pandas/tests/arrays/sparse/test_array.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,9 @@ def test_set_fill_invalid_non_scalar(self, val):
591591
with pytest.raises(ValueError, match=msg):
592592
arr.fill_value = val
593593

594-
def test_copy_shallow(self):
595-
arr2 = self.arr.copy(deep=False)
596-
assert arr2.sp_values is self.arr.sp_values
594+
def test_copy(self):
595+
arr2 = self.arr.copy()
596+
assert arr2.sp_values is not self.arr.sp_values
597597
assert arr2.sp_index is self.arr.sp_index
598598

599599
def test_values_asarray(self):

pandas/tests/extension/arrow/bool.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,8 @@ def take(self, indices, allow_fill=False, fill_value=None):
108108
allow_fill=allow_fill)
109109
return self._from_sequence(result, dtype=self.dtype)
110110

111-
def copy(self, deep=False):
112-
if deep:
113-
return type(self)(copy.deepcopy(self._data))
114-
else:
115-
return type(self)(copy.copy(self._data))
111+
def copy(self):
112+
return type(self)(copy.copy(self._data))
116113

117114
@classmethod
118115
def _concat_same_type(cls, to_concat):

pandas/tests/extension/arrow/test_bool.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ def dtype():
1717

1818
@pytest.fixture
1919
def data():
20-
return ArrowBoolArray.from_scalars(np.random.randint(0, 2, size=100,
21-
dtype=bool))
20+
values = np.random.randint(0, 2, size=100, dtype=bool)
21+
values[1] = ~values[0]
22+
return ArrowBoolArray.from_scalars(values)
2223

2324

2425
@pytest.fixture
@@ -36,7 +37,9 @@ def test_array_type_with_arg(self, data, dtype):
3637

3738

3839
class TestInterface(BaseArrowTests, base.BaseInterfaceTests):
39-
pass
40+
def test_copy(self, data):
41+
# __setitem__ does not work, so we only have a smoke-test
42+
data.copy()
4043

4144

4245
class TestConstructors(BaseArrowTests, base.BaseConstructorsTests):

pandas/tests/extension/base/interface.py

+8
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,11 @@ def test_isna_extension_array(self, data_missing):
6666
assert not na.all()
6767

6868
assert na.dtype._is_boolean
69+
70+
def test_copy(self, data):
71+
# GH#27083 removing deep keyword from EA.copy
72+
assert data[0] != data[1]
73+
result = data.copy()
74+
75+
data[1] = data[0]
76+
assert result[1] != result[0]

pandas/tests/extension/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def data():
1616
"""Length-100 array for this type.
1717
1818
* data[0] and data[1] should both be non missing
19-
* data[0] and data[1] should not gbe equal
19+
* data[0] and data[1] should not be equal
2020
"""
2121
raise NotImplementedError
2222

pandas/tests/extension/decimal/array.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,8 @@ def take(self, indexer, allow_fill=False, fill_value=None):
101101
allow_fill=allow_fill)
102102
return self._from_sequence(result)
103103

104-
def copy(self, deep=False):
105-
if deep:
106-
return type(self)(self._data.copy())
107-
return type(self)(self)
104+
def copy(self):
105+
return type(self)(self._data.copy())
108106

109107
def astype(self, dtype, copy=True):
110108
if isinstance(dtype, type(self.dtype)):

pandas/tests/extension/json/array.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def take(self, indexer, allow_fill=False, fill_value=None):
143143

144144
return self._from_sequence(output)
145145

146-
def copy(self, deep=False):
146+
def copy(self):
147147
return type(self)(self.data[:])
148148

149149
def astype(self, dtype, copy=True):

pandas/tests/extension/test_sparse.py

+4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ class TestInterface(BaseSparseTests, base.BaseInterfaceTests):
9898
def test_no_values_attribute(self, data):
9999
pytest.skip("We have values")
100100

101+
def test_copy(self, data):
102+
# __setitem__ does not work, so we only have a smoke-test
103+
data.copy()
104+
101105

102106
class TestConstructors(BaseSparseTests, base.BaseConstructorsTests):
103107
pass

0 commit comments

Comments
 (0)