0

This answer is what I'm looking for, but since boolean subtraction has been depreciated it no longer works. Is there a better way?

A   = np.array([[-2, -1,  1], 
                [-1,  0,  1], 
                [-1,  1,  2]], dtype=float)

zcs = np.diff(np.signbit(A), axis=1)     # find zero crossings in each row - now fails

generates:

Traceback (most recent call last):
  File "/Users/david/Documents/wow.py", line 4, in <module>
    zcs = np.diff(np.signbit(A), axis=1)     # now fails
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/function_base.py", line 1926, in diff
    return a[slice1]-a[slice2]

TypeError: numpy boolean subtract, the `-` operator, is deprecated, use the bitwise_xor, the `^` operator, or the logical_xor function instead.
uhoh
  • 3,713
  • 6
  • 42
  • 95
  • 1
    Works without error for me. What's your NumPy version? – Divakar Jan 17 '18 at 05:14
  • @Divakar '1.13.0' – uhoh Jan 17 '18 at 05:15
  • 1
    Upgrade to 1.14.0? – Divakar Jan 17 '18 at 05:15
  • @Divakar OK I just found/tried an Anaconda installation and indeed it works fine, problem solved. What to do in this case? Would you like to post a short answer? – uhoh Jan 17 '18 at 05:17
  • 1
    Nah, I am good :) This post isn't adding anything new, so maybe delete this one. – Divakar Jan 17 '18 at 05:18
  • @Divakar it seems you are! I quickly checked the [release notes](https://github.com/numpy/numpy/blob/master/doc/release/1.14.0-notes.rst) didn't see anything obvious but did find out support for Python 2 is ending which is good to know. Thanks! – uhoh Jan 17 '18 at 05:24
  • Hmm, I guess could be useful for people stuck with old versions. Take your call. Seems trivial to me. – Divakar Jan 17 '18 at 05:26
  • 1
    In any case, `zcs = np.diff(np.signbit(A).astype(int), axis=1)` should work. – Daniel F Jan 17 '18 at 07:19
  • @DanielF I was using signbit based on the linked answer *using* `numpy.signbit()` *is a little bit quicker than* `numpy.sign()`, but certainly recasting would work immediately in a pinch, I hadn't thought of that. Thanks! – uhoh Jan 17 '18 at 08:04

1 Answers1

1

This is apparently fixed in 1.14.0, but as a quick fix explicit type casting:

zcs = np.diff(np.signbit(A).astype(int), axis=1) 

should work.

Daniel F
  • 13,620
  • 2
  • 29
  • 55