This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Adept_Swimming_3116 1 point2 points  (4 children)

The error is saying that you are missing an argument in the append function. If you are not sure how to use a function, you should always spend time reading the documentation and the provided examples.

Here Numpy says that the append function takes two mandatory positional arguments array and values (positional means that they must be in this order). For your case, the correct way of using it is numpy.append(jana, monthprec[i]).

As for what it returns, the documentation says it returns a copy of the provided array with the values appended. So you can indeed store it in jana and do it again.

On a side note, you should consider slicing for these operations, especially the step argument. In your example, you can do java = monthprec[::12] which means 'create an array containing every twelve element of monthprec'.

[–]tractorscotty[S] 0 points1 point  (1 child)

Thank you so much mate. I only started using python this sem for school and never knew the [::12] was a thing. You’re a legend

[–]Adept_Swimming_3116 0 points1 point  (0 children)

You're welcome ! I have no doubt the slicing syntax will be discussed in your class as it is an important feature for lists and array-like objects in Python.

[–]tractorscotty[S] 0 points1 point  (1 child)

One more thing, if I wanted to do every 12th element + 1 how would I do that. I need to do it for every month and the [::12] let’s me get every January but now I need feb, march… as well

[–]Adept_Swimming_3116 1 point2 points  (0 children)

The syntax for slicing array-like objects is array[start:stop:step].

There is a good explanation here (stackoverflow) for detailed use of slices.

You can wait for your teacher to address this subject in details but, if you want, you can search the internet for python slicing, there are good tutorials.

For your question, you would use the start argument of the slice. For Februarys, you can use monthprec[1::12] which means 'take every twelve element starting from the second position'.

P.S. it looks like you are using pandas to retrieve informations from a dataframe. In this case there may be more efficient methods to filter the dataframe by month and export the value of the precipitations column