@@ -324,7 +324,7 @@ class ResolvedGrouper(Generic[T_DataWithCoords]):
324
324
codes : DataArray = field (init = False )
325
325
full_index : pd .Index = field (init = False )
326
326
group_indices : T_GroupIndices = field (init = False )
327
- unique_coord : IndexVariable | _DummyGroup = field (init = False )
327
+ unique_coord : Variable | _DummyGroup = field (init = False )
328
328
329
329
# _ensure_1d:
330
330
group1d : T_Group = field (init = False )
@@ -353,14 +353,18 @@ def __post_init__(self) -> None:
353
353
354
354
@property
355
355
def name (self ) -> Hashable :
356
+ """Name for the grouped coordinate after reduction."""
356
357
# the name has to come from unique_coord because we need `_bins` suffix for BinGrouper
357
- return self .unique_coord .name
358
+ (name ,) = self .unique_coord .dims
359
+ return name
358
360
359
361
@property
360
362
def size (self ) -> int :
363
+ """Number of groups."""
361
364
return len (self )
362
365
363
366
def __len__ (self ) -> int :
367
+ """Number of groups."""
364
368
return len (self .full_index )
365
369
366
370
@property
@@ -383,8 +387,8 @@ def factorize(self) -> None:
383
387
]
384
388
if encoded .unique_coord is None :
385
389
unique_values = self .full_index [np .unique (encoded .codes )]
386
- self .unique_coord = IndexVariable (
387
- self .codes .name , unique_values , attrs = self .group .attrs
390
+ self .unique_coord = Variable (
391
+ dims = self .codes .name , data = unique_values , attrs = self .group .attrs
388
392
)
389
393
else :
390
394
self .unique_coord = encoded .unique_coord
@@ -645,6 +649,8 @@ def _iter_grouped(self, warn_squeeze=True) -> Iterator[T_Xarray]:
645
649
yield self ._obj .isel ({self ._group_dim : indices })
646
650
647
651
def _infer_concat_args (self , applied_example ):
652
+ from xarray .core .groupers import BinGrouper
653
+
648
654
(grouper ,) = self .groupers
649
655
if self ._group_dim in applied_example .dims :
650
656
coord = grouper .group1d
@@ -653,7 +659,10 @@ def _infer_concat_args(self, applied_example):
653
659
coord = grouper .unique_coord
654
660
positions = None
655
661
(dim ,) = coord .dims
656
- if isinstance (coord , _DummyGroup ):
662
+ if isinstance (grouper .group , _DummyGroup ) and not isinstance (
663
+ grouper .grouper , BinGrouper
664
+ ):
665
+ # When binning we actually do set the index
657
666
coord = None
658
667
coord = getattr (coord , "variable" , coord )
659
668
return coord , dim , positions
@@ -670,12 +679,20 @@ def _binary_op(self, other, f, reflexive=False):
670
679
codes = self ._codes
671
680
dims = group .dims
672
681
673
- if isinstance (group , _DummyGroup ):
682
+ if isinstance (grouper . group , _DummyGroup ):
674
683
group = coord = group .to_dataarray ()
675
684
else :
676
685
coord = grouper .unique_coord
677
- if not isinstance (coord , DataArray ):
678
- coord = DataArray (grouper .unique_coord )
686
+ if isinstance (coord , Variable ):
687
+ assert coord .ndim == 1
688
+ (coord_dim ,) = coord .dims
689
+ # TODO: explicitly create Index here
690
+ coord = DataArray (
691
+ dims = coord_dim ,
692
+ data = coord .data ,
693
+ attrs = coord .attrs ,
694
+ coords = {coord_dim : coord .data },
695
+ )
679
696
name = grouper .name
680
697
681
698
if not isinstance (other , (Dataset , DataArray )):
@@ -873,7 +890,10 @@ def _flox_reduce(
873
890
# in the grouped variable
874
891
group_dims = grouper .group .dims
875
892
if set (group_dims ).issubset (set (parsed_dim )):
876
- result [grouper .name ] = output_index
893
+ new_coord = Variable (
894
+ dims = grouper .name , data = np .array (output_index ), attrs = self ._codes .attrs
895
+ )
896
+ result = result .assign_coords ({grouper .name : new_coord })
877
897
result = result .drop_vars (unindexed_dims )
878
898
879
899
# broadcast and restore non-numeric data variables (backcompat)
0 commit comments