votes up 1

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

Package:
numpy
github stars 18118
Exception Class:
ValueError

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)

    com
😲  Walkingbet is Android app that pays you real bitcoins for a walking. Withdrawable real money bonus is available now, hurry up! 🚶

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/function_base.py 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 31.6k

Add a possible fix

Please authorize to post fix