I have a boolean array to indicate dry days:
dry_bool
0 False
1 False
2 False
3 False
4 False
...
15336 False
15337 False
15338 False
15339 False
15340 False
Name: budget, Length: 15341, dtype: object
The number of False and True values are:
dry_bool.value_counts()
False 14594
True 747
Name: budget, dtype: int64
I want to find contiguous periods of dry days (where dry_bool=True). Using the ndimage.label() function by the scipy package
import scipy.ndimage as ndimage
events, n_events = ndimage.label(dry_bool)
gives me the following error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[76], line 2
1 # Find contiguous regions of dry_bool = True
----> 2 events, n_events = ndimage.label(dry_bool)
File ~/opt/anaconda3/lib/python3.9/site-packages/scipy/ndimage/_measurements.py:219, in label(input, structure, output)
216 return output, maxlabel
218 try:
--> 219 max_label = _ni_label._label(input, structure, output)
220 except _ni_label.NeedMoreBits as e:
221 # Make another attempt with enough bits, then try to cast to the
222 # new type.
223 tmp_output = np.empty(input.shape, np.intp if need_64bits else np.int32)
File _ni_label.pyx:202, in _ni_label._label()
File _ni_label.pyx:239, in _ni_label._label()
File _ni_label.pyx:95, in _ni_label.__pyx_fused_cpdef()
TypeError: No matching signature found
which I cannot wrap my head around. Any clue what's going on?
There are definitely consecutive dry days, as you can see when I read the indices of the True values in the dry_bool array:
dry_idcs = [i for i, x in enumerate(dry_bool) if x]
print(dry_idcs[0:10])
[165, 205, 206, 214, 229, 230, 262, 281, 292, 301]
In your first code snippet it says dtype: object
, even though it's supposed to be an array of data type boolean (not object). Given that your error message gives you a TypeError
, maybe that's the problem?
You could try explicitly converting the data type of your array:
dry_bool = dry_bool.astype(bool)
If you provide more of your code (e.g. how you create/populate the array dry_bool
) it's easier to figure out what exactly the problem is.