@@ -274,46 +274,34 @@ def _make_index(
274
274
self , data , alldata , columns , indexnamerow : list [Scalar ] | None = None
275
275
) -> tuple [Index | None , Sequence [Hashable ] | MultiIndex ]:
276
276
index : Index | None
277
- if not is_index_col (self .index_col ) or not self .index_col :
278
- index = None
277
+ if isinstance (self .index_col , list ) and len (self .index_col ):
278
+ to_remove = []
279
+ indexes = []
280
+ for idx in self .index_col :
281
+ if isinstance (idx , str ):
282
+ raise ValueError (f"Index { idx } invalid" )
283
+ to_remove .append (idx )
284
+ indexes .append (alldata [idx ])
285
+ # remove index items from content and columns, don't pop in
286
+ # loop
287
+ for i in sorted (to_remove , reverse = True ):
288
+ alldata .pop (i )
289
+ if not self ._implicit_index :
290
+ columns .pop (i )
291
+ index = self ._agg_index (indexes )
292
+
293
+ # add names for the index
294
+ if indexnamerow :
295
+ coffset = len (indexnamerow ) - len (columns )
296
+ index = index .set_names (indexnamerow [:coffset ])
279
297
else :
280
- simple_index = self ._get_simple_index (alldata , columns )
281
- index = self ._agg_index (simple_index )
282
-
283
- # add names for the index
284
- if indexnamerow :
285
- coffset = len (indexnamerow ) - len (columns )
286
- assert index is not None
287
- index = index .set_names (indexnamerow [:coffset ])
298
+ index = None
288
299
289
300
# maybe create a mi on the columns
290
301
columns = self ._maybe_make_multi_index_columns (columns , self .col_names )
291
302
292
303
return index , columns
293
304
294
- @final
295
- def _get_simple_index (self , data , columns ):
296
- def ix (col ):
297
- if not isinstance (col , str ):
298
- return col
299
- raise ValueError (f"Index { col } invalid" )
300
-
301
- to_remove = []
302
- index = []
303
- for idx in self .index_col :
304
- i = ix (idx )
305
- to_remove .append (i )
306
- index .append (data [i ])
307
-
308
- # remove index items from content and columns, don't pop in
309
- # loop
310
- for i in sorted (to_remove , reverse = True ):
311
- data .pop (i )
312
- if not self ._implicit_index :
313
- columns .pop (i )
314
-
315
- return index
316
-
317
305
@final
318
306
def _clean_mapping (self , mapping ):
319
307
"""converts col numbers to names"""
@@ -333,12 +321,13 @@ def _clean_mapping(self, mapping):
333
321
return clean
334
322
335
323
@final
336
- def _agg_index (self , index , try_parse_dates : bool = True ) -> Index :
324
+ def _agg_index (self , index ) -> Index :
337
325
arrays = []
338
326
converters = self ._clean_mapping (self .converters )
327
+ clean_dtypes = self ._clean_mapping (self .dtype )
339
328
340
329
for i , arr in enumerate (index ):
341
- if try_parse_dates and self ._should_parse_dates (i ):
330
+ if self ._should_parse_dates (i ):
342
331
arr = date_converter (
343
332
arr ,
344
333
col = self .index_names [i ] if self .index_names is not None else None ,
@@ -364,8 +353,6 @@ def _agg_index(self, index, try_parse_dates: bool = True) -> Index:
364
353
else :
365
354
col_na_values , col_na_fvalues = set (), set ()
366
355
367
- clean_dtypes = self ._clean_mapping (self .dtype )
368
-
369
356
cast_type = None
370
357
index_converter = False
371
358
if self .index_names is not None :
@@ -632,35 +619,6 @@ def _check_data_length(
632
619
stacklevel = find_stack_level (),
633
620
)
634
621
635
- @overload
636
- def _evaluate_usecols (
637
- self ,
638
- usecols : Callable [[Hashable ], object ],
639
- names : Iterable [Hashable ],
640
- ) -> set [int ]: ...
641
-
642
- @overload
643
- def _evaluate_usecols (
644
- self , usecols : SequenceT , names : Iterable [Hashable ]
645
- ) -> SequenceT : ...
646
-
647
- @final
648
- def _evaluate_usecols (
649
- self ,
650
- usecols : Callable [[Hashable ], object ] | SequenceT ,
651
- names : Iterable [Hashable ],
652
- ) -> SequenceT | set [int ]:
653
- """
654
- Check whether or not the 'usecols' parameter
655
- is a callable. If so, enumerates the 'names'
656
- parameter and returns a set of indices for
657
- each entry in 'names' that evaluates to True.
658
- If not a callable, returns 'usecols'.
659
- """
660
- if callable (usecols ):
661
- return {i for i , name in enumerate (names ) if usecols (name )}
662
- return usecols
663
-
664
622
@final
665
623
def _validate_usecols_names (self , usecols : SequenceT , names : Sequence ) -> SequenceT :
666
624
"""
@@ -988,3 +946,32 @@ def _validate_usecols_arg(usecols):
988
946
989
947
return usecols , usecols_dtype
990
948
return usecols , None
949
+
950
+
951
+ @overload
952
+ def evaluate_callable_usecols (
953
+ usecols : Callable [[Hashable ], object ],
954
+ names : Iterable [Hashable ],
955
+ ) -> set [int ]: ...
956
+
957
+
958
+ @overload
959
+ def evaluate_callable_usecols (
960
+ usecols : SequenceT , names : Iterable [Hashable ]
961
+ ) -> SequenceT : ...
962
+
963
+
964
+ def evaluate_callable_usecols (
965
+ usecols : Callable [[Hashable ], object ] | SequenceT ,
966
+ names : Iterable [Hashable ],
967
+ ) -> SequenceT | set [int ]:
968
+ """
969
+ Check whether or not the 'usecols' parameter
970
+ is a callable. If so, enumerates the 'names'
971
+ parameter and returns a set of indices for
972
+ each entry in 'names' that evaluates to True.
973
+ If not a callable, returns 'usecols'.
974
+ """
975
+ if callable (usecols ):
976
+ return {i for i , name in enumerate (names ) if usecols (name )}
977
+ return usecols
0 commit comments