@@ -628,8 +628,10 @@ def _prepare_blocks(self):
628
628
629
629
for unit in self .units :
630
630
join_blocks = unit .get_upcasted_blocks ()
631
- type_map = dict ((type (blk ), blk ) for blk in join_blocks )
632
- blockmaps .append (type_map )
631
+ type_map = {}
632
+ for blk in join_blocks :
633
+ type_map .setdefault (type (blk ), []).append (blk )
634
+ blockmaps .append ((unit , type_map ))
633
635
634
636
return blockmaps
635
637
@@ -640,26 +642,22 @@ def get_result(self):
640
642
merged : BlockManager
641
643
"""
642
644
blockmaps = self ._prepare_blocks ()
643
- kinds = _get_all_block_kinds (blockmaps )
645
+ kinds = _get_merge_block_kinds (blockmaps )
644
646
645
647
result_blocks = []
646
648
647
649
# maybe want to enable flexible copying <-- what did I mean?
648
650
for klass in kinds :
649
- klass_blocks = [mapping .get (klass ) for mapping in blockmaps ]
651
+ klass_blocks = []
652
+ for unit , mapping in blockmaps :
653
+ if klass in mapping :
654
+ klass_blocks .extend ((unit , b ) for b in mapping [klass ])
650
655
res_blk = self ._get_merged_block (klass_blocks )
651
656
result_blocks .append (res_blk )
652
657
653
658
return BlockManager (result_blocks , self .result_axes )
654
659
655
- def _get_merged_block (self , blocks ):
656
-
657
- to_merge = []
658
-
659
- for unit , block in zip (self .units , blocks ):
660
- if block is not None :
661
- to_merge .append ((unit , block ))
662
-
660
+ def _get_merged_block (self , to_merge ):
663
661
if len (to_merge ) > 1 :
664
662
return self ._merge_blocks (to_merge )
665
663
else :
@@ -682,7 +680,8 @@ def _merge_blocks(self, merge_chunks):
682
680
out_shape [self .axis ] = n
683
681
684
682
# Should use Fortran order??
685
- out = np .empty (out_shape , dtype = fblock .values .dtype )
683
+ block_dtype = _get_block_dtype ([x [1 ] for x in merge_chunks ])
684
+ out = np .empty (out_shape , dtype = block_dtype )
686
685
687
686
sofar = 0
688
687
for unit , blk in merge_chunks :
@@ -787,6 +786,25 @@ def _get_all_block_kinds(blockmaps):
787
786
kinds |= set (mapping )
788
787
return kinds
789
788
789
+ def _get_merge_block_kinds (blockmaps ):
790
+ kinds = set ()
791
+ for _ , mapping in blockmaps :
792
+ kinds |= set (mapping )
793
+ return kinds
794
+
795
+ def _get_block_dtype (blocks ):
796
+ if len (blocks ) == 0 :
797
+ return object
798
+ blk1 = blocks [0 ]
799
+ dtype = blk1 .dtype
800
+
801
+ if issubclass (dtype .type , np .floating ):
802
+ for blk in blocks :
803
+ if blk .dtype .type == np .float64 :
804
+ return blk .dtype
805
+
806
+ return dtype
807
+
790
808
#----------------------------------------------------------------------
791
809
# Concatenate DataFrame objects
792
810
@@ -928,16 +946,20 @@ def get_result(self):
928
946
def _get_fresh_axis (self ):
929
947
return Index (np .arange (len (self ._get_concat_axis ())))
930
948
949
+ def _prepare_blocks (self ):
950
+ reindexed_data = self ._get_reindexed_data ()
951
+
952
+ blockmaps = []
953
+ for data in reindexed_data :
954
+ data = data .consolidate ()
955
+ type_map = dict ((type (blk ), blk ) for blk in data .blocks )
956
+ blockmaps .append (type_map )
957
+ return blockmaps
958
+
931
959
def _get_concatenated_data (self ):
932
960
try :
933
961
# need to conform to same other (joined) axes for block join
934
- reindexed_data = self ._get_reindexed_data ()
935
-
936
- blockmaps = []
937
- for data in reindexed_data :
938
- data = data .consolidate ()
939
- type_map = dict ((type (blk ), blk ) for blk in data .blocks )
940
- blockmaps .append (type_map )
962
+ blockmaps = self ._prepare_blocks ()
941
963
kinds = _get_all_block_kinds (blockmaps )
942
964
943
965
new_blocks = []
0 commit comments