Skip to content

Commit 114f067

Browse files
authored
CLN: Remove unnecessary copy keyword (#56420)
* CLN: Remove unnecessary copy keyword * CLN: Remove unnecessary copy keyword * Fixup
1 parent 9893c43 commit 114f067

File tree

7 files changed

+19
-89
lines changed

7 files changed

+19
-89
lines changed

pandas/_testing/asserters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,8 +1206,8 @@ def assert_frame_equal(
12061206

12071207
# compare by blocks
12081208
if by_blocks:
1209-
rblocks = right._to_dict_of_blocks(copy=False)
1210-
lblocks = left._to_dict_of_blocks(copy=False)
1209+
rblocks = right._to_dict_of_blocks()
1210+
lblocks = left._to_dict_of_blocks()
12111211
for dtype in list(set(list(lblocks.keys()) + list(rblocks.keys()))):
12121212
assert dtype in lblocks
12131213
assert dtype in rblocks

pandas/core/frame.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12484,7 +12484,7 @@ def isin_(x):
1248412484
# ----------------------------------------------------------------------
1248512485
# Internal Interface Methods
1248612486

12487-
def _to_dict_of_blocks(self, copy: bool = True):
12487+
def _to_dict_of_blocks(self):
1248812488
"""
1248912489
Return a dict of dtype -> Constructor Types that
1249012490
each is a homogeneous dtype.
@@ -12496,7 +12496,7 @@ def _to_dict_of_blocks(self, copy: bool = True):
1249612496
mgr = cast(BlockManager, mgr_to_mgr(mgr, "block"))
1249712497
return {
1249812498
k: self._constructor_from_mgr(v, axes=v.axes).__finalize__(self)
12499-
for k, v, in mgr.to_dict(copy=copy).items()
12499+
for k, v, in mgr.to_dict().items()
1250012500
}
1250112501

1250212502
@property

pandas/core/groupby/generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2012,7 +2012,7 @@ def _get_data_to_aggregate(
20122012
mgr = obj._mgr
20132013

20142014
if numeric_only:
2015-
mgr = mgr.get_numeric_data(copy=False)
2015+
mgr = mgr.get_numeric_data()
20162016
return mgr
20172017

20182018
def _wrap_agged_manager(self, mgr: Manager2D) -> DataFrame:

pandas/core/internals/managers.py

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -495,17 +495,12 @@ def is_view(self) -> bool:
495495

496496
def _get_data_subset(self, predicate: Callable) -> Self:
497497
blocks = [blk for blk in self.blocks if predicate(blk.values)]
498-
return self._combine(blocks, copy=False)
498+
return self._combine(blocks)
499499

500-
def get_bool_data(self, copy: bool = False) -> Self:
500+
def get_bool_data(self) -> Self:
501501
"""
502502
Select blocks that are bool-dtype and columns from object-dtype blocks
503503
that are all-bool.
504-
505-
Parameters
506-
----------
507-
copy : bool, default False
508-
Whether to copy the blocks
509504
"""
510505

511506
new_blocks = []
@@ -518,26 +513,16 @@ def get_bool_data(self, copy: bool = False) -> Self:
518513
nbs = blk._split()
519514
new_blocks.extend(nb for nb in nbs if nb.is_bool)
520515

521-
return self._combine(new_blocks, copy)
516+
return self._combine(new_blocks)
522517

523-
def get_numeric_data(self, copy: bool = False) -> Self:
524-
"""
525-
Parameters
526-
----------
527-
copy : bool, default False
528-
Whether to copy the blocks
529-
"""
518+
def get_numeric_data(self) -> Self:
530519
numeric_blocks = [blk for blk in self.blocks if blk.is_numeric]
531520
if len(numeric_blocks) == len(self.blocks):
532521
# Avoid somewhat expensive _combine
533-
if copy:
534-
return self.copy(deep=True)
535522
return self
536-
return self._combine(numeric_blocks, copy)
523+
return self._combine(numeric_blocks)
537524

538-
def _combine(
539-
self, blocks: list[Block], copy: bool = True, index: Index | None = None
540-
) -> Self:
525+
def _combine(self, blocks: list[Block], index: Index | None = None) -> Self:
541526
"""return a new manager with the blocks"""
542527
if len(blocks) == 0:
543528
if self.ndim == 2:
@@ -554,11 +539,8 @@ def _combine(
554539
inv_indexer = lib.get_reverse_indexer(indexer, self.shape[0])
555540

556541
new_blocks: list[Block] = []
557-
# TODO(CoW) we could optimize here if we know that the passed blocks
558-
# are fully "owned" (eg created from an operation, not coming from
559-
# an existing manager)
560542
for b in blocks:
561-
nb = b.copy(deep=copy)
543+
nb = b.copy(deep=False)
562544
nb.mgr_locs = BlockPlacement(inv_indexer[nb.mgr_locs.indexer])
563545
new_blocks.append(nb)
564546

@@ -1636,14 +1618,10 @@ def unstack(self, unstacker, fill_value) -> BlockManager:
16361618
bm = BlockManager(new_blocks, [new_columns, new_index], verify_integrity=False)
16371619
return bm
16381620

1639-
def to_dict(self, copy: bool = True) -> dict[str, Self]:
1621+
def to_dict(self) -> dict[str, Self]:
16401622
"""
16411623
Return a dict of str(dtype) -> BlockManager
16421624
1643-
Parameters
1644-
----------
1645-
copy : bool, default True
1646-
16471625
Returns
16481626
-------
16491627
values : a dict of dtype -> BlockManager
@@ -1654,7 +1632,7 @@ def to_dict(self, copy: bool = True) -> dict[str, Self]:
16541632
bd.setdefault(str(b.dtype), []).append(b)
16551633

16561634
# TODO(EA2D): the combine will be unnecessary with 2D EAs
1657-
return {dtype: self._combine(blocks, copy=copy) for dtype, blocks in bd.items()}
1635+
return {dtype: self._combine(blocks) for dtype, blocks in bd.items()}
16581636

16591637
def as_array(
16601638
self,
@@ -2034,9 +2012,9 @@ def array_values(self) -> ExtensionArray:
20342012
"""The array that Series.array returns"""
20352013
return self._block.array_values
20362014

2037-
def get_numeric_data(self, copy: bool = False) -> Self:
2015+
def get_numeric_data(self) -> Self:
20382016
if self._block.is_numeric:
2039-
return self.copy(deep=copy)
2017+
return self.copy(deep=False)
20402018
return self.make_empty()
20412019

20422020
@property

pandas/tests/frame/constructors/test_from_records.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_from_records_sequencelike(self):
8080

8181
# this is actually tricky to create the recordlike arrays and
8282
# have the dtypes be intact
83-
blocks = df._to_dict_of_blocks(copy=False)
83+
blocks = df._to_dict_of_blocks()
8484
tuples = []
8585
columns = []
8686
dtypes = []
@@ -169,7 +169,7 @@ def test_from_records_dictlike(self):
169169

170170
# columns is in a different order here than the actual items iterated
171171
# from the dict
172-
blocks = df._to_dict_of_blocks(copy=False)
172+
blocks = df._to_dict_of_blocks()
173173
columns = []
174174
for b in blocks.values():
175175
columns.extend(b.columns)

pandas/tests/frame/methods/test_to_dict_of_blocks.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,6 @@
1414

1515

1616
class TestToDictOfBlocks:
17-
def test_copy_blocks(self, float_frame):
18-
# GH#9607
19-
df = DataFrame(float_frame, copy=True)
20-
column = df.columns[0]
21-
22-
# use the default copy=True, change a column
23-
_last_df = None
24-
blocks = df._to_dict_of_blocks(copy=True)
25-
for _df in blocks.values():
26-
_last_df = _df
27-
if column in _df:
28-
_df.loc[:, column] = _df[column] + 1
29-
30-
# make sure we did not change the original DataFrame
31-
assert _last_df is not None and not _last_df[column].equals(df[column])
32-
3317
@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning")
3418
def test_no_copy_blocks(self, float_frame, using_copy_on_write):
3519
# GH#9607
@@ -38,7 +22,7 @@ def test_no_copy_blocks(self, float_frame, using_copy_on_write):
3822

3923
_last_df = None
4024
# use the copy=False, change a column
41-
blocks = df._to_dict_of_blocks(copy=False)
25+
blocks = df._to_dict_of_blocks()
4226
for _df in blocks.values():
4327
_last_df = _df
4428
if column in _df:

pandas/tests/internals/test_internals.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -790,24 +790,6 @@ def test_get_numeric_data(self, using_copy_on_write):
790790
np.array([100.0, 200.0, 300.0]),
791791
)
792792

793-
numeric2 = mgr.get_numeric_data(copy=True)
794-
tm.assert_index_equal(numeric.items, Index(["int", "float", "complex", "bool"]))
795-
numeric2.iset(
796-
numeric2.items.get_loc("float"),
797-
np.array([1000.0, 2000.0, 3000.0]),
798-
inplace=True,
799-
)
800-
if using_copy_on_write:
801-
tm.assert_almost_equal(
802-
mgr.iget(mgr.items.get_loc("float")).internal_values(),
803-
np.array([1.0, 1.0, 1.0]),
804-
)
805-
else:
806-
tm.assert_almost_equal(
807-
mgr.iget(mgr.items.get_loc("float")).internal_values(),
808-
np.array([100.0, 200.0, 300.0]),
809-
)
810-
811793
def test_get_bool_data(self, using_copy_on_write):
812794
mgr = create_mgr(
813795
"int: int; float: float; complex: complex;"
@@ -835,20 +817,6 @@ def test_get_bool_data(self, using_copy_on_write):
835817
np.array([True, False, True]),
836818
)
837819

838-
# Check sharing
839-
bools2 = mgr.get_bool_data(copy=True)
840-
bools2.iset(0, np.array([False, True, False]))
841-
if using_copy_on_write:
842-
tm.assert_numpy_array_equal(
843-
mgr.iget(mgr.items.get_loc("bool")).internal_values(),
844-
np.array([True, True, True]),
845-
)
846-
else:
847-
tm.assert_numpy_array_equal(
848-
mgr.iget(mgr.items.get_loc("bool")).internal_values(),
849-
np.array([True, False, True]),
850-
)
851-
852820
def test_unicode_repr_doesnt_raise(self):
853821
repr(create_mgr("b,\u05d0: object"))
854822

0 commit comments

Comments
 (0)