4242 soft_convert_objects ,
4343)
4444from pandas .core .dtypes .common import (
45+ is_1d_only_ea_dtype ,
46+ is_1d_only_ea_obj ,
4547 is_categorical_dtype ,
4648 is_dtype_equal ,
4749 is_extension_array_dtype ,
@@ -224,7 +226,6 @@ def get_values(self, dtype: DtypeObj | None = None) -> np.ndarray:
224226 # expected "ndarray")
225227 return self .values # type: ignore[return-value]
226228
227- @final
228229 def get_block_values_for_json (self ) -> np .ndarray :
229230 """
230231 This is used in the JSON C code.
@@ -415,7 +416,11 @@ def _split_op_result(self, result) -> list[Block]:
415416 # if we get a 2D ExtensionArray, we need to split it into 1D pieces
416417 nbs = []
417418 for i , loc in enumerate (self ._mgr_locs ):
418- vals = result [i ]
419+ if not is_1d_only_ea_obj (result ):
420+ vals = result [i : i + 1 ]
421+ else :
422+ vals = result [i ]
423+
419424 block = self .make_block (values = vals , placement = loc )
420425 nbs .append (block )
421426 return nbs
@@ -1670,7 +1675,7 @@ class NumericBlock(NumpyBlock):
16701675 is_numeric = True
16711676
16721677
1673- class NDArrayBackedExtensionBlock (EABackedBlock ):
1678+ class NDArrayBackedExtensionBlock (libinternals . Block , EABackedBlock ):
16741679 """
16751680 Block backed by an NDArrayBackedExtensionArray
16761681 """
@@ -1683,11 +1688,6 @@ def is_view(self) -> bool:
16831688 # check the ndarray values of the DatetimeIndex values
16841689 return self .values ._ndarray .base is not None
16851690
1686- def iget (self , key ):
1687- # GH#31649 we need to wrap scalars in Timestamp/Timedelta
1688- # TODO(EA2D): this can be removed if we ever have 2D EA
1689- return self .values .reshape (self .shape )[key ]
1690-
16911691 def setitem (self , indexer , value ):
16921692 if not self ._can_hold_element (value ):
16931693 # TODO: general case needs casting logic.
@@ -1707,24 +1707,21 @@ def putmask(self, mask, new) -> list[Block]:
17071707 if not self ._can_hold_element (new ):
17081708 return self .astype (object ).putmask (mask , new )
17091709
1710- # TODO(EA2D): reshape unnecessary with 2D EAs
1711- arr = self .values .reshape (self .shape )
1710+ arr = self .values
17121711 arr .T .putmask (mask , new )
17131712 return [self ]
17141713
17151714 def where (self , other , cond , errors = "raise" ) -> list [Block ]:
17161715 # TODO(EA2D): reshape unnecessary with 2D EAs
1717- arr = self .values . reshape ( self . shape )
1716+ arr = self .values
17181717
17191718 cond = extract_bool_array (cond )
17201719
17211720 try :
17221721 res_values = arr .T .where (cond , other ).T
17231722 except (ValueError , TypeError ):
1724- return super () .where (other , cond , errors = errors )
1723+ return Block .where (self , other , cond , errors = errors )
17251724
1726- # TODO(EA2D): reshape not needed with 2D EAs
1727- res_values = res_values .reshape (self .values .shape )
17281725 nb = self .make_block_same_class (res_values )
17291726 return [nb ]
17301727
@@ -1748,15 +1745,13 @@ def diff(self, n: int, axis: int = 0) -> list[Block]:
17481745 The arguments here are mimicking shift so they are called correctly
17491746 by apply.
17501747 """
1751- # TODO(EA2D): reshape not necessary with 2D EAs
1752- values = self .values .reshape (self .shape )
1748+ values = self .values
17531749
17541750 new_values = values - values .shift (n , axis = axis )
17551751 return [self .make_block (new_values )]
17561752
17571753 def shift (self , periods : int , axis : int = 0 , fill_value : Any = None ) -> list [Block ]:
1758- # TODO(EA2D) this is unnecessary if these blocks are backed by 2D EAs
1759- values = self .values .reshape (self .shape )
1754+ values = self .values
17601755 new_values = values .shift (periods , fill_value = fill_value , axis = axis )
17611756 return [self .make_block_same_class (new_values )]
17621757
@@ -1776,31 +1771,27 @@ def fillna(
17761771 return [self .make_block_same_class (values = new_values )]
17771772
17781773
1779- class DatetimeLikeBlock (libinternals . Block , NDArrayBackedExtensionBlock ):
1774+ class DatetimeLikeBlock (NDArrayBackedExtensionBlock ):
17801775 """Block for datetime64[ns], timedelta64[ns]."""
17811776
17821777 __slots__ = ()
17831778 is_numeric = False
17841779 values : DatetimeArray | TimedeltaArray
17851780
1781+ def get_block_values_for_json (self ):
1782+ # Not necessary to override, but helps perf
1783+ return self .values ._ndarray
17861784
1787- class DatetimeTZBlock (ExtensionBlock , NDArrayBackedExtensionBlock ):
1785+
1786+ class DatetimeTZBlock (DatetimeLikeBlock ):
17881787 """ implement a datetime64 block with a tz attribute """
17891788
17901789 values : DatetimeArray
17911790
17921791 __slots__ = ()
17931792 is_extension = True
1794- is_numeric = False
1795-
1796- diff = NDArrayBackedExtensionBlock .diff
1797- where = NDArrayBackedExtensionBlock .where
1798- putmask = NDArrayBackedExtensionBlock .putmask
1799- fillna = NDArrayBackedExtensionBlock .fillna
1800-
1801- get_values = NDArrayBackedExtensionBlock .get_values
1802-
1803- is_view = NDArrayBackedExtensionBlock .is_view
1793+ _validate_ndim = True
1794+ _can_consolidate = False
18041795
18051796
18061797class ObjectBlock (NumpyBlock ):
@@ -1967,7 +1958,7 @@ def check_ndim(values, placement: BlockPlacement, ndim: int):
19671958 f"values.ndim > ndim [{ values .ndim } > { ndim } ]"
19681959 )
19691960
1970- elif isinstance (values . dtype , np .dtype ):
1961+ elif not is_1d_only_ea_dtype (values .dtype ):
19711962 # TODO(EA2D): special case not needed with 2D EAs
19721963 if values .ndim != ndim :
19731964 raise ValueError (
@@ -1981,7 +1972,7 @@ def check_ndim(values, placement: BlockPlacement, ndim: int):
19811972 )
19821973 elif ndim == 2 and len (placement ) != 1 :
19831974 # TODO(EA2D): special case unnecessary with 2D EAs
1984- raise AssertionError ( "block.size != values.size " )
1975+ raise ValueError ( "need to split " )
19851976
19861977
19871978def extract_pandas_array (
@@ -2026,8 +2017,9 @@ def ensure_block_shape(values: ArrayLike, ndim: int = 1) -> ArrayLike:
20262017 """
20272018 Reshape if possible to have values.ndim == ndim.
20282019 """
2020+
20292021 if values .ndim < ndim :
2030- if not is_extension_array_dtype (values .dtype ):
2022+ if not is_1d_only_ea_dtype (values .dtype ):
20312023 # TODO(EA2D): https://github.com/pandas-dev/pandas/issues/23023
20322024 # block.shape is incorrect for "2D" ExtensionArrays
20332025 # We can't, and don't need to, reshape.
0 commit comments