Skip to content

Commit 08c2c0d

Browse files
authored
REF: Make deep keyword only in Block(Manager).copy (#62969)
1 parent 50d8d1e commit 08c2c0d

File tree

7 files changed

+21
-32
lines changed

7 files changed

+21
-32
lines changed

pandas/core/generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def _init_mgr(
280280

281281
# make a copy if explicitly requested
282282
if copy:
283-
mgr = mgr.copy()
283+
mgr = mgr.copy(deep=True)
284284
if dtype is not None:
285285
# avoid further copies if we can
286286
if (

pandas/core/internals/blocks.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ def get_values_for_csv(
637637
return self.make_block(result)
638638

639639
@final
640-
def copy(self, deep: bool = True) -> Self:
640+
def copy(self, *, deep: bool) -> Self:
641641
"""copy constructor"""
642642
values = self.values
643643
refs: BlockValuesRefs | None
@@ -655,7 +655,7 @@ def _maybe_copy(self, inplace: bool) -> Self:
655655
if inplace:
656656
deep = self.refs.has_reference()
657657
return self.copy(deep=deep)
658-
return self.copy()
658+
return self.copy(deep=True)
659659

660660
@final
661661
def _get_refs_and_copy(self, inplace: bool):
@@ -922,10 +922,10 @@ def _replace_coerce(
922922
has_ref = self.refs.has_reference()
923923
nb = self.astype(np.dtype(object))
924924
if not inplace:
925-
nb = nb.copy()
925+
nb = nb.copy(deep=True)
926926
elif inplace and has_ref and nb.refs.has_reference():
927927
# no copy in astype and we had refs before
928-
nb = nb.copy()
928+
nb = nb.copy(deep=True)
929929
putmask_inplace(nb.values, mask, value)
930930
return [nb]
931931
return [self.copy(deep=False)]

pandas/core/internals/concat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,14 @@ def _maybe_reindex_columns_na_proxy(
175175
for i, indexer in indexers.items():
176176
mgr = mgr.reindex_indexer(
177177
axes[i],
178-
indexers[i],
178+
indexer,
179179
axis=i,
180180
only_slice=True, # only relevant for i==0
181181
allow_dups=True,
182182
use_na_proxy=True, # only relevant for i==0
183183
)
184184
if needs_copy and not indexers:
185-
mgr = mgr.copy()
185+
mgr = mgr.copy(deep=True)
186186

187187
new_mgrs.append(mgr)
188188
return new_mgrs

pandas/core/internals/construction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def rec_array_to_mgr(
181181
mgr = arrays_to_mgr(arrays, columns, index, dtype=dtype)
182182

183183
if copy:
184-
mgr = mgr.copy()
184+
mgr = mgr.copy(deep=True)
185185
return mgr
186186

187187

pandas/core/internals/managers.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ def setitem(self, indexer, value) -> Self:
598598
return self
599599
# No need to split if we either set all columns or on a single block
600600
# manager
601-
self = self.copy()
601+
self = self.copy(deep=True)
602602

603603
return self.apply("setitem", indexer=indexer, value=value)
604604

@@ -712,30 +712,21 @@ def _combine(self, blocks: list[Block], index: Index | None = None) -> Self:
712712
def nblocks(self) -> int:
713713
return len(self.blocks)
714714

715-
def copy(self, deep: bool | Literal["all"] = True) -> Self:
715+
def copy(self, *, deep: bool) -> Self:
716716
"""
717717
Make deep or shallow copy of BlockManager
718718
719719
Parameters
720720
----------
721721
deep : bool, string or None, default True
722-
If False or None, return a shallow copy (do not copy data)
723-
If 'all', copy data and a deep copy of the index
722+
If False, return a shallow copy (do not copy data)
724723
725724
Returns
726725
-------
727726
BlockManager
728727
"""
729-
# this preserves the notion of view copying of axes
730-
if deep:
731-
# hit in e.g. tests.io.json.test_pandas
732-
733-
def copy_func(ax):
734-
return ax.copy(deep=True) if deep == "all" else ax.view()
735-
736-
new_axes = [copy_func(ax) for ax in self.axes]
737-
else:
738-
new_axes = [ax.view() for ax in self.axes]
728+
# TODO: Should deep=True be respected for axes?
729+
new_axes = [ax.view() for ax in self.axes]
739730

740731
res = self.apply("copy", deep=deep)
741732
res.axes = new_axes
@@ -2188,7 +2179,7 @@ def setitem_inplace(self, indexer, value) -> None:
21882179
the dtype.
21892180
"""
21902181
if not self._has_no_reference(0):
2191-
self.blocks = (self._block.copy(),)
2182+
self.blocks = (self._block.copy(deep=True),)
21922183
self._reset_cache()
21932184

21942185
arr = self.array

pandas/core/series.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ def __init__(
500500
if dtype is not None:
501501
data = data.astype(dtype=dtype)
502502
elif copy:
503-
data = data.copy()
503+
data = data.copy(deep=True)
504504
else:
505505
data = sanitize_array(data, index, dtype, copy)
506506
data = SingleBlockManager.from_array(data, index, refs=refs)
@@ -1833,9 +1833,7 @@ def to_frame(self, name: Hashable = lib.no_default) -> DataFrame:
18331833
df = self._constructor_expanddim_from_mgr(mgr, axes=mgr.axes)
18341834
return df.__finalize__(self, method="to_frame")
18351835

1836-
def _set_name(
1837-
self, name, inplace: bool = False, deep: bool | None = None
1838-
) -> Series:
1836+
def _set_name(self, name, inplace: bool = False) -> Series:
18391837
"""
18401838
Set the Series name.
18411839

pandas/tests/internals/test_internals.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,12 @@ def test_attrs(self, fblock):
277277
assert len(fblock) == len(fblock.values)
278278

279279
def test_copy(self, fblock):
280-
cop = fblock.copy()
280+
cop = fblock.copy(deep=True)
281281
assert cop is not fblock
282282
assert_block_equal(fblock, cop)
283283

284284
def test_delete(self, fblock):
285-
newb = fblock.copy()
285+
newb = fblock.copy(deep=True)
286286
locs = newb.mgr_locs
287287
nb = newb.delete(0)[0]
288288
assert newb.mgr_locs is locs
@@ -295,7 +295,7 @@ def test_delete(self, fblock):
295295
assert not (newb.values[0] == 1).all()
296296
assert (nb.values[0] == 1).all()
297297

298-
newb = fblock.copy()
298+
newb = fblock.copy(deep=True)
299299
locs = newb.mgr_locs
300300
nb = newb.delete(1)
301301
assert len(nb) == 2
@@ -310,15 +310,15 @@ def test_delete(self, fblock):
310310
assert not (newb.values[1] == 2).all()
311311
assert (nb[1].values[0] == 2).all()
312312

313-
newb = fblock.copy()
313+
newb = fblock.copy(deep=True)
314314
nb = newb.delete(2)
315315
assert len(nb) == 1
316316
tm.assert_numpy_array_equal(
317317
nb[0].mgr_locs.as_array, np.array([0, 2], dtype=np.intp)
318318
)
319319
assert (nb[0].values[1] == 1).all()
320320

321-
newb = fblock.copy()
321+
newb = fblock.copy(deep=True)
322322

323323
with pytest.raises(IndexError, match=None):
324324
newb.delete(3)

0 commit comments

Comments
 (0)