79
79
if TYPE_CHECKING :
80
80
from numpy .ma .mrecords import MaskedRecords
81
81
82
+
82
83
# ---------------------------------------------------------------------
83
84
# BlockManager Interface
84
85
@@ -91,7 +92,7 @@ def arrays_to_mgr(
91
92
dtype : Optional [DtypeObj ] = None ,
92
93
verify_integrity : bool = True ,
93
94
typ : Optional [str ] = None ,
94
- ):
95
+ ) -> Manager :
95
96
"""
96
97
Segregate Series based on type and coerce into matrices.
97
98
@@ -109,11 +110,11 @@ def arrays_to_mgr(
109
110
# don't force copy because getting jammed in an ndarray anyway
110
111
arrays = _homogenize (arrays , index , dtype )
111
112
112
- columns = ensure_index (columns )
113
113
else :
114
- columns = ensure_index (columns )
115
114
index = ensure_index (index )
116
115
116
+ columns = ensure_index (columns )
117
+
117
118
# from BlockManager perspective
118
119
axes = [columns , index ]
119
120
@@ -140,9 +141,8 @@ def rec_array_to_mgr(
140
141
fdata = ma .getdata (data )
141
142
if index is None :
142
143
index = _get_names_from_index (fdata )
143
- if index is None :
144
- index = ibase .default_index (len (data ))
145
- index = ensure_index (index )
144
+ else :
145
+ index = ensure_index (index )
146
146
147
147
if columns is not None :
148
148
columns = ensure_index (columns )
@@ -215,14 +215,14 @@ def mgr_to_mgr(mgr, typ: str):
215
215
216
216
def ndarray_to_mgr (
217
217
values , index , columns , dtype : Optional [DtypeObj ], copy : bool , typ : str
218
- ):
218
+ ) -> Manager :
219
219
# used in DataFrame.__init__
220
- # input must be a ndarray, list, Series, index
220
+ # input must be a ndarray, list, Series, Index, ExtensionArray
221
221
222
222
if isinstance (values , ABCSeries ):
223
223
if columns is None :
224
224
if values .name is not None :
225
- columns = [values .name ]
225
+ columns = Index ( [values .name ])
226
226
if index is None :
227
227
index = values .index
228
228
else :
@@ -309,7 +309,9 @@ def ndarray_to_mgr(
309
309
return create_block_manager_from_blocks (block_values , [columns , index ])
310
310
311
311
312
- def dict_to_mgr (data : Dict , index , columns , dtype : Optional [DtypeObj ], typ : str ):
312
+ def dict_to_mgr (
313
+ data : Dict , index , columns , dtype : Optional [DtypeObj ], typ : str
314
+ ) -> Manager :
313
315
"""
314
316
Segregate Series based on type and coerce into matrices.
315
317
Needs to handle a lot of exceptional cases.
@@ -531,21 +533,18 @@ def extract_index(data) -> Index:
531
533
return ensure_index (index )
532
534
533
535
534
- def reorder_arrays (arrays , arr_columns , columns ):
536
+ def reorder_arrays (
537
+ arrays : List [ArrayLike ], arr_columns : Index , columns : Optional [Index ]
538
+ ) -> Tuple [List [ArrayLike ], Index ]:
535
539
# reorder according to the columns
536
- if (
537
- columns is not None
538
- and len (columns )
539
- and arr_columns is not None
540
- and len (arr_columns )
541
- ):
540
+ if columns is not None and len (columns ) and len (arr_columns ):
542
541
indexer = ensure_index (arr_columns ).get_indexer (columns )
543
542
arr_columns = ensure_index ([arr_columns [i ] for i in indexer ])
544
543
arrays = [arrays [i ] for i in indexer ]
545
544
return arrays , arr_columns
546
545
547
546
548
- def _get_names_from_index (data ):
547
+ def _get_names_from_index (data ) -> Index :
549
548
has_some_name = any (getattr (s , "name" , None ) is not None for s in data )
550
549
if not has_some_name :
551
550
return ibase .default_index (len (data ))
@@ -560,7 +559,7 @@ def _get_names_from_index(data):
560
559
index [i ] = f"Unnamed { count } "
561
560
count += 1
562
561
563
- return index
562
+ return Index ( index )
564
563
565
564
566
565
def _get_axes (
0 commit comments