Skip to content

Commit 49b0300

Browse files
committed
REF: Make deep keyword only in Block(Manager).copy
1 parent c3bace8 commit 49b0300

File tree

7 files changed

+17
-27
lines changed

7 files changed

+17
-27
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
@@ -638,7 +638,7 @@ def get_values_for_csv(
638638
return self.make_block(result)
639639

640640
@final
641-
def copy(self, deep: bool = True) -> Self:
641+
def copy(self, *, deep: bool) -> Self:
642642
"""copy constructor"""
643643
values = self.values
644644
refs: BlockValuesRefs | None
@@ -656,7 +656,7 @@ def _maybe_copy(self, inplace: bool) -> Self:
656656
if inplace:
657657
deep = self.refs.has_reference()
658658
return self.copy(deep=deep)
659-
return self.copy()
659+
return self.copy(deep=True)
660660

661661
@final
662662
def _get_refs_and_copy(self, inplace: bool):
@@ -923,10 +923,10 @@ def _replace_coerce(
923923
has_ref = self.refs.has_reference()
924924
nb = self.astype(np.dtype(object))
925925
if not inplace:
926-
nb = nb.copy()
926+
nb = nb.copy(deep=True)
927927
elif inplace and has_ref and nb.refs.has_reference():
928928
# no copy in astype and we had refs before
929-
nb = nb.copy()
929+
nb = nb.copy(deep=True)
930930
putmask_inplace(nb.values, mask, value)
931931
return [nb]
932932
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: 3 additions & 11 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,7 +712,7 @@ 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
@@ -727,15 +727,7 @@ def copy(self, deep: bool | Literal["all"] = True) -> Self:
727727
BlockManager
728728
"""
729729
# 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]
730+
new_axes = [ax.view() for ax in self.axes]
739731

740732
res = self.apply("copy", deep=deep)
741733
res.axes = new_axes

pandas/core/series.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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)