Skip to content

Commit fdb5658

Browse files
authored
REF: share _maybe_downcast between Block subclasses (#40619)
1 parent dad651d commit fdb5658

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

pandas/core/internals/blocks.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,19 @@ def split_and_operate(self, func, *args, **kwargs) -> List[Block]:
510510
res_blocks.extend(rbs)
511511
return res_blocks
512512

513+
@final
513514
def _maybe_downcast(self, blocks: List[Block], downcast=None) -> List[Block]:
514515

516+
if self.dtype == _dtype_obj:
517+
# TODO: why is behavior different for object dtype?
518+
if downcast is not None:
519+
return blocks
520+
521+
# split and convert the blocks
522+
return extend_blocks(
523+
[blk.convert(datetime=True, numeric=False) for blk in blocks]
524+
)
525+
515526
# no need to downcast our float
516527
# unless indicated
517528
if downcast is None and self.dtype.kind in ["f", "m", "M"]:
@@ -520,6 +531,7 @@ def _maybe_downcast(self, blocks: List[Block], downcast=None) -> List[Block]:
520531

521532
return extend_blocks([b.downcast(downcast) for b in blocks])
522533

534+
@final
523535
def downcast(self, dtypes=None) -> List[Block]:
524536
""" try to downcast each item to the dict of dtypes if present """
525537
# turn it off completely
@@ -1375,6 +1387,7 @@ def _unstack(self, unstacker, fill_value, new_placement):
13751387
blocks = [new_block(new_values, placement=new_placement, ndim=2)]
13761388
return blocks, mask
13771389

1390+
@final
13781391
def quantile(
13791392
self, qs: Float64Index, interpolation="linear", axis: int = 0
13801393
) -> Block:
@@ -1952,14 +1965,6 @@ def convert(
19521965
res_values = ensure_block_shape(res_values, self.ndim)
19531966
return [self.make_block(res_values)]
19541967

1955-
def _maybe_downcast(self, blocks: List[Block], downcast=None) -> List[Block]:
1956-
1957-
if downcast is not None:
1958-
return blocks
1959-
1960-
# split and convert the blocks
1961-
return extend_blocks([b.convert(datetime=True, numeric=False) for b in blocks])
1962-
19631968
def _can_hold_element(self, element: Any) -> bool:
19641969
return True
19651970

0 commit comments

Comments
 (0)