@@ -726,18 +726,22 @@ def __getitem__( # noqa: C901
726726 except RuntimeError :
727727 pass
728728
729- integrations = set ()
730- slices = []
729+ integrations : Set [int ] = set ()
730+ slices : List [_core .algorithm .reduce_command ] = []
731+ pick_each : Dict [int , int ] = dict ()
731732
732733 # Compute needed slices and projections
733734 for i , ind in enumerate (indexes ):
734735 if hasattr (ind , "__index__" ):
735- ind = slice (ind .__index__ (), ind .__index__ () + 1 , sum ) # type: ignore
736-
736+ pick_each [i ] = ind .__index__ () + ( # type: ignore
737+ 1 if self .axes [i ].traits .underflow else 0
738+ )
739+ continue
737740 elif not isinstance (ind , slice ):
738741 raise IndexError (
739742 "Must be a slice, an integer, or follow the locator protocol."
740743 )
744+
741745 # If the dictionary brackets are forgotten, it's easy to put a slice
742746 # into a slice - adding a nicer error message in that case
743747 if any (isinstance (v , slice ) for v in (ind .start , ind .stop , ind .step )):
@@ -778,15 +782,25 @@ def __getitem__( # noqa: C901
778782 logger .debug ("Reduce with %s" , slices )
779783 reduced = self ._hist .reduce (* slices )
780784
781- if not integrations :
782- return self ._new_hist (reduced )
783- projections = [i for i in range (self .ndim ) if i not in integrations ]
784-
785- return (
786- self ._new_hist (reduced .project (* projections ))
787- if projections
788- else reduced .sum (flow = True )
789- )
785+ if pick_each :
786+ my_slice = tuple (
787+ pick_each .get (i , slice (None )) for i in range (reduced .rank ())
788+ )
789+ logger .debug ("Slices: %s" , my_slice )
790+ axes = [
791+ reduced .axis (i ) for i in range (reduced .rank ()) if i not in pick_each
792+ ]
793+ logger .debug ("Axes: %s" , axes )
794+ new_reduced = reduced .__class__ (axes )
795+ new_reduced .view (flow = True )[...] = reduced .view (flow = True )[my_slice ]
796+ reduced = new_reduced
797+ integrations = {i - sum (j <= i for j in pick_each ) for i in integrations }
798+
799+ if integrations :
800+ projections = [i for i in range (reduced .rank ()) if i not in integrations ]
801+ reduced = reduced .project (* projections )
802+
803+ return self ._new_hist (reduced ) if reduced .rank () > 0 else reduced .sum (flow = True )
790804
791805 def __setitem__ (
792806 self , index : IndexingExpr , value : Union [ArrayLike , Accumulator ]
0 commit comments