@@ -464,7 +464,11 @@ def _agg_index(self, index, try_parse_dates: bool = True) -> Index:
464
464
arrays = []
465
465
converters = self ._clean_mapping (self .converters )
466
466
467
- for i , arr in enumerate (index ):
467
+ if self .index_names is not None :
468
+ names : Iterable = self .index_names
469
+ else :
470
+ names = itertools .cycle ([None ])
471
+ for i , (arr , name ) in enumerate (zip (index , names )):
468
472
if try_parse_dates and self ._should_parse_dates (i ):
469
473
arr = self ._date_conv (
470
474
arr ,
@@ -504,12 +508,17 @@ def _agg_index(self, index, try_parse_dates: bool = True) -> Index:
504
508
arr , _ = self ._infer_types (
505
509
arr , col_na_values | col_na_fvalues , cast_type is None , try_num_bool
506
510
)
507
- arrays .append (arr )
508
-
509
- names = self .index_names
510
- index = ensure_index_from_sequences (arrays , names )
511
+ if cast_type is not None :
512
+ # Don't perform RangeIndex inference
513
+ idx = Index (arr , name = name , dtype = cast_type )
514
+ else :
515
+ idx = ensure_index_from_sequences ([arr ], [name ])
516
+ arrays .append (idx )
511
517
512
- return index
518
+ if len (arrays ) == 1 :
519
+ return arrays [0 ]
520
+ else :
521
+ return MultiIndex .from_arrays (arrays )
513
522
514
523
@final
515
524
def _convert_to_ndarrays (
@@ -1084,12 +1093,11 @@ def _get_empty_meta(self, columns, dtype: DtypeArg | None = None):
1084
1093
dtype_dict : defaultdict [Hashable , Any ]
1085
1094
if not is_dict_like (dtype ):
1086
1095
# if dtype == None, default will be object.
1087
- default_dtype = dtype or object
1088
- dtype_dict = defaultdict (lambda : default_dtype )
1096
+ dtype_dict = defaultdict (lambda : dtype )
1089
1097
else :
1090
1098
dtype = cast (dict , dtype )
1091
1099
dtype_dict = defaultdict (
1092
- lambda : object ,
1100
+ lambda : None ,
1093
1101
{columns [k ] if is_integer (k ) else k : v for k , v in dtype .items ()},
1094
1102
)
1095
1103
@@ -1106,8 +1114,14 @@ def _get_empty_meta(self, columns, dtype: DtypeArg | None = None):
1106
1114
if (index_col is None or index_col is False ) or index_names is None :
1107
1115
index = default_index (0 )
1108
1116
else :
1109
- data = [Series ([], dtype = dtype_dict [name ]) for name in index_names ]
1110
- index = ensure_index_from_sequences (data , names = index_names )
1117
+ # TODO: We could return default_index(0) if dtype_dict[name] is None
1118
+ data = [
1119
+ Index ([], name = name , dtype = dtype_dict [name ]) for name in index_names
1120
+ ]
1121
+ if len (data ) == 1 :
1122
+ index = data [0 ]
1123
+ else :
1124
+ index = MultiIndex .from_arrays (data )
1111
1125
index_col .sort ()
1112
1126
1113
1127
for i , n in enumerate (index_col ):
0 commit comments