manage a single block with
| 2013 | |
| 2014 | |
| 2015 | class SingleBlockManager(BaseBlockManager): |
| 2016 | """manage a single block with""" |
| 2017 | |
| 2018 | @property |
| 2019 | def ndim(self) -> Literal[1]: |
| 2020 | return 1 |
| 2021 | |
| 2022 | _is_consolidated = True |
| 2023 | _known_consolidated = True |
| 2024 | __slots__ = () |
| 2025 | is_single_block = True |
| 2026 | |
| 2027 | def __init__( |
| 2028 | self, |
| 2029 | block: Block, |
| 2030 | axis: Index, |
| 2031 | verify_integrity: bool = False, |
| 2032 | ) -> None: |
| 2033 | # Assertions disabled for performance |
| 2034 | # assert isinstance(block, Block), type(block) |
| 2035 | # assert isinstance(axis, Index), type(axis) |
| 2036 | |
| 2037 | self.axes = [axis] |
| 2038 | self.blocks = (block,) |
| 2039 | |
| 2040 | @classmethod |
| 2041 | def from_blocks( |
| 2042 | cls, |
| 2043 | blocks: list[Block], |
| 2044 | axes: list[Index], |
| 2045 | ) -> Self: |
| 2046 | """ |
| 2047 | Constructor for BlockManager and SingleBlockManager with same signature. |
| 2048 | """ |
| 2049 | assert len(blocks) == 1 |
| 2050 | assert len(axes) == 1 |
| 2051 | return cls(blocks[0], axes[0], verify_integrity=False) |
| 2052 | |
| 2053 | @classmethod |
| 2054 | def from_array( |
| 2055 | cls, array: ArrayLike, index: Index, refs: BlockValuesRefs | None = None |
| 2056 | ) -> SingleBlockManager: |
| 2057 | """ |
| 2058 | Constructor for if we have an array that is not yet a Block. |
| 2059 | """ |
| 2060 | array = maybe_coerce_values(array) |
| 2061 | bp = BlockPlacement(slice(0, len(index))) |
| 2062 | block = new_block(array, placement=bp, ndim=1, refs=refs) |
| 2063 | return cls(block, index) |
| 2064 | |
| 2065 | def to_2d_mgr(self, columns: Index) -> BlockManager: |
| 2066 | """ |
| 2067 | Manager analogue of Series.to_frame |
| 2068 | """ |
| 2069 | blk = self.blocks[0] |
| 2070 | arr = ensure_block_shape(blk.values, ndim=2) |
| 2071 | bp = BlockPlacement(0) |
| 2072 | new_blk = type(blk)(arr, placement=bp, ndim=2, refs=blk.refs) |
no outgoing calls