Skip to content

[Bug] Reduce fails when no axis given #2768

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

Closed
TomNicholas opened this issue Feb 13, 2019 · 1 comment
Closed

[Bug] Reduce fails when no axis given #2768

TomNicholas opened this issue Feb 13, 2019 · 1 comment
Labels

Comments

@TomNicholas
Copy link
Member

DataArray.reduce() fails if you try to reduce using a function which doesn't accept any axis arguments.

import numpy as np
from xarray import DataArray

da = DataArray(np.array([[1, 3, 3], [2, 1, 5]]))

def total_sum(data):
    return np.sum(data.flatten())

sum = da.reduce(total_sum)
print(sum)

This should print a dataarray with just the number 15 in it, but instead it throws the error

Traceback (most recent call last):
  File "mwe.py", line 9, in <module>
    sum = da.reduce(total_sum)
  File "/home/tegn500/Documents/Work/Code/xarray/xarray/core/dataarray.py", line 1605, in reduce
    var = self.variable.reduce(func, dim, axis, keep_attrs, **kwargs)
  File "/home/tegn500/Documents/Work/Code/xarray/xarray/core/variable.py", line 1365, in reduce
    axis=axis, **kwargs)
TypeError: total_sum() got an unexpected keyword argument 'axis'

This contradicts what the docstring of .reduce() says:

axis: int or sequence of int, optional
    Axis(es) over which to repeatedly apply func. Only one of the ‘dim’ and ‘axis’ 
    arguments can be supplied. If neither are supplied, then the reduction is 
    calculated over the flattened array (by calling f(x) without an axis argument).

The problem is that in variable.py an axis=None kwarg is always passed to func, even if no axis argument is given by the user in reduce.

@TomNicholas TomNicholas mentioned this issue Feb 13, 2019
3 tasks
@shoyer shoyer added the bug label Feb 15, 2019
@shoyer
Copy link
Member

shoyer commented Feb 15, 2019

Yep, looks like a bug to me (fortunately it's also an easy fix)

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

No branches or pull requests

2 participants