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

can't do in-place clip() with DataArrays. #1997

Closed
WeatherGod opened this issue Mar 16, 2018 · 4 comments
Closed

can't do in-place clip() with DataArrays. #1997

WeatherGod opened this issue Mar 16, 2018 · 4 comments

Comments

@WeatherGod
Copy link
Contributor

Code Sample, a copy-pastable example if possible

Where foo is a DataArray, there doesn't seem to be a nice way to use clip() in-place.

foo.clip(0, None, out=foo)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    foo.clip(0, None, out=foo)
  File "/rd22/scratch/broot/Programs/xarray/xarray/core/dataarray.py", line 1726, in func
    **kwargs))
  File "/rd22/scratch/broot/Programs/xarray/xarray/core/ops.py", line 205, in func
    return _call_possibly_missing_method(self, name, args, kwargs)
  File "/rd22/scratch/broot/Programs/xarray/xarray/core/ops.py", line 192, in _call_possibly_missing_method
    return method(*args, **kwargs)
TypeError: output must be an array

You get a similar exception if you do np.clip(foo, ..., out=foo).

Problem description

Note the docstring for DataArray.clip():

Help on method clip in module xarray.core.ops:

clip(self, *args, **kwargs) method of xarray.core.dataarray.DataArray instance
    a.clip(min=None, max=None, out=None)
    
    Return an array whose values are limited to ``[min, max]``.
    One of max or min must be given.
    
    Refer to `numpy.clip` for full documentation.
    
    See Also
    --------
    numpy.clip : equivalent function

So, the docstring advertises support for out.

@shoyer
Copy link
Member

shoyer commented Mar 16, 2018

Hmm. We currently just blindly copy the docstring/method from NumPy here. We should probably update this to clarify that we don't support out.

@WeatherGod
Copy link
Contributor Author

MaskedArrays had a similar problem, IIRC, because it was blindly copying the NDArray docstrings. Not going to be easy to do, though.

"we don't support out": Is that a general rule for xarray? Any notes on how to do what I want for clip? The function this was in was supposed to be general use (ndarrays and xarrays).

@fmaussion
Copy link
Member

"we don't support out": Is that a general rule for xarray?

Not really, but the tendency is to go towards getting rid of inplace operations altogether. np.clip(foo, 0, 0.2, out=foo.values) will do what you want though.

@max-sixty
Copy link
Collaborator

Closing as inplace generally removed, reopen if I'm missing something

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants