Block.shape inconsistencies? · Issue #23023 · pandas-dev/pandas (original) (raw)

Trying to simplify some things in internals. I expected that it would always be the case that len(block) == block.shape[0], but shape is flipped in a few cases.

class Block:
    def __len__(self):
        return len(self.values)

    @property
    def shape(self):
        return self.values.shape

class NonConsolidatableMixin:
    @property
    def shape(self):
        if self.ndim == 1:
            return (len(self.values)),
        return (len(self.mgr_locs), len(self.values))

class SparseBlock:
    @property
    def shape(self):
        return (len(self.mgr_locs), self.sp_index.length)

    def __len__(self):
        try:
            return self.sp_index.length
        except AttributeError:
            return 0

So for Block len(b) == b.shape[0], 1-dim NonConsolidatable (and ScalarBlock) behave as expected, but 2-dim have len(b) == b.shape[1]. Ditto SparseBlock (a try/except notwithstanding).

What's the internal logic here?