MCPcopy
hub / github.com/pandas-dev/pandas / convert

Method convert

pandas/core/internals/blocks.py:488–522  ·  view source on GitHub ↗

Attempt to coerce any object types to better types. Return a copy of the block (if copy = True).

(self)

Source from the content-addressed store, hash-verified

486
487 @final
488 def convert(self) -> list[Block]:
489 """
490 Attempt to coerce any object types to better types. Return a copy
491 of the block (if copy = True).
492 """
493 if not self.is_object:
494 return [self.copy(deep=False)]
495
496 if self.ndim != 1 and self.shape[0] != 1:
497 blocks = self.split_and_operate(Block.convert)
498 if all(blk.dtype.kind == "O" for blk in blocks):
499 # Avoid fragmenting the block if convert is a no-op
500 return [self.copy(deep=False)]
501 return blocks
502
503 values = self.values
504 if values.ndim == 2:
505 # the check above ensures we only get here with values.shape[0] == 1,
506 # avoid doing .ravel as that might make a copy
507 values = values[0]
508
509 res_values = lib.maybe_convert_objects(
510 values, # type: ignore[arg-type]
511 convert_non_numeric=True,
512 )
513 refs = None
514 if res_values is values or (
515 isinstance(res_values, NumpyExtensionArray)
516 and res_values._ndarray is values
517 ):
518 refs = self.refs
519
520 res_values = ensure_block_shape(res_values, self.ndim)
521 res_values = maybe_coerce_values(res_values)
522 return [self.make_block(res_values, refs=refs)]
523
524 def convert_dtypes(
525 self,

Callers 2

convert_dtypesMethod · 0.95
infer_objectsMethod · 0.45

Calls 5

copyMethod · 0.95
split_and_operateMethod · 0.95
make_blockMethod · 0.95
ensure_block_shapeFunction · 0.85
maybe_coerce_valuesFunction · 0.85

Tested by

no test coverage detected