Skip to content

Commit 8cc7df2

Browse files
committed
CLN: respond to review comments
1 parent e9ed9f0 commit 8cc7df2

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
@@ -2119,54 +2146,11 @@ def is_numeric(self) -> bool:
21192146
dtype = self.values.dtype
21202147
kind = dtype.kind
21212148

2122-
if kind in "fciub":
2123-
return True
2124-
else:
2125-
return False
2149+
return kind in "fciub"
21262150

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

21712155

21722156
class NDArrayBackedExtensionBlock(libinternals.NDArrayBackedBlock, EABackedBlock):

0 commit comments

Comments
 (0)