@@ -137,7 +137,6 @@ def __init__(
137137 # Bug Fix GH 61221
138138 # The -1 in the unsorted unique codes causes for errors
139139 # saving the NA location to be used in the repeater
140- self .na = np .where (unique_codes == - 1 )[0 ][0 ] if - 1 in unique_codes else None
141140 unique_codes = unique_codes [unique_codes != - 1 ]
142141 self .removed_level = self .removed_level .take (unique_codes )
143142 self .removed_level_full = self .removed_level_full .take (unique_codes )
@@ -398,22 +397,22 @@ def _repeater(self) -> np.ndarray:
398397 # In this case, we remap the new codes to the original level:
399398 repeater = self .removed_level_full .get_indexer (self .removed_level )
400399 if self .lift :
401- if not self .sort and self .na :
402- repeater = np .insert (repeater , self .na , - 1 )
403- else :
404- repeater = np .insert (repeater , 0 , - 1 )
400+ na_index = (self .index .codes [self .level ] == - 1 ).nonzero ()[0 ][0 ]
401+ repeater = np .insert (repeater , na_index , - 1 )
402+
405403 else :
406404 # Otherwise, we just use each level item exactly once:
407405 stride = len (self .removed_level ) + self .lift
408- if self .sort or not self .na :
406+ if self .sort or not self .lift :
409407 repeater = np .arange (stride ) - self .lift
410408 else :
411- # move the -1 to the position at self.na
409+ # move the -1 to the position at na_index
410+ na_index = (self .index .codes [self .level ] == - 1 ).nonzero ()[0 ][0 ]
412411 repeater = np .arange (stride )
413- if self . na :
414- repeater [self . na ] = - 1
415- if (self . na + 1 ) < len (repeater ):
416- repeater [self . na + 1 :] -= 1
412+ if na_index :
413+ repeater [na_index ] = - 1
414+ if (na_index + 1 ) < len (repeater ):
415+ repeater [na_index + 1 :] -= 1
417416
418417 return repeater
419418
0 commit comments