44from copy import copy
55import csv
66from enum import Enum
7+ import itertools
78from typing import (
89 TYPE_CHECKING ,
910 Any ,
@@ -271,7 +272,7 @@ def _maybe_make_multi_index_columns(
271272
272273 @final
273274 def _make_index (
274- self , data , alldata , columns , indexnamerow : list [Scalar ] | None = None
275+ self , alldata , columns , indexnamerow : list [Scalar ] | None = None
275276 ) -> tuple [Index | None , Sequence [Hashable ] | MultiIndex ]:
276277 index : Index | None
277278 if isinstance (self .index_col , list ) and len (self .index_col ):
@@ -326,7 +327,11 @@ def _agg_index(self, index) -> Index:
326327 converters = self ._clean_mapping (self .converters )
327328 clean_dtypes = self ._clean_mapping (self .dtype )
328329
329- for i , arr in enumerate (index ):
330+ if self .index_names is not None :
331+ names : Iterable = self .index_names
332+ else :
333+ names = itertools .cycle ([None ])
334+ for i , (arr , name ) in enumerate (zip (index , names )):
330335 if self ._should_parse_dates (i ):
331336 arr = date_converter (
332337 arr ,
@@ -369,12 +374,17 @@ def _agg_index(self, index) -> Index:
369374 arr , _ = self ._infer_types (
370375 arr , col_na_values | col_na_fvalues , cast_type is None , try_num_bool
371376 )
372- arrays .append (arr )
373-
374- names = self .index_names
375- index = ensure_index_from_sequences (arrays , names )
377+ if cast_type is not None :
378+ # Don't perform RangeIndex inference
379+ idx = Index (arr , name = name , dtype = cast_type )
380+ else :
381+ idx = ensure_index_from_sequences ([arr ], [name ])
382+ arrays .append (idx )
376383
377- return index
384+ if len (arrays ) == 1 :
385+ return arrays [0 ]
386+ else :
387+ return MultiIndex .from_arrays (arrays )
378388
379389 @final
380390 def _set_noconvert_dtype_columns (
@@ -704,12 +714,11 @@ def _get_empty_meta(
704714 dtype_dict : defaultdict [Hashable , Any ]
705715 if not is_dict_like (dtype ):
706716 # if dtype == None, default will be object.
707- default_dtype = dtype or object
708- dtype_dict = defaultdict (lambda : default_dtype )
717+ dtype_dict = defaultdict (lambda : dtype )
709718 else :
710719 dtype = cast (dict , dtype )
711720 dtype_dict = defaultdict (
712- lambda : object ,
721+ lambda : None ,
713722 {columns [k ] if is_integer (k ) else k : v for k , v in dtype .items ()},
714723 )
715724
@@ -726,8 +735,14 @@ def _get_empty_meta(
726735 if (index_col is None or index_col is False ) or index_names is None :
727736 index = default_index (0 )
728737 else :
729- data = [Series ([], dtype = dtype_dict [name ]) for name in index_names ]
730- index = ensure_index_from_sequences (data , names = index_names )
738+ # TODO: We could return default_index(0) if dtype_dict[name] is None
739+ data = [
740+ Index ([], name = name , dtype = dtype_dict [name ]) for name in index_names
741+ ]
742+ if len (data ) == 1 :
743+ index = data [0 ]
744+ else :
745+ index = MultiIndex .from_arrays (data )
731746 index_col .sort ()
732747
733748 for i , n in enumerate (index_col ):
0 commit comments