45
45
from pandas .core .dtypes .dtypes import ExtensionDtype
46
46
from pandas .core .dtypes .generic import (
47
47
ABCDataFrame ,
48
- ABCPandasArray ,
49
48
ABCSeries ,
50
49
)
51
50
from pandas .core .dtypes .missing import (
@@ -316,6 +315,8 @@ def __getstate__(self):
316
315
def __setstate__ (self , state ):
317
316
def unpickle_block (values , mgr_locs , ndim : int ):
318
317
# TODO(EA2D): ndim would be unnecessary with 2D EAs
318
+ # older pickles may store e.g. DatetimeIndex instead of DatetimeArray
319
+ values = extract_array (values , extract_numpy = True )
319
320
return make_block (values , placement = mgr_locs , ndim = ndim )
320
321
321
322
if isinstance (state , tuple ) and len (state ) >= 4 and "0.14.1" in state [3 ]:
@@ -1212,6 +1213,7 @@ def insert(self, loc: int, item: Hashable, value, allow_duplicates: bool = False
1212
1213
# TODO(EA2D): special case not needed with 2D EAs
1213
1214
value = ensure_block_shape (value , ndim = 2 )
1214
1215
1216
+ # TODO: type value as ArrayLike
1215
1217
block = make_block (values = value , ndim = self .ndim , placement = slice (loc , loc + 1 ))
1216
1218
1217
1219
for blkno , count in _fast_count_smallints (self .blknos [loc :]):
@@ -1673,16 +1675,20 @@ def create_block_manager_from_blocks(blocks, axes: List[Index]) -> BlockManager:
1673
1675
raise construction_error (tot_items , blocks [0 ].shape [1 :], axes , e )
1674
1676
1675
1677
1678
+ # We define this here so we can override it in tests.extension.test_numpy
1679
+ def _extract_array (obj ):
1680
+ return extract_array (obj , extract_numpy = True )
1681
+
1682
+
1676
1683
def create_block_manager_from_arrays (
1677
1684
arrays , names : Index , axes : List [Index ]
1678
1685
) -> BlockManager :
1679
1686
assert isinstance (names , Index )
1680
1687
assert isinstance (axes , list )
1681
1688
assert all (isinstance (x , Index ) for x in axes )
1682
1689
1683
- # ensure we dont have any PandasArrays when we call get_block_type
1684
- # Note: just calling extract_array breaks tests that patch PandasArray._typ.
1685
- arrays = [x if not isinstance (x , ABCPandasArray ) else x .to_numpy () for x in arrays ]
1690
+ arrays = [_extract_array (x ) for x in arrays ]
1691
+
1686
1692
try :
1687
1693
blocks = _form_blocks (arrays , names , axes )
1688
1694
mgr = BlockManager (blocks , axes )
@@ -1692,7 +1698,12 @@ def create_block_manager_from_arrays(
1692
1698
raise construction_error (len (arrays ), arrays [0 ].shape , axes , e )
1693
1699
1694
1700
1695
- def construction_error (tot_items , block_shape , axes , e = None ):
1701
+ def construction_error (
1702
+ tot_items : int ,
1703
+ block_shape : Shape ,
1704
+ axes : List [Index ],
1705
+ e : Optional [ValueError ] = None ,
1706
+ ):
1696
1707
""" raise a helpful message about our construction """
1697
1708
passed = tuple (map (int , [tot_items ] + list (block_shape )))
1698
1709
# Correcting the user facing error message during dataframe construction
@@ -1716,7 +1727,9 @@ def construction_error(tot_items, block_shape, axes, e=None):
1716
1727
# -----------------------------------------------------------------------
1717
1728
1718
1729
1719
- def _form_blocks (arrays , names : Index , axes : List [Index ]) -> List [Block ]:
1730
+ def _form_blocks (
1731
+ arrays : List [ArrayLike ], names : Index , axes : List [Index ]
1732
+ ) -> List [Block ]:
1720
1733
# put "leftover" items in float bucket, where else?
1721
1734
# generalize?
1722
1735
items_dict : DefaultDict [str , List ] = defaultdict (list )
@@ -1836,21 +1849,14 @@ def _multi_blockify(tuples, dtype: Optional[Dtype] = None):
1836
1849
1837
1850
def _stack_arrays (tuples , dtype : np .dtype ):
1838
1851
1839
- # fml
1840
- def _asarray_compat (x ):
1841
- if isinstance (x , ABCSeries ):
1842
- return x ._values
1843
- else :
1844
- return np .asarray (x )
1845
-
1846
1852
placement , arrays = zip (* tuples )
1847
1853
1848
1854
first = arrays [0 ]
1849
1855
shape = (len (arrays ),) + first .shape
1850
1856
1851
1857
stacked = np .empty (shape , dtype = dtype )
1852
1858
for i , arr in enumerate (arrays ):
1853
- stacked [i ] = _asarray_compat ( arr )
1859
+ stacked [i ] = arr
1854
1860
1855
1861
return stacked , placement
1856
1862
@@ -1874,7 +1880,7 @@ def _interleaved_dtype(blocks: Sequence[Block]) -> Optional[DtypeObj]:
1874
1880
return find_common_type ([b .dtype for b in blocks ])
1875
1881
1876
1882
1877
- def _consolidate (blocks ) :
1883
+ def _consolidate (blocks : Tuple [ Block , ...]) -> List [ Block ] :
1878
1884
"""
1879
1885
Merge blocks having same dtype, exclude non-consolidating blocks
1880
1886
"""
0 commit comments