47
47
from pandas .core .dtypes .dtypes import ExtensionDtype
48
48
from pandas .core .dtypes .generic import (
49
49
ABCDataFrame ,
50
- ABCDatetimeIndex ,
51
- ABCIndex ,
52
50
ABCSeries ,
53
- ABCTimedeltaIndex ,
54
51
)
55
52
56
53
from pandas .core import (
71
68
)
72
69
from pandas .core .indexes import base as ibase
73
70
from pandas .core .indexes .api import (
71
+ DatetimeIndex ,
74
72
Index ,
73
+ TimedeltaIndex ,
75
74
ensure_index ,
76
75
get_objs_combined_axis ,
77
76
union_indexes ,
101
100
102
101
def arrays_to_mgr (
103
102
arrays ,
104
- arr_names ,
103
+ arr_names : Index ,
105
104
index ,
106
105
columns ,
107
106
* ,
@@ -115,8 +114,6 @@ def arrays_to_mgr(
115
114
116
115
Needs to handle a lot of exceptional cases.
117
116
"""
118
- arr_names = ensure_index (arr_names )
119
-
120
117
if verify_integrity :
121
118
# figure out the index, if necessary
122
119
if index is None :
@@ -286,10 +283,12 @@ def ndarray_to_mgr(
286
283
287
284
if columns is None :
288
285
columns = Index (range (len (values )))
286
+ else :
287
+ columns = ensure_index (columns )
289
288
290
289
return arrays_to_mgr (values , columns , index , columns , dtype = dtype , typ = typ )
291
290
292
- if is_extension_array_dtype (vdtype ) and not is_1d_only_ea_dtype (vdtype ):
291
+ elif is_extension_array_dtype (vdtype ) and not is_1d_only_ea_dtype (vdtype ):
293
292
# i.e. Datetime64TZ
294
293
values = extract_array (values , extract_numpy = True )
295
294
if copy :
@@ -454,7 +453,7 @@ def dict_to_mgr(
454
453
arrays = [com .maybe_iterable_to_list (data [k ]) for k in keys ]
455
454
# GH#24096 need copy to be deep for datetime64tz case
456
455
# TODO: See if we can avoid these copies
457
- arrays = [arr if not isinstance (arr , ABCIndex ) else arr ._data for arr in arrays ]
456
+ arrays = [arr if not isinstance (arr , Index ) else arr ._data for arr in arrays ]
458
457
arrays = [
459
458
arr if not is_datetime64tz_dtype (arr ) else arr .copy () for arr in arrays
460
459
]
@@ -480,7 +479,7 @@ def nested_data_to_arrays(
480
479
columns : Index | None ,
481
480
index : Index | None ,
482
481
dtype : DtypeObj | None ,
483
- ):
482
+ ) -> tuple [ list [ ArrayLike ], Index , Index ] :
484
483
"""
485
484
Convert a single sequence of arrays to multiple arrays.
486
485
"""
@@ -548,7 +547,7 @@ def convert(v):
548
547
if is_list_like (values [0 ]):
549
548
values = np .array ([convert (v ) for v in values ])
550
549
elif isinstance (values [0 ], np .ndarray ) and values [0 ].ndim == 0 :
551
- # GH#21861
550
+ # GH#21861 see test_constructor_list_of_lists
552
551
values = np .array ([convert (v ) for v in values ])
553
552
else :
554
553
values = convert (values )
@@ -566,31 +565,30 @@ def convert(v):
566
565
return values
567
566
568
567
569
- def _homogenize (data , index : Index , dtype : DtypeObj | None ):
568
+ def _homogenize (data , index : Index , dtype : DtypeObj | None ) -> list [ ArrayLike ] :
570
569
oindex = None
571
570
homogenized = []
572
571
573
572
for val in data :
574
573
if isinstance (val , ABCSeries ):
575
574
if dtype is not None :
576
- val = val .astype (dtype )
575
+ val = val .astype (dtype , copy = False )
577
576
if val .index is not index :
578
577
# Forces alignment. No need to copy data since we
579
578
# are putting it into an ndarray later
580
579
val = val .reindex (index , copy = False )
581
- # TODO extract_array should be preferred, but that gives failures for
582
- # `extension/test_numpy.py` (extract_array will convert numpy arrays
583
- # to PandasArray), see https://github.com/pandas-dev/pandas/issues/40021
584
- # val = extract_array(val, extract_numpy=True)
580
+
585
581
val = val ._values
586
582
else :
587
583
if isinstance (val , dict ):
588
584
if oindex is None :
589
585
oindex = index .astype ("O" )
590
586
591
- if isinstance (index , (ABCDatetimeIndex , ABCTimedeltaIndex )):
587
+ if isinstance (index , (DatetimeIndex , TimedeltaIndex )):
588
+ # see test_constructor_dict_datetime64_index
592
589
val = dict_compat (val )
593
590
else :
591
+ # see test_constructor_subclass_dict
594
592
val = dict (val )
595
593
val = lib .fast_multiget (val , oindex ._values , default = np .nan )
596
594
val = sanitize_array (
@@ -749,6 +747,7 @@ def to_arrays(
749
747
Return list of arrays, columns.
750
748
"""
751
749
if isinstance (data , ABCDataFrame ):
750
+ # see test_from_records_with_index_data, test_from_records_bad_index_column
752
751
if columns is not None :
753
752
arrays = [
754
753
data ._ixs (i , axis = 1 ).values
@@ -884,7 +883,7 @@ def _list_of_dict_to_arrays(
884
883
885
884
# assure that they are of the base dict class and not of derived
886
885
# classes
887
- data = [( type (d ) is dict ) and d or dict (d ) for d in data ]
886
+ data = [d if type (d ) is dict else dict (d ) for d in data ]
888
887
889
888
content = lib .dicts_to_array (data , list (columns ))
890
889
return content , columns
0 commit comments