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