xarray icon indicating copy to clipboard operation
xarray copied to clipboard

Add parse_dims func

Open headtr1ck opened this issue 3 years ago • 6 comments
trafficstars

This PR adds a utils.parse_dims function for parsing one or more dimensions. Currently every function that accepts multiple dimensions does this by itself.

I decided to first see if it would be useful to centralize the dimension parsing and collect inputs before adding it to other functions.

headtr1ck avatar Sep 18 '22 15:09 headtr1ck

I might have thought both of them could be encapsulated in a single func... Maybe that's infix_dims, or fine to rename!

I always thought that these two methods are incompatible. But I guess ... Is synonym for None (= all dims) and if ... Appears in an iterable, it just means replace it with all leftover dims.

headtr1ck avatar Sep 20 '22 16:09 headtr1ck

I always thought that these two methods are incompatible. But I guess ... Is synonym for None (= all dims) and if ... Appears in an iterable, it just means replace it with all leftover dims.

Yes! ... is the better synonym — None is somewhat an artifact of history. So +1 for replace_none, which maybe we can gradually turn to False by default over time.

max-sixty avatar Sep 20 '22 19:09 max-sixty

Yes! ... is the better synonym — None is somewhat an artifact of history. So +1 for replace_none, which maybe we can gradually turn to False by default over time.

Nothing wrong with None, it is just pythons default.

The intention of replace_none=False was to leave None as None, which is important for some low level functions as they might be optimized (like numpy sum, which sums over all axes for None).

headtr1ck avatar Sep 20 '22 19:09 headtr1ck

Nothing wrong with None, it is just pythons default.

The intention of replace_none=False was to leave None as None, which is important for some low level functions as they might be optimized (like numpy sum, which sums over all axes for None).

Not relevant to this PR but for background — it used to be that da.groupby() defaulted to None, and reduced all dimensions — now for a couple of years we require ....

But still da.sum() is equivalent to da.sum(...), which is arguably a bit incongruent with da.sum('a') reducing fewer dimensions than da.sum(['a','b']). But would be quite hard to change now.

max-sixty avatar Sep 20 '22 22:09 max-sixty

For xr.dot it's also different:

xr.dot(a, b, dims=None) # reduce over common dimensions
xr.dot(a, b, dims=...) # reduce over all dimensions

mathause avatar Sep 22 '22 20:09 mathause

I think it is best to implement two functions, one for sets of dims and one for sequences of dims. This will be easier to type/read/use than trying to put both kinds into one function.

See also #7094

Maybe parse_dims and parse_ordered_dims?

headtr1ck avatar Oct 03 '22 09:10 headtr1ck