@@ -452,6 +452,8 @@ def _check_changed_attr_names(self, attr: str, columns: bool = False):
452452 attr_names_changed , attr_columns_changed = False , False
453453 if not hasattr (self , attrhash ):
454454 attr_names_changed , attr_columns_changed = True , True
455+ elif len (self .mod ) < len (getattr (self , attrhash )):
456+ attr_names_changed , attr_columns_changed = True , None
455457 else :
456458 for m , mod in self .mod .items ():
457459 if m in getattr (self , attrhash ):
@@ -607,7 +609,13 @@ def _update_attr(
607609 _attrhash = f"_{ attr } hash"
608610 attr_changed = self ._check_changed_attr_names (attr )
609611
610- attr_duplicated = self ._check_duplicated_attr_names (attr )
612+ if not any (attr_changed ):
613+ # Nothing to update
614+ return
615+
616+ data_global = getattr (self , attr )
617+
618+ attr_duplicated = not data_global .index .is_unique or self ._check_duplicated_attr_names (attr )
611619 attr_intersecting = self ._check_intersecting_attr_names (attr )
612620
613621 if attr_duplicated :
@@ -619,12 +627,6 @@ def _update_attr(
619627 f"Behaviour is not defined with axis=-1, { attr } _names need to be made unique first."
620628 )
621629
622- if not any (attr_changed ):
623- # Nothing to update
624- return
625-
626- data_global = getattr (self , attr )
627-
628630 # Generate unique colnames
629631 (rowcol ,) = self ._find_unique_colnames (attr , 1 )
630632
@@ -646,7 +648,6 @@ def _update_attr(
646648 # Join modality .obs/.var tables
647649 #
648650 # Main case: no duplicates and no intersection if the axis is not shared
649- #
650651 if not attr_duplicated :
651652 # Shared axis
652653 if axis == (1 - self ._axis ) or self ._axis == - 1 :
@@ -665,7 +666,7 @@ def _update_attr(
665666 data_mod = _make_index_unique (data_mod , force = attr_intersecting )
666667 data_global = _make_index_unique (data_global , force = attr_intersecting )
667668 if data_global .shape [1 ] > 0 :
668- data_mod = data_global .join (data_mod , how = "left" , sort = False )
669+ data_mod = data_mod .join (data_global , how = "left" , sort = False )
669670
670671 if data_global .shape [0 ] > 0 :
671672 # reorder new index to conform to the old index as much as possible
@@ -728,8 +729,11 @@ def _update_attr(
728729 )
729730 # after inserting a new modality with duplicates, but no duplicates before:
730731 # data_mod.index is not unique
731- data_global = _make_index_unique (data_global , force = not data_mod .index .is_unique )
732- data_mod = _make_index_unique (data_mod )
732+ # after deleting a modality with duplicates: data_global.index is not unique, but
733+ # data_mod.index is unique
734+ need_unique = data_mod .index .is_unique | data_global .index .is_unique
735+ data_global = _make_index_unique (data_global , force = need_unique )
736+ data_mod = _make_index_unique (data_mod , force = need_unique )
733737 data_mod = data_mod .join (data_global , how = "left" , sort = False )
734738
735739 # reorder new index to conform to the old index as much as possible
@@ -750,6 +754,10 @@ def _update_attr(
750754 > data_global .shape [0 ] # new modality added and concacenated
751755 )
752756
757+ if need_unique :
758+ data_mod = _restore_index (data_mod )
759+ data_global = _restore_index (data_global )
760+
753761 data_mod .reset_index (level = list (range (1 , data_mod .index .nlevels )), inplace = True )
754762 data_global .reset_index (level = list (range (1 , data_global .index .nlevels )), inplace = True )
755763 data_mod .index .set_names (None , inplace = True )
0 commit comments