@@ -464,7 +464,11 @@ def _agg_index(self, index, try_parse_dates: bool = True) -> Index:
464464 arrays = []
465465 converters = self ._clean_mapping (self .converters )
466466
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 )):
468472 if try_parse_dates and self ._should_parse_dates (i ):
469473 arr = self ._date_conv (
470474 arr ,
@@ -504,12 +508,17 @@ def _agg_index(self, index, try_parse_dates: bool = True) -> Index:
504508 arr , _ = self ._infer_types (
505509 arr , col_na_values | col_na_fvalues , cast_type is None , try_num_bool
506510 )
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 )
511517
512- return index
518+ if len (arrays ) == 1 :
519+ return arrays [0 ]
520+ else :
521+ return MultiIndex .from_arrays (arrays )
513522
514523 @final
515524 def _convert_to_ndarrays (
@@ -1084,12 +1093,11 @@ def _get_empty_meta(self, columns, dtype: DtypeArg | None = None):
10841093 dtype_dict : defaultdict [Hashable , Any ]
10851094 if not is_dict_like (dtype ):
10861095 # 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 )
10891097 else :
10901098 dtype = cast (dict , dtype )
10911099 dtype_dict = defaultdict (
1092- lambda : object ,
1100+ lambda : None ,
10931101 {columns [k ] if is_integer (k ) else k : v for k , v in dtype .items ()},
10941102 )
10951103
@@ -1106,8 +1114,14 @@ def _get_empty_meta(self, columns, dtype: DtypeArg | None = None):
11061114 if (index_col is None or index_col is False ) or index_names is None :
11071115 index = default_index (0 )
11081116 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 )
11111125 index_col .sort ()
11121126
11131127 for i , n in enumerate (index_col ):
0 commit comments