2

I have been using scipy's binned_statistic_2d function to plot a 2d histogram of some data, particularly to return a list of the index of which bin the data is in, by setting the expand_binnumbers = True. It was working perfectly, until today. The following code demonstrates my problem:

import numpy as np
from scipy.stats import binned_statistic_2d as hist 

# my data is two arrays of numbers 
x = np.random.random((5,))
y = np.random.random((5,))

# I need to know which bin the values are in so I return the bin_idx
data = hist(x,y, bins = [2,2], statistic = 'count', values = None, expand_binnumbers = True)

bin_idx = data[3]

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Any ideas why this should suddenly stop working?

shadowbiscuit
  • 218
  • 1
  • 8
  • 1
    The `values` argument is required. The function doesn't make much sense without it. – Warren Weckesser Mar 10 '20 at 19:58
  • 1
    @WarrenWeckesser The scipy documentation tells me values is not used if count is my statistic: docs.scipy.org/doc/scipy/reference/generated/…‘count’ : compute the count of points within each bin. This is identical to an unweighted histogram. values array is not referenced. What should I put in its place? – shadowbiscuit Mar 10 '20 at 20:11
  • 1
    Ah, you're right. It looks like a bug that was introduced in a recent version of SciPy. – Warren Weckesser Mar 10 '20 at 20:13
  • 1
    Did you intentionally make `x` and `y` have shape `(1, 5)` instead of just `(5,)`? I think there is an easy work-around for the bug, but those shapes appear to cause a different issue. – Warren Weckesser Mar 10 '20 at 20:13
  • 1
    @WarrenWeckesser No, I just wanted to demonstrate the issue! I am working with 1D arrays in my actual project - i just wanted to make some 1D arrays with data. Is it more correct to write (5,) ? I will try.. – shadowbiscuit Mar 10 '20 at 20:18
  • @WarrenWeckesser yes, doing (5,) instead gets rid of an error 'too many values to unpack', but that isn't the main error I'm encountering in my code and a separate issue! I will edit my example code anyway so people can get the error I'm seeing. – shadowbiscuit Mar 10 '20 at 20:20
  • If `x` and `y` are 1-d, then the work-around for the bug is to pass the argument `values=x`. – Warren Weckesser Mar 10 '20 at 20:30
  • @WarrenWeckesser That works! Is there somewhere I should flag this bug up so they can fix it for others? And if you write that as an answer I will accept it as the solution. Thanks! – shadowbiscuit Mar 10 '20 at 20:43
  • I made a note of the problem in the pull request that introduced the bug: https://github.com/scipy/scipy/pull/10664#issuecomment-597298083 – Warren Weckesser Mar 10 '20 at 20:52
  • I think the open PR I have open fixes the issue: https://github.com/scipy/scipy/pull/11382/ – Lucas Roberts Mar 10 '20 at 22:07

1 Answers1

2

The recent update to Scipy broke things somewhat - as @WarrenWeckesser said in the comments, setting values = x makes things work again.

shadowbiscuit
  • 218
  • 1
  • 8