-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[namedarray] split .set_dims()
into .expand_dims()
and broadcast_to()
#8380
Conversation
.set_dims()
, .transpose()
and .T
to namedarray.set_dims()
into .expand_dims()
and broadcast_to()
Co-authored-by: Illviljan <14371165+Illviljan@users.noreply.github.com>
I have frequently unwrapped to access and compute on the underlying data container, which does indeed care about axis order. |
Designing an API around not using the class seems odd to me. I agree that once you have retrieved the underlying data you will indeed have to deal with the axis order. But didn't you feel like you were hacking around a problem with xarray? That's what it sounds like to me. This pattern is not realistic since we support any from numpy import array_api as nxp
import xarray as xr
from xarray.namedarray import _array_api as xrp
na = xr.namedarray.core.NamedArray(
dims=("a", None, ("c",)), data=nxp.reshape(nxp.arange(4 * 4 * 4), (4, 4, 4))
)
na.expand_dims(x=-2)
na.expand_dims(100=-2) # Error, not a valid identifier, str(100).isidentifier() == False Is this what you want then? from numpy import array_api as nxp
import xarray as xr
from xarray.namedarray import _array_api as xrp
na = xr.namedarray.core.NamedArray(
dims=("a", None, ("c",)), data=nxp.reshape(nxp.arange(4 * 4 * 4), (4, 4, 4))
)
na.expand_dims(dim=100, axis=-2) |
i'm revisiting this PR, and i've noticed that there is no clear agreement on the desired API spec for i understand that na.expand_dims(100=-2)
# or
na.expand_dims(None=0) won;t work but the following works na.expand_dims(dim={100:-2})
# or
na.expand_dims(dim={None=0}) should we then remove support for am i missing something? |
thinking more about this, we don't seem to restrict user from assigning hashables like ( In [33]: v = xr.Variable(data=np.zeros(6).reshape(2, 3), dims=[100, None])
In [34]: v
Out[34]:
<xarray.Variable (100: 2, None: 3)>
array([[0., 0., 0.],
[0., 0., 0.]])
In [35]: v.sel(100=1)
Cell In[35], line 1
v.sel(100=1)
^
SyntaxError: expression cannot contain assignment, perhaps you meant "=="? so, i'm not sure we should drop na.expand_dims(b=2) |
THis is a fairly minor point so lets just go with |
Yeah, I'm thinking |
for more information, see https://pre-commit.ci
simplified the `expand_dims` method of the NamedArray class by removing support for multiple and keyword arguments for new dimensions. the change updates the method signature to only accept a single dimension name, enhancing clarity and maintainability. this shift focuses on the common use case of adding a single dimension and moves extended functionality to a separate API.
…llowing only the broadcasting of existing ones
@dcherian, when you get a chance, this is ready for another round of review |
* main: (153 commits) Add overloads to get_axis_num (pydata#8547) Fix CI: temporary pin pytest version to 7.4.* (pydata#8682) Bump the actions group with 1 update (pydata#8678) [namedarray] split `.set_dims()` into `.expand_dims()` and `broadcast_to()` (pydata#8380) Add chunk-friendly code path to `encode_cf_datetime` and `encode_cf_timedelta` (pydata#8575) Fix NetCDF4 C version detection (pydata#8675) groupby: Don't set `method` by default on flox>=0.9 (pydata#8657) Fix automatic broadcasting when wrapping array api class (pydata#8669) Fix unstack method when wrapping array api class (pydata#8668) Fix `variables` arg typo in `Dataset.sortby()` docstring (pydata#8670) dt.weekday_name - removal of function (pydata#8664) Add `dev` dependencies to `pyproject.toml` (pydata#8661) CI: Pin scientific-python/upload-nightly-action to release sha (pydata#8662) Update HOW_TO_RELEASE.md by clarifying where RTD build can be found (pydata#8655) ruff: use extend-exclude (pydata#8649) new whats-new section (pydata#8652) xfail another test on windows (pydata#8648) use first element of residual in _nonpolyfit_1d (pydata#8647) whatsnew for v2024.01.1 implement `isnull` using `full_like` instead of `zeros_like` (pydata#7395) ...
this is the initial phase of our plan to split the
.set_dims()
method into two distinct methods.expand_dims()
and.broadcast_to()
as outlined in the design document.expand_dims()
broadcast_to()
.transpose()
and.T
toNamedArray
whats-new.rst