votes up 4

diff requires input that is at least one dimensional

Package:
numpy
github stars 18118
Exception Class:
ValueError

Raise code

    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)

    combined = []
    if prepend is not np._NoValue:
        prepend = np.asanyarray(prepend)
        if prepend.ndim == 0:
            shape = list(a.shape)
😲  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 5 votes down

Error Code:

import numpy as np
arr = 1
diff1 = np.diff(arr) <- ValueError: diff requires input that is at least one dimensional. 
print(diff1)

Fix code:

import numpy as np
arr = [1] <- Here you've to change it, array should be at least one dimension. So you have to insert a list or at least one-dimensional array.
diff1 = np.diff(arr)  
print(diff1)

Explanation:

np.diff contain a part of code-
nd = a.ndim
if nd == 0:
	raise ValueError("diff requires input that is at least one dimensional")

If you input a number rather than a list or ndarray(where n = N) then, the value of nd would be 0 in code, which will raise an error.

May 26, 2021 Rna1h answer
Rna1h 1.1k

Add a possible fix

Please authorize to post fix