Set values with indexer. For SingleBlockManager, this backs s[indexer] = value
(self, indexer, value)
| 550 | return self.apply("shift", periods=periods, fill_value=fill_value) |
| 551 | |
| 552 | def setitem(self, indexer, value) -> Self: |
| 553 | """ |
| 554 | Set values with indexer. |
| 555 | |
| 556 | For SingleBlockManager, this backs s[indexer] = value |
| 557 | """ |
| 558 | if isinstance(indexer, np.ndarray) and indexer.ndim > self.ndim: |
| 559 | raise ValueError(f"Cannot set values with ndim > {self.ndim}") |
| 560 | |
| 561 | if not self._has_no_reference(0): |
| 562 | # this method is only called if there is a single block -> hardcoded 0 |
| 563 | # Split blocks to only copy the columns we want to modify |
| 564 | if self.ndim == 2 and isinstance(indexer, tuple): |
| 565 | blk_loc = self.blklocs[indexer[1]] |
| 566 | if is_list_like(blk_loc) and blk_loc.ndim == 2: |
| 567 | blk_loc = np.squeeze(blk_loc, axis=0) |
| 568 | elif not is_list_like(blk_loc): |
| 569 | # Keep dimension and copy data later |
| 570 | blk_loc = [blk_loc] # type: ignore[assignment] |
| 571 | if len(blk_loc) == 0: |
| 572 | return self.copy(deep=False) |
| 573 | |
| 574 | values = self.blocks[0].values |
| 575 | if values.ndim == 2: |
| 576 | # Block.delete in _iset_split_block requires sorted unique |
| 577 | # locs; inverse maps the requested column order onto the |
| 578 | # new block (GH#65446) |
| 579 | blk_loc, inverse = np.unique(blk_loc, return_inverse=True) |
| 580 | values = values[blk_loc] |
| 581 | # "T" has no attribute "_iset_split_block" |
| 582 | self._iset_split_block( # type: ignore[attr-defined] |
| 583 | 0, blk_loc, values |
| 584 | ) |
| 585 | |
| 586 | indexer = list(indexer) |
| 587 | # first block equals values we are setting to -> set to all columns |
| 588 | if lib.is_integer(indexer[1]): |
| 589 | col_indexer = 0 |
| 590 | elif len(inverse) > 1 and lib.is_range_indexer( |
| 591 | inverse, len(blk_loc) |
| 592 | ): |
| 593 | col_indexer = slice(None) # type: ignore[assignment] |
| 594 | else: |
| 595 | col_indexer = inverse # type: ignore[assignment] |
| 596 | indexer[1] = col_indexer |
| 597 | |
| 598 | row_indexer = indexer[0] |
| 599 | if isinstance(col_indexer, np.ndarray): |
| 600 | if ( |
| 601 | isinstance(row_indexer, np.ndarray) |
| 602 | and row_indexer.ndim == 1 |
| 603 | ): |
| 604 | # GH#65446: Make the row indexer 2d to take a cross product |
| 605 | row_indexer = row_indexer[:, None] |
| 606 | elif isinstance(row_indexer, np.ndarray) and row_indexer.ndim == 2: |
| 607 | # numpy cannot handle a 2d indexer in combo with a slice |
| 608 | row_indexer = np.squeeze(row_indexer, axis=1) |
| 609 | if isinstance(row_indexer, np.ndarray) and len(row_indexer) == 0: |
no test coverage detected