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