-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Use duck array ops in more places #8267
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
Conversation
TomNicholas
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for these additions @tomwhite ! Obviously I missed these ones.
Don't forget to add a note in whatsnew!
|
|
||
| import xarray as xr | ||
| from xarray import DataArray, Dataset, set_options | ||
| from xarray.core import duck_array_ops |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This effectively changes the test from only testing public xarray API to now relying on some internals too, which is not ideal from a code separation perspective.
In practice I think this is probably fine here, and I know you're doing it to allow Cubed arrays to go through the modified tests, but just something to bear in mind.
Illviljan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it better to get used to using reshape directly?
|
|
||
| if not isinstance(values, CFTimeIndex): | ||
| values_as_cftimeindex = CFTimeIndex(values.ravel()) | ||
| values_as_cftimeindex = CFTimeIndex(duck_array_ops.ravel(values)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| values_as_cftimeindex = CFTimeIndex(duck_array_ops.ravel(values)) | |
| values_as_cftimeindex = CFTimeIndex(duck_array_ops.reshape(values, (-1,))) |
Can't we just get used to using reshape instead?
| access requested datetime component | ||
| """ | ||
| values_as_series = pd.Series(values.ravel(), copy=False) | ||
| values_as_series = pd.Series(duck_array_ops.ravel(values), copy=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| values_as_series = pd.Series(duck_array_ops.ravel(values), copy=False) | |
| values_as_series = pd.Series(duck_array_ops.reshape(values, (-1,)), copy=False) |
|
|
||
| if is_np_datetime_like(values.dtype): | ||
| values_as_series = pd.Series(values.ravel(), copy=False) | ||
| values_as_series = pd.Series(duck_array_ops.ravel(values), copy=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| values_as_series = pd.Series(duck_array_ops.ravel(values), copy=False) | |
| values_as_series = pd.Series(duck_array_ops.reshape(values, (-1,)), copy=False) |
| method = getattr(values_as_series.dt, name) | ||
| else: | ||
| values_as_cftimeindex = CFTimeIndex(values.ravel()) | ||
| values_as_cftimeindex = CFTimeIndex(duck_array_ops.ravel(values)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| values_as_cftimeindex = CFTimeIndex(duck_array_ops.ravel(values)) | |
| values_as_cftimeindex = CFTimeIndex(duck_array_ops.reshape(values, (-1,))) |
| from xarray.coding.cftimeindex import CFTimeIndex | ||
|
|
||
| values_as_cftimeindex = CFTimeIndex(values.ravel()) | ||
| values_as_cftimeindex = CFTimeIndex(duck_array_ops.ravel(values)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| values_as_cftimeindex = CFTimeIndex(duck_array_ops.ravel(values)) | |
| values_as_cftimeindex = CFTimeIndex(duck_array_ops.reshape(values, (-1,))) |
| apply string formatting | ||
| """ | ||
| values_as_series = pd.Series(values.ravel(), copy=False) | ||
| values_as_series = pd.Series(duck_array_ops.ravel(values), copy=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| values_as_series = pd.Series(duck_array_ops.ravel(values), copy=False) | |
| values_as_series = pd.Series(duck_array_ops.reshape(values, (-1,)), copy=False) |
| chunks = dict(zip(array.dims, array.chunks)) | ||
| dask_coord = chunkmanager.from_array(array[dim].data, chunks=chunks[dim]) | ||
| res = indx.copy(data=dask_coord[indx.data.ravel()].reshape(indx.shape)) | ||
| data = dask_coord[duck_array_ops.ravel(indx.data)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| data = dask_coord[duck_array_ops.ravel(indx.data)] | |
| data = dask_coord[duck_array_ops.reshape(indx.data, (-1,))] |
| def ravel(array): | ||
| return reshape(array, (-1,)) | ||
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| def ravel(array): | |
| return reshape(array, (-1,)) |
|
Thanks @Illviljan I think using ravel is a bit more readable and newcomer friendly. |
* upstream/main: (46 commits) xfail flaky test (pydata#8299) Most of mypy 1.6.0 passing (pydata#8296) Rename `reset_encoding` to `drop_encoding` (pydata#8287) Enable `.rolling_exp` to work on dask arrays (pydata#8284) Fix `GroupBy` import (pydata#8286) Ask bug reporters to confirm they're using a recent version of xarray (pydata#8283) Add pyright type checker (pydata#8279) Improved typing of align & broadcast (pydata#8234) Update ci-additional.yaml (pydata#8280) Fix time encoding regression (pydata#8272) Allow a function in `.sortby` method (pydata#8273) make more args kw only (except 'dim') (pydata#6403) Use duck array ops in more places (pydata#8267) Don't raise rename warning if it is a no operation (pydata#8266) Mandate kwargs on `to_zarr` (pydata#8257) copy the `dtypes` module to the `namedarray` package. (pydata#8250) Add xarray-regrid to ecosystem.rst (pydata#8270) Use strict type hinting for namedarray (pydata#8241) Update type annotation for center argument of dataaray_plot methods (pydata#8261) [pre-commit.ci] pre-commit autoupdate (pydata#8262) ...
This uses
duck_array_opsin some more places that were found to cause failures in cubed-dev/cubed-xarray#8whats-new.rstapi.rst