@@ -2301,8 +2301,8 @@ def maybe_reorder(
2301
2301
exclude .update (index )
2302
2302
2303
2303
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 }
2306
2306
arrays = [v for i , v in enumerate (arrays ) if i not in to_remove ]
2307
2307
2308
2308
columns = columns .drop (exclude )
@@ -3705,7 +3705,7 @@ def transpose(
3705
3705
nv .validate_transpose (args , {})
3706
3706
# construct the args
3707
3707
3708
- dtypes = list ( self .dtypes )
3708
+ first_dtype = self .dtypes . iloc [ 0 ] if len ( self . columns ) else None
3709
3709
3710
3710
if self ._can_fast_transpose :
3711
3711
# Note: tests pass without this, but this improves perf quite a bit.
@@ -3723,11 +3723,11 @@ def transpose(
3723
3723
3724
3724
elif (
3725
3725
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 )
3728
3728
):
3729
3729
new_values : list
3730
- if isinstance (dtypes [ 0 ] , BaseMaskedDtype ):
3730
+ if isinstance (first_dtype , BaseMaskedDtype ):
3731
3731
# We have masked arrays with the same dtype. We can transpose faster.
3732
3732
from pandas .core .arrays .masked import (
3733
3733
transpose_homogeneous_masked_arrays ,
@@ -3736,7 +3736,7 @@ def transpose(
3736
3736
new_values = transpose_homogeneous_masked_arrays (
3737
3737
cast (Sequence [BaseMaskedArray ], self ._iter_column_arrays ())
3738
3738
)
3739
- elif isinstance (dtypes [ 0 ] , ArrowDtype ):
3739
+ elif isinstance (first_dtype , ArrowDtype ):
3740
3740
# We have arrow EAs with the same dtype. We can transpose faster.
3741
3741
from pandas .core .arrays .arrow .array import (
3742
3742
ArrowExtensionArray ,
@@ -3748,10 +3748,11 @@ def transpose(
3748
3748
)
3749
3749
else :
3750
3750
# 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 ()
3753
3752
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
+ ]
3755
3756
3756
3757
result = type (self )._from_arrays (
3757
3758
new_values ,
@@ -5882,7 +5883,7 @@ def set_index(
5882
5883
else :
5883
5884
arrays .append (self .index )
5884
5885
5885
- to_remove : list [Hashable ] = []
5886
+ to_remove : set [Hashable ] = set ()
5886
5887
for col in keys :
5887
5888
if isinstance (col , MultiIndex ):
5888
5889
arrays .extend (col ._get_level_values (n ) for n in range (col .nlevels ))
@@ -5909,7 +5910,7 @@ def set_index(
5909
5910
arrays .append (frame [col ])
5910
5911
names .append (col )
5911
5912
if drop :
5912
- to_remove .append (col )
5913
+ to_remove .add (col )
5913
5914
5914
5915
if len (arrays [- 1 ]) != len (self ):
5915
5916
# check newest element against length of calling frame, since
@@ -5926,7 +5927,7 @@ def set_index(
5926
5927
raise ValueError (f"Index has duplicate keys: { duplicates } " )
5927
5928
5928
5929
# 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 :
5930
5931
del frame [c ]
5931
5932
5932
5933
# clear up memory usage
0 commit comments