@@ -3119,6 +3119,9 @@ def reindex_indexer(self, new_axis, indexer, axis=1, fill_value=None,
3119
3119
if not allow_dups and not self .axes [axis ].is_unique :
3120
3120
raise ValueError ("cannot reindex from a duplicate axis" )
3121
3121
3122
+ if not self .is_consolidated ():
3123
+ self = self .consolidate ()
3124
+
3122
3125
if axis == 0 :
3123
3126
return self ._reindex_indexer_items (new_axis , indexer , fill_value )
3124
3127
@@ -3140,38 +3143,62 @@ def _reindex_indexer_items(self, new_items, indexer, fill_value):
3140
3143
new_blocks = []
3141
3144
is_unique = new_items .is_unique
3142
3145
3146
+ # we have duplicates in the items and what we are reindexing
3147
+ if not is_unique and not self .items .is_unique :
3148
+
3149
+ rl = self ._set_ref_locs (do_refs = 'force' )
3150
+ for i , idx in enumerate (indexer ):
3151
+ item = new_items .take ([i ])
3152
+ if idx >= 0 :
3153
+ blk , lidx = rl [idx ]
3154
+ blk = make_block (_block_shape (blk .iget (lidx )), item ,
3155
+ new_items , ndim = self .ndim , fastpath = True ,
3156
+ placement = [i ])
3157
+
3158
+ # a missing value
3159
+ else :
3160
+ blk = self ._make_na_block (item ,
3161
+ new_items ,
3162
+ placement = [i ],
3163
+ fill_value = fill_value )
3164
+ new_blocks .append (blk )
3165
+ new_blocks = _consolidate (new_blocks , new_items )
3166
+
3167
+
3143
3168
# keep track of what items aren't found anywhere
3144
- l = np .arange (len (item_order ))
3145
- mask = np .zeros (len (item_order ), dtype = bool )
3146
- for blk in self .blocks :
3147
- blk_indexer = blk .items .get_indexer (item_order )
3148
- selector = blk_indexer != - 1
3169
+ else :
3170
+ l = np .arange (len (item_order ))
3171
+ mask = np .zeros (len (item_order ), dtype = bool )
3149
3172
3150
- # update with observed items
3151
- mask |= selector
3173
+ for blk in self .blocks :
3174
+ blk_indexer = blk .items .get_indexer (item_order )
3175
+ selector = blk_indexer != - 1
3176
+
3177
+ # update with observed items
3178
+ mask |= selector
3152
3179
3153
- if not selector .any ():
3154
- continue
3180
+ if not selector .any ():
3181
+ continue
3155
3182
3156
- new_block_items = new_items .take (selector .nonzero ()[0 ])
3157
- new_values = com .take_nd (blk .values , blk_indexer [selector ], axis = 0 ,
3158
- allow_fill = False )
3159
- placement = l [selector ] if not is_unique else None
3160
- new_blocks .append (make_block (new_values ,
3161
- new_block_items ,
3183
+ new_block_items = new_items .take (selector .nonzero ()[0 ])
3184
+ new_values = com .take_nd (blk .values , blk_indexer [selector ], axis = 0 ,
3185
+ allow_fill = False )
3186
+ placement = l [selector ] if not is_unique else None
3187
+ new_blocks .append (make_block (new_values ,
3188
+ new_block_items ,
3162
3189
new_items ,
3163
- placement = placement ,
3164
- fastpath = True ))
3165
-
3166
- if not mask .all ():
3167
- na_items = new_items [- mask ]
3168
- placement = l [- mask ] if not is_unique else None
3169
- na_block = self ._make_na_block (na_items ,
3170
- new_items ,
3171
- placement = placement ,
3172
- fill_value = fill_value )
3173
- new_blocks .append (na_block )
3174
- new_blocks = _consolidate (new_blocks , new_items )
3190
+ placement = placement ,
3191
+ fastpath = True ))
3192
+
3193
+ if not mask .all ():
3194
+ na_items = new_items [- mask ]
3195
+ placement = l [- mask ] if not is_unique else None
3196
+ na_block = self ._make_na_block (na_items ,
3197
+ new_items ,
3198
+ placement = placement ,
3199
+ fill_value = fill_value )
3200
+ new_blocks .append (na_block )
3201
+ new_blocks = _consolidate (new_blocks , new_items )
3175
3202
3176
3203
return self .__class__ (new_blocks , new_axes )
3177
3204
0 commit comments