22
22
from pandas ._typing import (
23
23
Axis ,
24
24
AxisInt ,
25
+ Dtype ,
25
26
HashableT ,
26
27
)
27
28
from pandas .util ._decorators import cache_readonly
28
29
30
+ from pandas .core .dtypes .common import is_float_dtype
29
31
from pandas .core .dtypes .concat import concat_compat
30
32
from pandas .core .dtypes .generic import (
31
33
ABCDataFrame ,
@@ -564,6 +566,18 @@ def __init__(
564
566
565
567
self .new_axes = self ._get_new_axes ()
566
568
569
+ def _maybe_float_dtype (self ) -> Dtype | None :
570
+ """If all columns in all objs are float only, we may be able to optimize."""
571
+ all_dtypes = [
572
+ blk .dtype
573
+ for df in self .objs
574
+ for blk in df ._mgr .blocks # type: ignore[union-attr]
575
+ ]
576
+ all_dtypes = [* dict .fromkeys (all_dtypes )]
577
+ if len (all_dtypes ) != 1 :
578
+ return None
579
+ return all_dtypes [0 ] if is_float_dtype (all_dtypes [0 ]) else None
580
+
567
581
def get_result (self ):
568
582
cons : Callable [..., DataFrame | Series ]
569
583
sample : DataFrame | Series
@@ -598,6 +612,7 @@ def get_result(self):
598
612
# combine block managers
599
613
else :
600
614
sample = cast ("DataFrame" , self .objs [0 ])
615
+ maybe_float = self ._maybe_float_dtype ()
601
616
602
617
mgrs_indexers = []
603
618
for obj in self .objs :
@@ -609,9 +624,17 @@ def get_result(self):
609
624
continue
610
625
611
626
# 1-ax to convert BlockManager axis to DataFrame axis
612
- obj_labels = obj .axes [1 - ax ]
613
- if not new_labels .equals (obj_labels ):
614
- indexers [ax ] = obj_labels .get_indexer (new_labels )
627
+ obj_labels = obj .axes [self .bm_axis ]
628
+ if new_labels .equals (obj_labels ):
629
+ continue
630
+ if maybe_float is not None and obj_labels .is_unique :
631
+ # by aligning dataframes to new_labels, we get a perf boost
632
+ # only done with frames with all floats ATM
633
+ obj = obj .reindex (new_labels , axis = self .bm_axis )
634
+ obj = obj .astype (maybe_float )
635
+ continue
636
+
637
+ indexers [ax ] = obj_labels .get_indexer (new_labels )
615
638
616
639
mgrs_indexers .append ((obj ._mgr , indexers ))
617
640
0 commit comments