BaseBlockManager that holds 2D blocks.
| 1096 | |
| 1097 | |
| 1098 | class BlockManager(libinternals.BlockManager, BaseBlockManager): |
| 1099 | """ |
| 1100 | BaseBlockManager that holds 2D blocks. |
| 1101 | """ |
| 1102 | |
| 1103 | ndim = 2 |
| 1104 | |
| 1105 | # ---------------------------------------------------------------- |
| 1106 | # Constructors |
| 1107 | |
| 1108 | def __init__( |
| 1109 | self, |
| 1110 | blocks: Sequence[Block], |
| 1111 | axes: Sequence[Index], |
| 1112 | verify_integrity: bool = True, |
| 1113 | ) -> None: |
| 1114 | if verify_integrity: |
| 1115 | # Assertion disabled for performance |
| 1116 | # assert all(isinstance(x, Index) for x in axes) |
| 1117 | |
| 1118 | for block in blocks: |
| 1119 | if self.ndim != block.ndim: |
| 1120 | raise AssertionError( |
| 1121 | f"Number of Block dimensions ({block.ndim}) must equal " |
| 1122 | f"number of axes ({self.ndim})" |
| 1123 | ) |
| 1124 | # As of 2.0, the caller is responsible for ensuring that |
| 1125 | # DatetimeTZBlock with block.ndim == 2 has block.values.ndim ==2; |
| 1126 | # previously there was a special check for fastparquet compat. |
| 1127 | |
| 1128 | self._verify_integrity() |
| 1129 | |
| 1130 | def _verify_integrity(self) -> None: |
| 1131 | mgr_shape = self.shape |
| 1132 | tot_items = sum(len(x.mgr_locs) for x in self.blocks) |
| 1133 | for block in self.blocks: |
| 1134 | if block.shape[1:] != mgr_shape[1:]: |
| 1135 | raise_construction_error(tot_items, block.shape[1:], self.axes) |
| 1136 | if len(self.items) != tot_items: |
| 1137 | raise AssertionError( |
| 1138 | "Number of manager items must equal union of " |
| 1139 | f"block items\n# manager items: {len(self.items)}, # " |
| 1140 | f"tot_items: {tot_items}" |
| 1141 | ) |
| 1142 | |
| 1143 | @classmethod |
| 1144 | def from_blocks(cls, blocks: list[Block], axes: list[Index]) -> Self: |
| 1145 | """ |
| 1146 | Constructor for BlockManager and SingleBlockManager with same signature. |
| 1147 | """ |
| 1148 | return cls(blocks, axes, verify_integrity=False) |
| 1149 | |
| 1150 | # ---------------------------------------------------------------- |
| 1151 | # Indexing |
| 1152 | |
| 1153 | def fast_xs(self, loc: int) -> SingleBlockManager: |
| 1154 | """ |
| 1155 | Return the array corresponding to `frame.iloc[loc]`. |
no outgoing calls