@@ -2645,29 +2645,29 @@ def _translate_axis_key_chunk(self, axis_key):
26452645
26462646 Returns
26472647 -------
2648- IGroup
2648+ (axis, indices)
26492649 Indices group with a valid axis (from self)
26502650 """
26512651 axis_key = remove_nested_groups (axis_key )
26522652
2653- if isinstance (axis_key , IGroup ) and axis_key .axis is not None :
2653+ if isinstance (axis_key , IGroup ):
2654+ if axis_key .axis is None :
2655+ raise ValueError ("positional groups without axis are not supported" )
2656+
26542657 # retarget to real axis, if needed
26552658 # only retarget IGroup and not LGroup to give the opportunity for axis.translate to try the "ticks"
26562659 # version of the group ONLY if key.axis is not real_axis (for performance reasons)
26572660 if axis_key .axis in self :
26582661 axis_key = axis_key .retarget_to (self [axis_key .axis ])
2662+ # already positional
2663+ if isinstance (axis_key , IGroup ):
2664+ return axis_key .axis , axis_key .key
26592665 else :
26602666 # axis associated with axis_key may not belong to self.
26612667 # In that case, we translate IGroup to labels and search for a compatible axis
26622668 # (see end of this method)
26632669 axis_key = axis_key .to_label ()
26642670
2665- # already positional
2666- if isinstance (axis_key , IGroup ):
2667- if axis_key .axis is None :
2668- raise ValueError ("positional groups without axis are not supported" )
2669- return axis_key
2670-
26712671 # labels but known axis
26722672 if isinstance (axis_key , LGroup ) and axis_key .axis is not None :
26732673 try :
@@ -2676,7 +2676,7 @@ def _translate_axis_key_chunk(self, axis_key):
26762676 axis_pos_key = real_axis .index (axis_key )
26772677 except KeyError :
26782678 raise ValueError (f"{ axis_key !r} is not a valid label for any axis" )
2679- return real_axis . i [ axis_pos_key ]
2679+ return real_axis , axis_pos_key
26802680 except KeyError :
26812681 # axis associated with axis_key may not belong to self.
26822682 # In that case, we translate LGroup to labels and search for a compatible axis
@@ -2702,7 +2702,8 @@ def _translate_axis_key_chunk(self, axis_key):
27022702 valid_axes = ', ' .join (a .name if a .name is not None else f'{{{ self .index (a )} }}'
27032703 for a in valid_axes )
27042704 raise ValueError (f'{ axis_key } is ambiguous (valid in { valid_axes } )' )
2705- return valid_axes [0 ].i [axis_pos_key ]
2705+ real_axis = valid_axes [0 ]
2706+ return real_axis , axis_pos_key
27062707
27072708 def _translate_axis_key (self , axis_key ):
27082709 """
@@ -2715,7 +2716,7 @@ def _translate_axis_key(self, axis_key):
27152716
27162717 Returns
27172718 -------
2718- IGroup
2719+ (axis, indices)
27192720 Indices group with a valid axis (from self)
27202721 """
27212722 # called from _key_to_igroups
@@ -2751,8 +2752,9 @@ def _translate_axis_key(self, axis_key):
27512752 # TODO: do not recheck already checked elements
27522753 key_chunk = axis_key .i [:size ] if isinstance (axis_key , Array ) else axis_key [:size ]
27532754 try :
2754- tkey = self ._translate_axis_key_chunk (key_chunk )
2755- axis = tkey .axis
2755+ axis , ikey = self ._translate_axis_key_chunk (key_chunk )
2756+ # if key is unambiguous (did not raise an exception), we know the axis
2757+ # TODO: if len(axis_key) < size, we can return axis, ikey directly
27562758 break
27572759 # TODO: we should only continue when ValueError is caused by an ambiguous key, otherwise we only delay
27582760 # an inevitable failure
@@ -2763,17 +2765,17 @@ def _translate_axis_key(self, axis_key):
27632765 # make sure we have an Axis object
27642766 # TODO: we should make sure the tkey returned from _translate_axis_key_chunk always contains a
27652767 # real Axis (and thus kill this line)
2766- axis = self [axis ]
2768+ # axis = self[axis]
27672769 # wrap key in LGroup
27682770 axis_key = axis [axis_key ]
27692771 # XXX: reuse tkey chunks and only translate the rest?
27702772 return self ._translate_axis_key_chunk (axis_key )
27712773 else :
27722774 return self ._translate_axis_key_chunk (axis_key )
27732775
2774- def _key_to_igroups (self , key ):
2776+ def _key_to_axis_and_indices (self , key ):
27752777 """
2776- Translates any key to an IGroups tuple.
2778+ Translates any key to a tuple of (axis, indices) tuples .
27772779
27782780 Parameters
27792781 ----------
@@ -2783,8 +2785,8 @@ def _key_to_igroups(self, key):
27832785 Returns
27842786 -------
27852787 tuple
2786- tuple of IGroup, each IGroup having a real axis from this array.
2787- The order of the IGroups is *not* guaranteed to be the same as the order of axes.
2788+ tuple of (axis, indices) pairs, with axis from this array.
2789+ The order of the pairs is *not* guaranteed to be the same as the order of axes.
27882790
27892791 See Also
27902792 --------
@@ -2827,7 +2829,7 @@ def _key_to_igroups(self, key):
28272829 key = [axis_key for axis_key in key
28282830 if not _isnoneslice (axis_key ) and axis_key is not Ellipsis ]
28292831
2830- # translate all keys to IGroup
2832+ # translate all keys to (axis, indices) pairs
28312833 return tuple (self ._translate_axis_key (axis_key ) for axis_key in key )
28322834
28332835 def _key_to_raw_and_axes (self , key , collapse_slices = False , translate_key = True , points = False , wildcard = False ):
@@ -2853,21 +2855,18 @@ def _key_to_raw_and_axes(self, key, collapse_slices=False, translate_key=True, p
28532855 # the key we need to know which axis each key belongs to and to do that, we need to
28542856 # translate the key to indices)
28552857
2856- # any key -> (IGroup, IGroup, ...)
2857- igroup_key = self ._key_to_igroups (key )
2858-
2859- # extract axis from Group keys
2860- key_items = [(k1 .axis , k1 ) for k1 in igroup_key ]
2858+ # any key -> ((axis, indices), (axis, indices), ...)
2859+ key_items = self ._key_to_axis_and_indices (key )
28612860
28622861 # even keys given as dict can contain duplicates (if the same axis was
28632862 # given under different forms, e.g. name and AxisReference).
2864- dupe_axes = list (duplicates (axis1 for axis1 , key1 in key_items ))
2863+ dupe_axes = list (duplicates (axis for axis , key in key_items ))
28652864 if dupe_axes :
2866- dupe_axes = ', ' .join (str (axis1 ) for axis1 in dupe_axes )
2865+ dupe_axes = ', ' .join (str (axis ) for axis in dupe_axes )
28672866 raise ValueError (f"key has several values for axis: { dupe_axes } \n { key_items } " )
28682867
2869- # IGroup -> raw positional
2870- dict_key = { axis1 : axis1 . index ( key1 ) for axis1 , key1 in key_items }
2868+ # ((axis, indices), (axis, indices), ...) -> dict
2869+ dict_key = dict ( key_items )
28712870
28722871 # dict -> tuple (complete and order key)
28732872 assert all (isinstance (k1 , Axis ) for k1 in dict_key )
0 commit comments