Skip to content

Commit 4bec9c4

Browse files
jbrockmendeljreback
authored andcommitted
CLN: BlockManager.apply (#29825)
1 parent 9ed267e commit 4bec9c4

File tree

3 files changed

+32
-50
lines changed

3 files changed

+32
-50
lines changed

pandas/core/generic.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -5571,7 +5571,7 @@ def _to_dict_of_blocks(self, copy=True):
55715571
for k, v, in self._data.to_dict(copy=copy).items()
55725572
}
55735573

5574-
def astype(self, dtype, copy=True, errors="raise"):
5574+
def astype(self, dtype, copy: bool_t = True, errors: str = "raise"):
55755575
"""
55765576
Cast a pandas object to a specified dtype ``dtype``.
55775577
@@ -5697,10 +5697,10 @@ def astype(self, dtype, copy=True, errors="raise"):
56975697
elif is_extension_array_dtype(dtype) and self.ndim > 1:
56985698
# GH 18099/22869: columnwise conversion to extension dtype
56995699
# GH 24704: use iloc to handle duplicate column names
5700-
results = (
5700+
results = [
57015701
self.iloc[:, i].astype(dtype, copy=copy)
57025702
for i in range(len(self.columns))
5703-
)
5703+
]
57045704

57055705
else:
57065706
# else, only a single dtype is given

pandas/core/internals/blocks.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -523,16 +523,14 @@ def f(mask, val, idx):
523523

524524
return self.split_and_operate(None, f, False)
525525

526-
def astype(self, dtype, copy=False, errors="raise", **kwargs):
527-
return self._astype(dtype, copy=copy, errors=errors, **kwargs)
528-
529-
def _astype(self, dtype, copy=False, errors="raise", **kwargs):
530-
"""Coerce to the new type
526+
def astype(self, dtype, copy: bool = False, errors: str = "raise"):
527+
"""
528+
Coerce to the new dtype.
531529
532530
Parameters
533531
----------
534532
dtype : str, dtype convertible
535-
copy : boolean, default False
533+
copy : bool, default False
536534
copy if indicated
537535
errors : str, {'raise', 'ignore'}, default 'ignore'
538536
- ``raise`` : allow exceptions to be raised
@@ -2142,7 +2140,7 @@ def _maybe_coerce_values(self, values):
21422140
assert isinstance(values, np.ndarray), type(values)
21432141
return values
21442142

2145-
def _astype(self, dtype, **kwargs):
2143+
def astype(self, dtype, copy: bool = False, errors: str = "raise"):
21462144
"""
21472145
these automatically copy, so copy=True has no effect
21482146
raise on an except if raise == True
@@ -2158,7 +2156,7 @@ def _astype(self, dtype, **kwargs):
21582156
return self.make_block(values)
21592157

21602158
# delegate
2161-
return super()._astype(dtype=dtype, **kwargs)
2159+
return super().astype(dtype=dtype, copy=copy, errors=errors)
21622160

21632161
def _can_hold_element(self, element: Any) -> bool:
21642162
tipo = maybe_infer_dtype_type(element)

pandas/core/internals/managers.py

+23-39
Original file line numberDiff line numberDiff line change
@@ -340,33 +340,20 @@ def _verify_integrity(self):
340340
"tot_items: {1}".format(len(self.items), tot_items)
341341
)
342342

343-
def apply(
344-
self,
345-
f,
346-
axes=None,
347-
filter=None,
348-
do_integrity_check=False,
349-
consolidate=True,
350-
**kwargs,
351-
):
343+
def apply(self, f: str, filter=None, **kwargs):
352344
"""
353-
iterate over the blocks, collect and create a new block manager
345+
Iterate over the blocks, collect and create a new BlockManager.
354346
355347
Parameters
356348
----------
357-
f : the callable or function name to operate on at the block level
358-
axes : optional (if not supplied, use self.axes)
349+
f : str
350+
Name of the Block method to apply.
359351
filter : list, if supplied, only call the block if the filter is in
360352
the block
361-
do_integrity_check : boolean, default False. Do the block manager
362-
integrity check
363-
consolidate: boolean, default True. Join together blocks having same
364-
dtype
365353
366354
Returns
367355
-------
368-
Block Manager (new object)
369-
356+
BlockManager
370357
"""
371358

372359
result_blocks = []
@@ -380,8 +367,7 @@ def apply(
380367
else:
381368
kwargs["filter"] = filter_locs
382369

383-
if consolidate:
384-
self._consolidate_inplace()
370+
self._consolidate_inplace()
385371

386372
if f == "where":
387373
align_copy = True
@@ -429,11 +415,8 @@ def apply(
429415
result_blocks = _extend_blocks(applied, result_blocks)
430416

431417
if len(result_blocks) == 0:
432-
return self.make_empty(axes or self.axes)
433-
bm = type(self)(
434-
result_blocks, axes or self.axes, do_integrity_check=do_integrity_check
435-
)
436-
bm._consolidate_inplace()
418+
return self.make_empty(self.axes)
419+
bm = type(self)(result_blocks, self.axes, do_integrity_check=False)
437420
return bm
438421

439422
def quantile(
@@ -540,8 +523,8 @@ def get_axe(block, qs, axes):
540523
[make_block(values, ndim=1, placement=np.arange(len(values)))], axes[0]
541524
)
542525

543-
def isna(self, func, **kwargs):
544-
return self.apply("apply", func=func, **kwargs)
526+
def isna(self, func):
527+
return self.apply("apply", func=func)
545528

546529
def where(self, **kwargs):
547530
return self.apply("where", **kwargs)
@@ -567,8 +550,8 @@ def fillna(self, **kwargs):
567550
def downcast(self, **kwargs):
568551
return self.apply("downcast", **kwargs)
569552

570-
def astype(self, dtype, **kwargs):
571-
return self.apply("astype", dtype=dtype, **kwargs)
553+
def astype(self, dtype, copy: bool = False, errors: str = "raise"):
554+
return self.apply("astype", dtype=dtype, copy=copy, errors=errors)
572555

573556
def convert(self, **kwargs):
574557
return self.apply("convert", **kwargs)
@@ -768,14 +751,19 @@ def copy(self, deep=True):
768751
"""
769752
# this preserves the notion of view copying of axes
770753
if deep:
771-
if deep == "all":
772-
copy = lambda ax: ax.copy(deep=True)
773-
else:
774-
copy = lambda ax: ax.view()
775-
new_axes = [copy(ax) for ax in self.axes]
754+
755+
def copy_func(ax):
756+
if deep == "all":
757+
return ax.copy(deep=True)
758+
else:
759+
return ax.view()
760+
761+
new_axes = [copy_func(ax) for ax in self.axes]
776762
else:
777763
new_axes = list(self.axes)
778-
return self.apply("copy", axes=new_axes, deep=deep, do_integrity_check=False)
764+
res = self.apply("copy", deep=deep)
765+
res.axes = new_axes
766+
return res
779767

780768
def as_array(self, transpose=False, items=None):
781769
"""Convert the blockmanager data into an numpy array.
@@ -1527,10 +1515,6 @@ def get_slice(self, slobj, axis=0):
15271515
def index(self):
15281516
return self.axes[0]
15291517

1530-
def convert(self, **kwargs):
1531-
""" convert the whole block as one """
1532-
return self.apply("convert", **kwargs)
1533-
15341518
@property
15351519
def dtype(self):
15361520
return self._block.dtype

0 commit comments

Comments
 (0)