@@ -2301,8 +2301,8 @@ def maybe_reorder(
23012301 exclude .update (index )
23022302
23032303 if any (exclude ):
2304- arr_exclude = [ x for x in exclude if x in arr_columns ]
2305- to_remove = [ arr_columns .get_loc (col ) for col in arr_exclude ]
2304+ arr_exclude = ( x for x in exclude if x in arr_columns )
2305+ to_remove = { arr_columns .get_loc (col ) for col in arr_exclude }
23062306 arrays = [v for i , v in enumerate (arrays ) if i not in to_remove ]
23072307
23082308 columns = columns .drop (exclude )
@@ -3705,7 +3705,7 @@ def transpose(
37053705 nv .validate_transpose (args , {})
37063706 # construct the args
37073707
3708- dtypes = list ( self .dtypes )
3708+ first_dtype = self .dtypes . iloc [ 0 ] if len ( self . columns ) else None
37093709
37103710 if self ._can_fast_transpose :
37113711 # Note: tests pass without this, but this improves perf quite a bit.
@@ -3723,11 +3723,11 @@ def transpose(
37233723
37243724 elif (
37253725 self ._is_homogeneous_type
3726- and dtypes
3727- and isinstance (dtypes [ 0 ] , ExtensionDtype )
3726+ and first_dtype is not None
3727+ and isinstance (first_dtype , ExtensionDtype )
37283728 ):
37293729 new_values : list
3730- if isinstance (dtypes [ 0 ] , BaseMaskedDtype ):
3730+ if isinstance (first_dtype , BaseMaskedDtype ):
37313731 # We have masked arrays with the same dtype. We can transpose faster.
37323732 from pandas .core .arrays .masked import (
37333733 transpose_homogeneous_masked_arrays ,
@@ -3736,7 +3736,7 @@ def transpose(
37363736 new_values = transpose_homogeneous_masked_arrays (
37373737 cast (Sequence [BaseMaskedArray ], self ._iter_column_arrays ())
37383738 )
3739- elif isinstance (dtypes [ 0 ] , ArrowDtype ):
3739+ elif isinstance (first_dtype , ArrowDtype ):
37403740 # We have arrow EAs with the same dtype. We can transpose faster.
37413741 from pandas .core .arrays .arrow .array import (
37423742 ArrowExtensionArray ,
@@ -3748,10 +3748,11 @@ def transpose(
37483748 )
37493749 else :
37503750 # We have other EAs with the same dtype. We preserve dtype in transpose.
3751- dtyp = dtypes [0 ]
3752- arr_typ = dtyp .construct_array_type ()
3751+ arr_typ = first_dtype .construct_array_type ()
37533752 values = self .values
3754- new_values = [arr_typ ._from_sequence (row , dtype = dtyp ) for row in values ]
3753+ new_values = [
3754+ arr_typ ._from_sequence (row , dtype = first_dtype ) for row in values
3755+ ]
37553756
37563757 result = type (self )._from_arrays (
37573758 new_values ,
@@ -5882,7 +5883,7 @@ def set_index(
58825883 else :
58835884 arrays .append (self .index )
58845885
5885- to_remove : list [Hashable ] = []
5886+ to_remove : set [Hashable ] = set ()
58865887 for col in keys :
58875888 if isinstance (col , MultiIndex ):
58885889 arrays .extend (col ._get_level_values (n ) for n in range (col .nlevels ))
@@ -5909,7 +5910,7 @@ def set_index(
59095910 arrays .append (frame [col ])
59105911 names .append (col )
59115912 if drop :
5912- to_remove .append (col )
5913+ to_remove .add (col )
59135914
59145915 if len (arrays [- 1 ]) != len (self ):
59155916 # check newest element against length of calling frame, since
@@ -5926,7 +5927,7 @@ def set_index(
59265927 raise ValueError (f"Index has duplicate keys: { duplicates } " )
59275928
59285929 # use set to handle duplicate column names gracefully in case of drop
5929- for c in set ( to_remove ) :
5930+ for c in to_remove :
59305931 del frame [c ]
59315932
59325933 # clear up memory usage
0 commit comments