2
2
import itertools
3
3
import operator
4
4
import re
5
- from typing import Dict , List , Optional , Sequence , Tuple , TypeVar , Union
5
+ from typing import DefaultDict , Dict , List , Optional , Sequence , Tuple , TypeVar , Union
6
6
import warnings
7
7
8
8
import numpy as np
@@ -341,7 +341,7 @@ def _verify_integrity(self) -> None:
341
341
tot_items = sum (len (x .mgr_locs ) for x in self .blocks )
342
342
for block in self .blocks :
343
343
if block ._verify_integrity and block .shape [1 :] != mgr_shape [1 :]:
344
- construction_error (tot_items , block .shape [1 :], self .axes )
344
+ raise construction_error (tot_items , block .shape [1 :], self .axes )
345
345
if len (self .items ) != tot_items :
346
346
raise AssertionError (
347
347
"Number of manager items must equal union of "
@@ -1648,7 +1648,7 @@ def concat(
1648
1648
# Constructor Helpers
1649
1649
1650
1650
1651
- def create_block_manager_from_blocks (blocks , axes ) :
1651
+ def create_block_manager_from_blocks (blocks , axes : List [ Index ]) -> BlockManager :
1652
1652
try :
1653
1653
if len (blocks ) == 1 and not isinstance (blocks [0 ], Block ):
1654
1654
# if blocks[0] is of length 0, return empty blocks
@@ -1669,18 +1669,23 @@ def create_block_manager_from_blocks(blocks, axes):
1669
1669
except ValueError as e :
1670
1670
blocks = [getattr (b , "values" , b ) for b in blocks ]
1671
1671
tot_items = sum (b .shape [0 ] for b in blocks )
1672
- construction_error (tot_items , blocks [0 ].shape [1 :], axes , e )
1672
+ raise construction_error (tot_items , blocks [0 ].shape [1 :], axes , e )
1673
1673
1674
1674
1675
- def create_block_manager_from_arrays (arrays , names , axes ):
1675
+ def create_block_manager_from_arrays (
1676
+ arrays , names : Index , axes : List [Index ]
1677
+ ) -> BlockManager :
1678
+ assert isinstance (names , Index )
1679
+ assert isinstance (axes , list )
1680
+ assert all (isinstance (x , Index ) for x in axes )
1676
1681
1677
1682
try :
1678
1683
blocks = form_blocks (arrays , names , axes )
1679
1684
mgr = BlockManager (blocks , axes )
1680
1685
mgr ._consolidate_inplace ()
1681
1686
return mgr
1682
1687
except ValueError as e :
1683
- construction_error (len (arrays ), arrays [0 ].shape , axes , e )
1688
+ raise construction_error (len (arrays ), arrays [0 ].shape , axes , e )
1684
1689
1685
1690
1686
1691
def construction_error (tot_items , block_shape , axes , e = None ):
@@ -1695,23 +1700,25 @@ def construction_error(tot_items, block_shape, axes, e=None):
1695
1700
if len (implied ) <= 2 :
1696
1701
implied = implied [::- 1 ]
1697
1702
1703
+ # We return the exception object instead of raising it so that we
1704
+ # can raise it in the caller; mypy plays better with that
1698
1705
if passed == implied and e is not None :
1699
- raise e
1706
+ return e
1700
1707
if block_shape [0 ] == 0 :
1701
- raise ValueError ("Empty data passed with indices specified." )
1702
- raise ValueError (f"Shape of passed values is { passed } , indices imply { implied } " )
1708
+ return ValueError ("Empty data passed with indices specified." )
1709
+ return ValueError (f"Shape of passed values is { passed } , indices imply { implied } " )
1703
1710
1704
1711
1705
1712
# -----------------------------------------------------------------------
1706
1713
1707
1714
1708
- def form_blocks (arrays , names , axes ):
1715
+ def form_blocks (arrays , names : Index , axes ) -> List [ Block ] :
1709
1716
# put "leftover" items in float bucket, where else?
1710
1717
# generalize?
1711
- items_dict = defaultdict (list )
1718
+ items_dict : DefaultDict [ str , List ] = defaultdict (list )
1712
1719
extra_locs = []
1713
1720
1714
- names_idx = ensure_index ( names )
1721
+ names_idx = names
1715
1722
if names_idx .equals (axes [0 ]):
1716
1723
names_indexer = np .arange (len (names_idx ))
1717
1724
else :
@@ -1729,7 +1736,7 @@ def form_blocks(arrays, names, axes):
1729
1736
block_type = get_block_type (v )
1730
1737
items_dict [block_type .__name__ ].append ((i , k , v ))
1731
1738
1732
- blocks = []
1739
+ blocks : List [ Block ] = []
1733
1740
if len (items_dict ["FloatBlock" ]):
1734
1741
float_blocks = _multi_blockify (items_dict ["FloatBlock" ])
1735
1742
blocks .extend (float_blocks )
0 commit comments