Iterate over the blocks, collect and create a new BlockManager. Parameters ---------- f : str or callable Name of the Block method to apply. align_keys: List[str] or None, default None **kwargs Keywords to pass to `f`
(
self,
f,
align_keys: list[str] | None = None,
**kwargs,
)
| 397 | return self._equal_values(other) |
| 398 | |
| 399 | def apply( |
| 400 | self, |
| 401 | f, |
| 402 | align_keys: list[str] | None = None, |
| 403 | **kwargs, |
| 404 | ) -> Self: |
| 405 | """ |
| 406 | Iterate over the blocks, collect and create a new BlockManager. |
| 407 | |
| 408 | Parameters |
| 409 | ---------- |
| 410 | f : str or callable |
| 411 | Name of the Block method to apply. |
| 412 | align_keys: List[str] or None, default None |
| 413 | **kwargs |
| 414 | Keywords to pass to `f` |
| 415 | |
| 416 | Returns |
| 417 | ------- |
| 418 | BlockManager |
| 419 | """ |
| 420 | assert "filter" not in kwargs |
| 421 | |
| 422 | align_keys = align_keys or [] |
| 423 | result_blocks: list[Block] = [] |
| 424 | # fillna: Series/DataFrame is responsible for making sure value is aligned |
| 425 | |
| 426 | aligned_args = {k: kwargs[k] for k in align_keys} |
| 427 | |
| 428 | for b in self.blocks: |
| 429 | if aligned_args: |
| 430 | for k, obj in aligned_args.items(): |
| 431 | if isinstance(obj, (ABCSeries, ABCDataFrame)): |
| 432 | # The caller is responsible for ensuring that |
| 433 | # obj.axes[-1].equals(self.items) |
| 434 | if obj.ndim == 1: |
| 435 | kwargs[k] = obj.iloc[b.mgr_locs.indexer]._values |
| 436 | else: |
| 437 | kwargs[k] = obj.iloc[:, b.mgr_locs.indexer]._values |
| 438 | else: |
| 439 | # otherwise we have an ndarray |
| 440 | kwargs[k] = obj[b.mgr_locs.indexer] |
| 441 | |
| 442 | if callable(f): |
| 443 | applied = b.apply(f, **kwargs) |
| 444 | else: |
| 445 | applied = getattr(b, f)(**kwargs) |
| 446 | result_blocks = extend_blocks(applied, result_blocks) |
| 447 | |
| 448 | out = type(self).from_blocks(result_blocks, [ax.view() for ax in self.axes]) |
| 449 | return out |
| 450 | |
| 451 | @final |
| 452 | def isna(self, func) -> Self: |
no test coverage detected