-
-
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
Use duck array ops in more places #8267
Conversation
for more information, see https://pre-commit.ci
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!
@@ -6,6 +6,7 @@ | |||
|
|||
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.
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?
@@ -50,7 +51,7 @@ def _access_through_cftimeindex(values, name): | |||
from xarray.coding.cftimeindex import CFTimeIndex | |||
|
|||
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?
@@ -69,7 +70,7 @@ def _access_through_series(values, name): | |||
"""Coerce an array of datetime-like values to a pandas Series and | |||
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) |
@@ -148,10 +149,10 @@ def _round_through_series_or_index(values, name, freq): | |||
from xarray.coding.cftimeindex import CFTimeIndex | |||
|
|||
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,))) |
@@ -195,7 +196,7 @@ def _strftime_through_cftimeindex(values, date_format: str): | |||
""" | |||
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,))) |
@@ -205,7 +206,7 @@ def _strftime_through_series(values, date_format: str): | |||
"""Coerce an array of datetime-like values to a pandas Series and | |||
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) |
@@ -2123,7 +2123,8 @@ def _calc_idxminmax( | |||
chunkmanager = get_chunked_array_type(array.data) | |||
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_ops
in some more places that were found to cause failures in cubed-dev/cubed-xarray#8whats-new.rst
api.rst