1

I am trying to simplify some code with a function. The intent is to use the function to declare blank series to populate later.

The code currently declares each series on a separate line like this:

series1=pd.Series()

series2=pd.Series()

This approach works well but makes the code lengthy with many series.

I would like to do the following:

Create a list of blank objects to use in the function with the names series1, series2, etc. or with a more descriptive name for each

series_list=[series1,series2]

Declare function

def series(name):
    name=pd.Series()
    return name

Call function with input

for i in series_list:
    series(i)

However, when I try to declare the series_list, it returns the NameError: [variable] is not defined. Is there a way to populate the series_list with empty objects(i.e. no data but with the names series1, series2, ... series1000)?

carrie3zz
  • 43
  • 9
  • Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – G. Anderson Dec 16 '19 at 16:26
  • If you're getting an error, please include the code that generates the error, and the full error traceback – G. Anderson Dec 16 '19 at 16:27
  • @G.Anderson The code that generates the error is: series_list=[series1,series2] – carrie3zz Dec 16 '19 at 16:30
  • @G.Anderson that post referenced above does not appear to answer the question. – carrie3zz Dec 16 '19 at 16:35
  • If you want to create a variable number of variables without declaring them individually (which appears to be what you're asking) then a dict is the best way to do that. Your custom function is just a wrapper around the `pd.Series` method, so it's not doing any additional work. If you don;t need the objects to be called by name, then you can declare a blank list and use `append` to acheive the same thing – G. Anderson Dec 16 '19 at 16:40
  • Thanks for the response @G.Anderson. I know the number of variables I want to create, so that is where I lose you on the variable number of variables. All I want to do is iteratively declare many series. My issue is the simple approach shown above where it is done line-by-line works. I want to be able to create a list of all the series and then use a function or for loop to make them all series objects. My issue is with series_list. – carrie3zz Dec 16 '19 at 16:46
  • "use the function to declare blank series to populate later" and "makes the code lengthy with many series" are exactly the problems covered in the question. In your case, the "variable" number of variables is however many series you want to create – G. Anderson Dec 16 '19 at 16:49
  • Your comment is a bit confusing. You want to "create a list of all the series" then "make them all series objects". You do that when you "create the list of series" in the first place. Otherwise you're creating a list of...something...then trying to make them objects later. Hence, a variable number of variables. – G. Anderson Dec 16 '19 at 16:51
  • @G.Anderson Thanks again, but unfortunately, I can't see the parallels between my question and the post you reference. The problem is that I can't create the list . – carrie3zz Dec 16 '19 at 17:00
  • You can't create the list because you haven't declare the variables. See my answer below for how the marked duplicate solves your issue – G. Anderson Dec 16 '19 at 17:15
  • Terminology note: Python **doesn't have variable declarations**. – juanpa.arrivillaga Dec 16 '19 at 17:27
  • @juanpa.arrivillaga I wasn't aware of that, thank you for the information. I'll correct my terminology going forward. – G. Anderson Dec 16 '19 at 17:58

1 Answers1

0

Here's how you instantiate the Series objects iteratively, then use the generated list to assign to known variables

def assign_series(n):
    series_list = []
    #series_dict = {}
    num_of_series = n

    for i in range(num_of_series):
        series_list.append(pd.Series())
        #or if you want to call them by name
        #series_dict['series'+str(i)] = pd.Series()
    return series_list

corporate_securities, agency_securities, unrealized_gainloss = assign_series(3)

corporate_securities
Series([], dtype: float64)
G. Anderson
  • 5,815
  • 2
  • 14
  • 21
  • 1
    did you mean `series_list.append(pd.Series())`? – juanpa.arrivillaga Dec 16 '19 at 17:28
  • Yes, that is exactly what I meant. Thanks for catching that – G. Anderson Dec 16 '19 at 17:56
  • I guess my request is not clear, so I'll try again... I have many series to create. Instead of having all series declared on their own line, I would like to do it in less lines of code. In order to do this, I would like to create a list with the names of the series to be created. For example, series_list=[series1, series2, ... seriesn]. If I do this as shown, I get an error. How can I create this list? – carrie3zz Dec 16 '19 at 20:27
  • What I provided is the way to generate a list of objects without assigning them each individually and putting them in a list. I'm afraid I don't understand the distinction between what I provided and what you're after – G. Anderson Dec 16 '19 at 21:05
  • Maybe a combination of what I posted, along with [How to assign each element of a alist to a separate variable](https://stackoverflow.com/questions/19300174/how-to-assign-each-element-of-a-list-to-a-separate-variable) – G. Anderson Dec 16 '19 at 21:06
  • Thanks again for trying to help me @G.Anderson!!! I copied your illustrative code above and ran it to see what it would do. I need to have each of the series in the list to have a specific name such as series1 and series2 (or to not be generic- corporate_securities, agency securities, unrealized_gainloss, etc) . With the code you provided, I would still have to write the code line by line to get them named appropriately. – carrie3zz Dec 16 '19 at 21:10
  • In python, you simply can't use a variable you haven't yet assigned. In some way, shape, or form, you will need to type out the name of each variable and assign them a series object. It's just a matter of whether you do it on one line or individual lines. See my edit above for how to do it with a function and multiple assignment from a list – G. Anderson Dec 16 '19 at 21:23
  • This worked (the edited illustrative code)! Thank you. – carrie3zz Dec 16 '19 at 21:29