4
4
from copy import copy
5
5
import csv
6
6
from enum import Enum
7
+ import itertools
7
8
from typing import (
8
9
TYPE_CHECKING ,
9
10
Any ,
@@ -271,7 +272,7 @@ def _maybe_make_multi_index_columns(
271
272
272
273
@final
273
274
def _make_index (
274
- self , data , alldata , columns , indexnamerow : list [Scalar ] | None = None
275
+ self , alldata , columns , indexnamerow : list [Scalar ] | None = None
275
276
) -> tuple [Index | None , Sequence [Hashable ] | MultiIndex ]:
276
277
index : Index | None
277
278
if isinstance (self .index_col , list ) and len (self .index_col ):
@@ -326,7 +327,11 @@ def _agg_index(self, index) -> Index:
326
327
converters = self ._clean_mapping (self .converters )
327
328
clean_dtypes = self ._clean_mapping (self .dtype )
328
329
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 )):
330
335
if self ._should_parse_dates (i ):
331
336
arr = date_converter (
332
337
arr ,
@@ -369,12 +374,17 @@ def _agg_index(self, index) -> Index:
369
374
arr , _ = self ._infer_types (
370
375
arr , col_na_values | col_na_fvalues , cast_type is None , try_num_bool
371
376
)
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 )
376
383
377
- return index
384
+ if len (arrays ) == 1 :
385
+ return arrays [0 ]
386
+ else :
387
+ return MultiIndex .from_arrays (arrays )
378
388
379
389
@final
380
390
def _set_noconvert_dtype_columns (
@@ -704,12 +714,11 @@ def _get_empty_meta(
704
714
dtype_dict : defaultdict [Hashable , Any ]
705
715
if not is_dict_like (dtype ):
706
716
# 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 )
709
718
else :
710
719
dtype = cast (dict , dtype )
711
720
dtype_dict = defaultdict (
712
- lambda : object ,
721
+ lambda : None ,
713
722
{columns [k ] if is_integer (k ) else k : v for k , v in dtype .items ()},
714
723
)
715
724
@@ -726,8 +735,14 @@ def _get_empty_meta(
726
735
if (index_col is None or index_col is False ) or index_names is None :
727
736
index = default_index (0 )
728
737
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 )
731
746
index_col .sort ()
732
747
733
748
for i , n in enumerate (index_col ):
0 commit comments