@@ -299,7 +299,7 @@ class ResolvedGrouper(Generic[T_DataWithCoords]):
299
299
codes : DataArray = field (init = False )
300
300
full_index : pd .Index = field (init = False )
301
301
group_indices : T_GroupIndices = field (init = False )
302
- unique_coord : IndexVariable | _DummyGroup = field (init = False )
302
+ unique_coord : Variable | _DummyGroup = field (init = False )
303
303
304
304
# _ensure_1d:
305
305
group1d : T_Group = field (init = False )
@@ -328,14 +328,18 @@ def __post_init__(self) -> None:
328
328
329
329
@property
330
330
def name (self ) -> Hashable :
331
+ """Name for the grouped coordinate after reduction."""
331
332
# the name has to come from unique_coord because we need `_bins` suffix for BinGrouper
332
- return self .unique_coord .name
333
+ (name ,) = self .unique_coord .dims
334
+ return name
333
335
334
336
@property
335
337
def size (self ) -> int :
338
+ """Number of groups."""
336
339
return len (self )
337
340
338
341
def __len__ (self ) -> int :
342
+ """Number of groups."""
339
343
return len (self .full_index )
340
344
341
345
@property
@@ -358,8 +362,8 @@ def factorize(self) -> None:
358
362
]
359
363
if encoded .unique_coord is None :
360
364
unique_values = self .full_index [np .unique (encoded .codes )]
361
- self .unique_coord = IndexVariable (
362
- self .codes .name , unique_values , attrs = self .group .attrs
365
+ self .unique_coord = Variable (
366
+ dims = self .codes .name , data = unique_values , attrs = self .group .attrs
363
367
)
364
368
else :
365
369
self .unique_coord = encoded .unique_coord
@@ -620,6 +624,8 @@ def _iter_grouped(self, warn_squeeze=True) -> Iterator[T_Xarray]:
620
624
yield self ._obj .isel ({self ._group_dim : indices })
621
625
622
626
def _infer_concat_args (self , applied_example ):
627
+ from xarray .core .groupers import BinGrouper
628
+
623
629
(grouper ,) = self .groupers
624
630
if self ._group_dim in applied_example .dims :
625
631
coord = grouper .group1d
@@ -628,7 +634,10 @@ def _infer_concat_args(self, applied_example):
628
634
coord = grouper .unique_coord
629
635
positions = None
630
636
(dim ,) = coord .dims
631
- if isinstance (coord , _DummyGroup ):
637
+ if isinstance (grouper .group , _DummyGroup ) and not isinstance (
638
+ grouper .grouper , BinGrouper
639
+ ):
640
+ # When binning we actually do set the index
632
641
coord = None
633
642
coord = getattr (coord , "variable" , coord )
634
643
return coord , dim , positions
@@ -645,12 +654,20 @@ def _binary_op(self, other, f, reflexive=False):
645
654
codes = self ._codes
646
655
dims = group .dims
647
656
648
- if isinstance (group , _DummyGroup ):
657
+ if isinstance (grouper . group , _DummyGroup ):
649
658
group = coord = group .to_dataarray ()
650
659
else :
651
660
coord = grouper .unique_coord
652
- if not isinstance (coord , DataArray ):
653
- coord = DataArray (grouper .unique_coord )
661
+ if isinstance (coord , Variable ):
662
+ assert coord .ndim == 1
663
+ (coord_dim ,) = coord .dims
664
+ # TODO: explicitly create Index here
665
+ coord = DataArray (
666
+ dims = coord_dim ,
667
+ data = coord .data ,
668
+ attrs = coord .attrs ,
669
+ coords = {coord_dim : coord .data },
670
+ )
654
671
name = grouper .name
655
672
656
673
if not isinstance (other , (Dataset , DataArray )):
@@ -848,7 +865,10 @@ def _flox_reduce(
848
865
# in the grouped variable
849
866
group_dims = grouper .group .dims
850
867
if set (group_dims ).issubset (set (parsed_dim )):
851
- result [grouper .name ] = output_index
868
+ new_coord = Variable (
869
+ dims = grouper .name , data = np .array (output_index ), attrs = self ._codes .attrs
870
+ )
871
+ result = result .assign_coords ({grouper .name : new_coord })
852
872
result = result .drop_vars (unindexed_dims )
853
873
854
874
# broadcast and restore non-numeric data variables (backcompat)
0 commit comments