Skip to content

Commit 050ea70

Browse files
committed
working!
1 parent 24980f2 commit 050ea70

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

xarray/core/groupby.py

+24-15
Original file line numberDiff line numberDiff line change
@@ -489,25 +489,28 @@ def _binary_op(self, other, f, reflexive=False):
489489
group = self._original_group
490490
name = group.name
491491
dim = self._group_dim
492-
# import IPython; IPython.core.debugger.set_trace()
492+
493493
try:
494494
if self._bins is not None:
495-
if self._stacked_dim is not None:
496-
group = self._group.unstack()
497-
# idx = pd.factorize(group.data.ravel())[0]
498-
# group_idx = group.copy(data=idx.reshape(group.data.shape))
499-
other = other.sel({f"{name}_bins": group})
500-
if isinstance(group, _DummyGroup):
495+
group = self._maybe_unstack(self._group)
496+
other = other.sel({f"{name}_bins": self._group})
497+
# TODO: vectorized indexing bug in .sel; name_bins is still an IndexVariable!
498+
other[f"{name}_bins"] = other[f"{name}_bins"].variable.to_variable()
499+
if name == dim and dim not in obj.xindexes:
501500
# When binning by unindexed coordinate we need to reindex obj.
502501
# _full_index is IntervalIndex, so idx will be -1 where
503502
# a value does not belong to any bin. Using IntervalIndex
504503
# accounts for any non-default cut_kwargs passed to the constructor
505504
idx = pd.cut(obj[dim], bins=self._full_index).codes
506-
obj = obj.isel({dim: np.arange(group.size)[idx != -1]})
505+
obj = obj.isel({dim: np.arange(obj[dim].size)[idx != -1]})
506+
else:
507+
obj = self._obj
508+
507509
else:
508510
if isinstance(group, _DummyGroup):
509511
group = obj[dim]
510512
other = other.sel({name: group})
513+
511514
except AttributeError:
512515
raise TypeError(
513516
"GroupBy objects only support binary ops "
@@ -523,7 +526,12 @@ def _binary_op(self, other, f, reflexive=False):
523526
)
524527
# some labels are absent i.e. other is not aligned
525528
# so we align by reindexing and then rename dimensions.
526-
# TODO: probably need to copy some coordinates over
529+
# Broadcast out scalars.
530+
for var in other.coords:
531+
if other[var].ndim == 0:
532+
other[var] = (
533+
other[var].drop(var).expand_dims({name: other.sizes[name]})
534+
)
527535
other = (
528536
other.reindex({name: group.data})
529537
.rename({name: dim})
@@ -532,12 +540,13 @@ def _binary_op(self, other, f, reflexive=False):
532540

533541
result = g(obj, other)
534542

535-
# backcompat:
536-
for var in set(obj.coords) - set(obj.xindexes):
537-
print(var)
538-
if dim not in obj[var]:
539-
print(f"excluding {dim}, broadcasting {var}")
540-
result[var] = obj[var].reset_coords(drop=True).broadcast_like(result)
543+
if group.ndim > 1:
544+
# backcompat:
545+
for var in set(obj.coords) - set(obj.xindexes):
546+
if set(obj[var].dims) < set(group.dims):
547+
result[var] = obj[var].reset_coords(drop=True).broadcast_like(group)
548+
549+
result = self._maybe_unstack(result).transpose(*group.dims, ...)
541550
return result
542551

543552
def _maybe_restore_empty_groups(self, combined):

0 commit comments

Comments
 (0)