-
-
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
data/coords alias #8054
Comments
Thanks for opening your first issue here at xarray! Be sure to follow the issue template! |
If the goal is for a variable and its aliases to share the same underlying data, you could already do it (xarray Dataset is "just" a fancy dict-like container): import numpy as np
import xarray as xr
arr = np.array([1, 2, 3])
ds = xr.Dataset({"foo": ("x", arr)})
ds["bar"] = ds["foo"]
ds.foo[0] = 4
print(ds)
# <xarray.Dataset>
# Dimensions: (x: 3)
# Dimensions without coordinates: x
# Data variables:
# foo (x) int64 4 2 3
# bar (x) int64 4 2 3 This won't work with variable attributes and also likely neither with indexed coordinate variables, though.
That could be possible, but I'm not sure the extra convenience it provides (for a few cases?) would be worth the extra cost in maintenance, complexity, etc. This could also be easily done externally IMHO. |
Hmm, indeed that is quite neat. I don't like that it displays as a separate data so the fact that they are aliased is not obvious. But indeed I think this can be handled externally. Implementation wise that would mean to make a separate wrapper class for |
I was rather thinking about just using a dictionary of name aliases like aliases = {"t": "time"}
ds[aliases["t"]].identical(ds["time"])
# True For a bit more convenience, you could create a Dataset accessor to get the original item from the alias name: @xr.register_dataset_accessor("aliases")
class AliasesAccessor:
def __init__(self, xarray_obj):
self._obj = xarray_obj
self._aliases = aliases
def __getitem__(self, key):
return self._obj[self._aliases[key]] ds = xr.Dataset({"time": [1, 2, 3]})
ds.aliases["t"].identical(ds["time"])
# True |
Thanks, that's really interesting. I'll think about how to design along those lines |
note that a big part of cf-xarray is basically that: a way to look up aliases using standardized metadata (ie. metadata following the CF conventions) |
👍 cf-xarray seems like the way to go |
It would be nice to allow in dataset/dataarray to alias a coordinate/data. E.g.
ds['time'] == ds['t']
. This would be beneficial for midleware libraries to create more natural interface.Implementation wise, this can be restricted to the indexers of the dataset/dataarray, and internally save a dictionary of aliases.
The text was updated successfully, but these errors were encountered: