0

I have the ndarray "diffTemp":

diffTemp = np.diff([df.Temp])

Where Temp are temperature values whose differences I compute using the difference operator. In this case using print() I get:

print(diffTemp) = [[-0.16 -0.05]]

To convert it into a column vector I use:

diffTemp = diffTemp.transpose() 

And then convert is from ndarray into Series using:

diffTemp = pd.Series([diffTemp]) 

(This allows me later to concatenate diffTime with its corresponding Series dates (diffDates).)

Unfortunately this outputs that diffTemp is:

print(diffTemp) = 0    [[-0.16000000000000014], [-0.05000000000000071]]

If I instead use (i.e. without hard brackets [ ]), such that instead:

diffTemp = pd.Series(diffTemp)

I instead get the error message:

Exception: Data must be 1-dimensional

Totally new to Python and have tried google the last few days without any success. Any help is much much appreciated.

1 Answer 1

1

The issue here is that you are trying to convert a two-dimensional array into a 1-dimensional series. Notice that there are two brackets around [[-0.16 -0.05]]. You can write the following to get back a series by just grabbing the 1-d array that you want:

diffTemp = pd.Series(diffTemp[0])
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much!! That is brilliant, I had lost track of the dimensions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.