Skip to content
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

Add to_numpy() and as_numpy() methods #5568

Merged
merged 33 commits into from
Jul 21, 2021
Merged

Conversation

TomNicholas
Copy link
Member

@TomNicholas TomNicholas commented Jul 2, 2021

  • Closes sparse and other duck array issues #3245
  • Tests added
  • Passes pre-commit run --all-files
  • User visible changes (including notable bug fixes) are documented in whats-new.rst
  • New functions/methods are listed in api.rst

@github-actions
Copy link
Contributor

github-actions bot commented Jul 2, 2021

Unit Test Results

         6 files  ±0           6 suites  ±0   51m 2s ⏱️ ±0s
16 202 tests ±0  14 476 ✔️ ±0  1 726 💤 ±0  0 ❌ ±0 
90 408 runs  ±0  82 242 ✔️ ±0  8 166 💤 ±0  0 ❌ ±0 

Results for commit c5ee050. ± Comparison against base commit c5ee050.

♻️ This comment has been updated with latest results.

@TomNicholas
Copy link
Member Author

Irritating linting error:

xarray/core/variable.py:1095: error: Incompatible return value type (got "Variable", expected "VariableType")  [return-value]
xarray/core/dataarray.py:661: error: Incompatible return value type (got "DataArray", expected "T_DataArray")  [return-value]
Found 2 errors in 2 files (checked 143 source files)

@TomNicholas TomNicholas added API design topic-arrays related to flexible array support labels Jul 2, 2021
@max-sixty
Copy link
Collaborator

@TomNicholas I think that should fix it. But it's become Haskell-esque!

@pep8speaks
Copy link

pep8speaks commented Jul 3, 2021

Hello @TomNicholas! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2021-07-21 21:11:19 UTC

@TomNicholas TomNicholas mentioned this pull request Jul 8, 2021
8 tasks
@dcherian dcherian requested a review from shoyer July 8, 2021 17:15
@TomNicholas
Copy link
Member Author

The only failure is the same as in #5600 , so I think this should be ready to merge.

@TomNicholas TomNicholas added plan to merge Final call for comments and removed needs review labels Jul 16, 2021
Copy link
Contributor

@dcherian dcherian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great. Thanks @TomNicholas

xarray/core/dataarray.py Outdated Show resolved Hide resolved
xarray/core/dataarray.py Outdated Show resolved Hide resolved
xarray/core/dataarray.py Show resolved Hide resolved
xarray/core/dataarray.py Show resolved Hide resolved
xarray/core/pycompat.py Outdated Show resolved Hide resolved
xarray/core/variable.py Outdated Show resolved Hide resolved
Comment on lines 1076 to 1078
try:
data = data.to_numpy()
except AttributeError:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this always failing? Should we avoid it until such a protocol actually exists?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be my inclination, too.

I can also imagine some other library implementing to_numpy() in a weird way, which could cause issues. At the least, there should be a defensive check of isinstance(data, np.ndarray) afterwards.

xarray/core/variable.py Outdated Show resolved Hide resolved
@@ -2540,3 +2542,68 @@ def test_clip(var):
var.mean("z").data[:, :, np.newaxis],
),
)


@pytest.mark.parametrize("Var", [Variable, IndexVariable])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could stick these in VariableSubclassObjects? Could also do that later if it's a good idea

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just had a go at that and it broke the pint-only test through interaction with TestVariableWithDask, which tries to chunk.

@TomNicholas
Copy link
Member Author

TomNicholas commented Jul 16, 2021 via email

xarray/core/variable.py Outdated Show resolved Hide resolved
xarray/core/variable.py Outdated Show resolved Hide resolved
xarray/core/variable.py Outdated Show resolved Hide resolved
@TomNicholas
Copy link
Member Author

The only error is the ffspec error now

@dcherian dcherian requested a review from shoyer July 21, 2021 20:43
Comment on lines 1076 to 1078
try:
data = data.to_numpy()
except AttributeError:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be my inclination, too.

I can also imagine some other library implementing to_numpy() in a weird way, which could cause issues. At the least, there should be a defensive check of isinstance(data, np.ndarray) afterwards.

xarray/core/variable.py Outdated Show resolved Hide resolved
@TomNicholas
Copy link
Member Author

Okay thanks @shoyer - I've removed the type check and the attempt to call .to_numpy().

@TomNicholas TomNicholas merged commit c5ee050 into pydata:main Jul 21, 2021
st-bender added a commit to st-bender/xarray that referenced this pull request Aug 10, 2021
* main: (31 commits)
  Refactor index vs. coordinate variable(s) (pydata#5636)
  pre-commit: autoupdate hook versions (pydata#5685)
  Flexible Indexes: Avoid len(index) in map_blocks (pydata#5670)
  Speed up _mapping_repr (pydata#5661)
  update the link to `scipy`'s intersphinx file (pydata#5665)
  Bump styfle/cancel-workflow-action from 0.9.0 to 0.9.1 (pydata#5663)
  pre-commit: autoupdate hook versions (pydata#5660)
  fix the binder environment (pydata#5650)
  Update api.rst (pydata#5639)
  Kwargs to rasterio open (pydata#5609)
  Bump codecov/codecov-action from 1 to 2.0.2 (pydata#5633)
  new blank whats-new for v0.19.1
  v0.19.0 release notes (pydata#5632)
  remove deprecations scheduled for 0.19 (pydata#5630)
  Make typing-extensions optional (pydata#5624)
  Plots get labels from pint arrays (pydata#5561)
  Add to_numpy() and as_numpy() methods (pydata#5568)
  pin fsspec (pydata#5627)
  pre-commit: autoupdate hook versions (pydata#5617)
  Add dataarray scatter with 3d support (pydata#4909)
  ...
dcherian added a commit to kmsquire/xarray that referenced this pull request Aug 11, 2021
* upstream/main: (31 commits)
  Refactor index vs. coordinate variable(s) (pydata#5636)
  pre-commit: autoupdate hook versions (pydata#5685)
  Flexible Indexes: Avoid len(index) in map_blocks (pydata#5670)
  Speed up _mapping_repr (pydata#5661)
  update the link to `scipy`'s intersphinx file (pydata#5665)
  Bump styfle/cancel-workflow-action from 0.9.0 to 0.9.1 (pydata#5663)
  pre-commit: autoupdate hook versions (pydata#5660)
  fix the binder environment (pydata#5650)
  Update api.rst (pydata#5639)
  Kwargs to rasterio open (pydata#5609)
  Bump codecov/codecov-action from 1 to 2.0.2 (pydata#5633)
  new blank whats-new for v0.19.1
  v0.19.0 release notes (pydata#5632)
  remove deprecations scheduled for 0.19 (pydata#5630)
  Make typing-extensions optional (pydata#5624)
  Plots get labels from pint arrays (pydata#5561)
  Add to_numpy() and as_numpy() methods (pydata#5568)
  pin fsspec (pydata#5627)
  pre-commit: autoupdate hook versions (pydata#5617)
  Add dataarray scatter with 3d support (pydata#4909)
  ...
dcherian added a commit to dcherian/xarray that referenced this pull request Aug 13, 2021
* upstream/main: (34 commits)
  Use same bool validator as other inputs (pydata#5703)
  conditionally disable bottleneck (pydata#5560)
  Refactor index vs. coordinate variable(s) (pydata#5636)
  pre-commit: autoupdate hook versions (pydata#5685)
  Flexible Indexes: Avoid len(index) in map_blocks (pydata#5670)
  Speed up _mapping_repr (pydata#5661)
  update the link to `scipy`'s intersphinx file (pydata#5665)
  Bump styfle/cancel-workflow-action from 0.9.0 to 0.9.1 (pydata#5663)
  pre-commit: autoupdate hook versions (pydata#5660)
  fix the binder environment (pydata#5650)
  Update api.rst (pydata#5639)
  Kwargs to rasterio open (pydata#5609)
  Bump codecov/codecov-action from 1 to 2.0.2 (pydata#5633)
  new blank whats-new for v0.19.1
  v0.19.0 release notes (pydata#5632)
  remove deprecations scheduled for 0.19 (pydata#5630)
  Make typing-extensions optional (pydata#5624)
  Plots get labels from pint arrays (pydata#5561)
  Add to_numpy() and as_numpy() methods (pydata#5568)
  pin fsspec (pydata#5627)
  ...
dcherian added a commit to dcherian/xarray that referenced this pull request Aug 13, 2021
* upstream/main: (307 commits)
  Use same bool validator as other inputs (pydata#5703)
  conditionally disable bottleneck (pydata#5560)
  Refactor index vs. coordinate variable(s) (pydata#5636)
  pre-commit: autoupdate hook versions (pydata#5685)
  Flexible Indexes: Avoid len(index) in map_blocks (pydata#5670)
  Speed up _mapping_repr (pydata#5661)
  update the link to `scipy`'s intersphinx file (pydata#5665)
  Bump styfle/cancel-workflow-action from 0.9.0 to 0.9.1 (pydata#5663)
  pre-commit: autoupdate hook versions (pydata#5660)
  fix the binder environment (pydata#5650)
  Update api.rst (pydata#5639)
  Kwargs to rasterio open (pydata#5609)
  Bump codecov/codecov-action from 1 to 2.0.2 (pydata#5633)
  new blank whats-new for v0.19.1
  v0.19.0 release notes (pydata#5632)
  remove deprecations scheduled for 0.19 (pydata#5630)
  Make typing-extensions optional (pydata#5624)
  Plots get labels from pint arrays (pydata#5561)
  Add to_numpy() and as_numpy() methods (pydata#5568)
  pin fsspec (pydata#5627)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API design plan to merge Final call for comments topic-arrays related to flexible array support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sparse and other duck array issues
8 participants