Attempt to coerce any object types to better types. Return a copy of the block (if copy = True).
(self)
| 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, |
no test coverage detected