votes up 1

order must be 'n'o'n'-'n'egative but got 'n'

github stars 18118
Exception Class:

Raise code

""" np.diff(x)
    array([1, 1], dtype='timedelta64[D]')

    if n == 0:
        return a
    if n < 0:
        raise ValueError(
            "order must be non-negative but got " + repr(n))

    a = asanyarray(a)
    nd = a.ndim
    if nd == 0:
        raise ValueError("diff requires input that is at least one dimensional")
    axis = normalize_axis_index(axis, nd)

😲 Agile task management is now easier than calling a taxi. #Tracklify
🙏 Scream for help to Ukraine
Today, 8th August 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

Ways to fix

votes up 1 votes down

The method numpy.diff should be given a non negative order argument.

Code to reproduce the exception:

import numpy as np
x = np.array([1, 2, 4, 7, 0])
np.diff(x, n=-2)

ValueError Traceback (most recent call last)
<ipython-input-24-0c42bcf56fc6> in <module>()  1 import numpy as np  2 x = np.array([1, 2, 4, 7, 0]) ----> 3 np.diff(x, n=-2) 
<__array_function__ internals> in diff(*args, **kwargs) 
/usr/local/lib/python3.7/dist-packages/numpy/lib/ in diff(a, n, axis, prepend, append)  1251 if n < 0:  1252 raise ValueError( -> 1253 "order must be non-negative but got " + repr(n))  1254   1255 a = asanyarray(a) 
ValueError: order must be non-negative but got -2

Fixed version of the code:

import numpy as np
x = np.array([1, 2, 4, 7, 0])
np.diff(x, n=2)

May 09, 2022 kellemnegasi answer
kellemnegasi 30.6k

Add a possible fix

Please authorize to post fix