@@ -468,13 +468,36 @@ def convert(
468
468
using_cow : bool = False ,
469
469
) -> list [Block ]:
470
470
"""
471
- attempt to coerce any object types to better types return a copy
472
- of the block (if copy = True) by definition we are not an ObjectBlock
473
- here!
471
+ Attempt to coerce any object types to better types. Return a copy
472
+ of the block (if copy = True).
474
473
"""
475
- if not copy and using_cow :
476
- return [self .copy (deep = False )]
477
- return [self .copy ()] if copy else [self ]
474
+ if not self .is_object :
475
+ if not copy and using_cow :
476
+ return [self .copy (deep = False )]
477
+ return [self .copy ()] if copy else [self ]
478
+
479
+ if self .ndim != 1 and self .shape [0 ] != 1 :
480
+ return self .split_and_operate (Block .convert , copy = copy , using_cow = using_cow )
481
+
482
+ values = self .values
483
+ if values .ndim == 2 :
484
+ # the check above ensures we only get here with values.shape[0] == 1,
485
+ # avoid doing .ravel as that might make a copy
486
+ values = values [0 ]
487
+
488
+ res_values = lib .maybe_convert_objects (
489
+ values , # type: ignore[arg-type]
490
+ convert_non_numeric = True ,
491
+ )
492
+ refs = None
493
+ if copy and res_values is values :
494
+ res_values = values .copy ()
495
+ elif res_values is values and using_cow :
496
+ refs = self .refs
497
+
498
+ res_values = ensure_block_shape (res_values , self .ndim )
499
+ res_values = maybe_coerce_values (res_values )
500
+ return [self .make_block (res_values , refs = refs )]
478
501
479
502
# ---------------------------------------------------------------------
480
503
# Array-Like Methods
@@ -679,7 +702,7 @@ def _replace_regex(
679
702
List[Block]
680
703
"""
681
704
if not self ._can_hold_element (to_replace ):
682
- # i.e. only ObjectBlock , but could in principle include a
705
+ # i.e. only if self.is_object is True , but could in principle include a
683
706
# String ExtensionBlock
684
707
if using_cow :
685
708
return [self .copy (deep = False )]
@@ -1272,7 +1295,7 @@ def fillna(
1272
1295
) -> list [Block ]:
1273
1296
"""
1274
1297
fillna on the block with the value. If we fail, then convert to
1275
- ObjectBlock and try again
1298
+ block to hold objects instead and try again
1276
1299
"""
1277
1300
# Caller is responsible for validating limit; if int it is strictly positive
1278
1301
inplace = validate_bool_kwarg (inplace , "inplace" )
@@ -2063,7 +2086,7 @@ def _unstack(
2063
2086
needs_masking : npt .NDArray [np .bool_ ],
2064
2087
):
2065
2088
# ExtensionArray-safe unstack.
2066
- # We override ObjectBlock ._unstack, which unstacks directly on the
2089
+ # We override Block ._unstack, which unstacks directly on the
2067
2090
# values of the array. For EA-backed blocks, this would require
2068
2091
# converting to a 2-D ndarray of objects.
2069
2092
# Instead, we unstack an ndarray of integer positions, followed by
@@ -2099,6 +2122,7 @@ def _unstack(
2099
2122
2100
2123
class NumpyBlock (libinternals .NumpyBlock , Block ):
2101
2124
values : np .ndarray
2125
+ __slots__ = ()
2102
2126
2103
2127
@property
2104
2128
def is_view (self ) -> bool :
@@ -2117,10 +2141,28 @@ def get_values(self, dtype: DtypeObj | None = None) -> np.ndarray:
2117
2141
def values_for_json (self ) -> np .ndarray :
2118
2142
return self .values
2119
2143
2144
+ @cache_readonly
2145
+ def is_numeric (self ) -> bool : # type: ignore[override]
2146
+ dtype = self .values .dtype
2147
+ kind = dtype .kind
2148
+
2149
+ return kind in "fciub"
2150
+
2151
+ @cache_readonly
2152
+ def is_object (self ) -> bool : # type: ignore[override]
2153
+ return self .values .dtype .kind == "O"
2154
+
2120
2155
2121
2156
class NumericBlock (NumpyBlock ):
2157
+ # this Block type is kept for backwards-compatibility
2158
+ # TODO(3.0): delete and remove deprecation in __init__.py.
2159
+ __slots__ = ()
2160
+
2161
+
2162
+ class ObjectBlock (NumpyBlock ):
2163
+ # this Block type is kept for backwards-compatibility
2164
+ # TODO(3.0): delete and remove deprecation in __init__.py.
2122
2165
__slots__ = ()
2123
- is_numeric = True
2124
2166
2125
2167
2126
2168
class NDArrayBackedExtensionBlock (libinternals .NDArrayBackedBlock , EABackedBlock ):
@@ -2256,49 +2298,6 @@ class DatetimeTZBlock(DatetimeLikeBlock):
2256
2298
values_for_json = NDArrayBackedExtensionBlock .values_for_json
2257
2299
2258
2300
2259
- class ObjectBlock (NumpyBlock ):
2260
- __slots__ = ()
2261
- is_object = True
2262
-
2263
- @maybe_split
2264
- def convert (
2265
- self ,
2266
- * ,
2267
- copy : bool = True ,
2268
- using_cow : bool = False ,
2269
- ) -> list [Block ]:
2270
- """
2271
- attempt to cast any object types to better types return a copy of
2272
- the block (if copy = True) by definition we ARE an ObjectBlock!!!!!
2273
- """
2274
- if self .dtype != _dtype_obj :
2275
- # GH#50067 this should be impossible in ObjectBlock, but until
2276
- # that is fixed, we short-circuit here.
2277
- if using_cow :
2278
- return [self .copy (deep = False )]
2279
- return [self ]
2280
-
2281
- values = self .values
2282
- if values .ndim == 2 :
2283
- # maybe_split ensures we only get here with values.shape[0] == 1,
2284
- # avoid doing .ravel as that might make a copy
2285
- values = values [0 ]
2286
-
2287
- res_values = lib .maybe_convert_objects (
2288
- values ,
2289
- convert_non_numeric = True ,
2290
- )
2291
- refs = None
2292
- if copy and res_values is values :
2293
- res_values = values .copy ()
2294
- elif res_values is values and using_cow :
2295
- refs = self .refs
2296
-
2297
- res_values = ensure_block_shape (res_values , self .ndim )
2298
- res_values = maybe_coerce_values (res_values )
2299
- return [self .make_block (res_values , refs = refs )]
2300
-
2301
-
2302
2301
# -----------------------------------------------------------------
2303
2302
# Constructor Helpers
2304
2303
@@ -2357,10 +2356,8 @@ def get_block_type(dtype: DtypeObj) -> type[Block]:
2357
2356
kind = dtype .kind
2358
2357
if kind in "Mm" :
2359
2358
return DatetimeLikeBlock
2360
- elif kind in "fciub" :
2361
- return NumericBlock
2362
2359
2363
- return ObjectBlock
2360
+ return NumpyBlock
2364
2361
2365
2362
2366
2363
def new_block_2d (
0 commit comments