Skip to content

Commit b58bef6

Browse files
committed
CLN: respond to review comments
1 parent d3770c6 commit b58bef6

File tree

1 file changed

+32
-48
lines changed

1 file changed

+32
-48
lines changed

pandas/core/internals/blocks.py

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,36 @@ def convert(
470470
Attempt to coerce any object types to better types. Return a copy
471471
of the block (if copy = True).
472472
"""
473-
if not copy and using_cow:
474-
return [self.copy(deep=False)]
475-
return [self.copy()] if copy else [self]
473+
if not self.is_object:
474+
if not copy and using_cow:
475+
return [self.copy(deep=False)]
476+
return [self.copy()] if copy else [self]
477+
478+
if self.ndim != 1 and self.shape[0] != 1:
479+
return self.split_and_operate(Block.convert, copy=copy, using_cow=using_cow)
480+
481+
values = self.values
482+
if values.ndim == 2:
483+
# maybe_split ensures we only get here with values.shape[0] == 1,
484+
# avoid doing .ravel as that might make a copy
485+
values = values[0]
486+
487+
res_values = lib.maybe_convert_objects(
488+
values,
489+
convert_datetime=True,
490+
convert_timedelta=True,
491+
convert_period=True,
492+
convert_interval=True,
493+
)
494+
refs = None
495+
if copy and res_values is values:
496+
res_values = values.copy()
497+
elif res_values is values and using_cow:
498+
refs = self.refs
499+
500+
res_values = ensure_block_shape(res_values, self.ndim)
501+
res_values = maybe_coerce_values(res_values)
502+
return [self.make_block(res_values, refs=refs)]
476503

477504
# ---------------------------------------------------------------------
478505
# Array-Like Methods
@@ -2121,54 +2148,11 @@ def is_numeric(self) -> bool:
21212148
dtype = self.values.dtype
21222149
kind = dtype.kind
21232150

2124-
if kind in "fciub":
2125-
return True
2126-
else:
2127-
return False
2151+
return kind in "fciub"
21282152

21292153
@cache_readonly
21302154
def is_object(self) -> bool:
2131-
if self.values.dtype.kind == "O":
2132-
return True
2133-
else:
2134-
return False
2135-
2136-
@maybe_split
2137-
def convert(
2138-
self,
2139-
*,
2140-
copy: bool = True,
2141-
using_cow: bool = False,
2142-
) -> list[Block]:
2143-
"""
2144-
Attempt to coerce any object types to better types. Return a copy
2145-
of the block (if copy = True).
2146-
"""
2147-
if not self.is_object:
2148-
return super().convert(copy=copy, using_cow=using_cow)
2149-
2150-
values = self.values
2151-
if values.ndim == 2:
2152-
# maybe_split ensures we only get here with values.shape[0] == 1,
2153-
# avoid doing .ravel as that might make a copy
2154-
values = values[0]
2155-
2156-
res_values = lib.maybe_convert_objects(
2157-
values,
2158-
convert_datetime=True,
2159-
convert_timedelta=True,
2160-
convert_period=True,
2161-
convert_interval=True,
2162-
)
2163-
refs = None
2164-
if copy and res_values is values:
2165-
res_values = values.copy()
2166-
elif res_values is values and using_cow:
2167-
refs = self.refs
2168-
2169-
res_values = ensure_block_shape(res_values, self.ndim)
2170-
res_values = maybe_coerce_values(res_values)
2171-
return [self.make_block(res_values, refs=refs)]
2155+
return self.values.dtype.kind == "O"
21722156

21732157

21742158
class NDArrayBackedExtensionBlock(libinternals.NDArrayBackedBlock, EABackedBlock):

0 commit comments

Comments
 (0)