4

I am trying to create a python function that plots the data from a DataFrame. The parameters should either be just the data. Or the data and the standard deviation.

As a default parameter for the standard deviation, I want to use an empty DataFrame.

def plot_average(avg_df, stdev=pd.DataFrame()):           
    if not stdev.empty:
        ...
    ...

But implementing it like that gives me the following error message:

TypeError: 'module' object is not callable

How can an empty DataFrame be created as a default parameter?

matiit
  • 7,969
  • 5
  • 41
  • 65
Bajellor
  • 257
  • 1
  • 4
  • 11
  • 3
    I run the code you posted, and it works fine. Your error comes from something you didn't post. – Aryerez Nov 18 '19 at 09:49
  • 1
    your code works have a look at it for better practice https://stackoverflow.com/questions/13784192/creating-an-empty-pandas-dataframe-then-filling-it – nithin Nov 18 '19 at 09:52
  • I think you are right. But for me the code doesn't work if I remove everything from the function. In the same file I have another function, but that one works just fine – Bajellor Nov 18 '19 at 10:41

3 Answers3

3

for a default empty dataframe :

def f1(my_df=None):
    if(my_df is None):
        my_df = pd.DataFrame()
    #stuff to do if it's not empty
    if(len(my_df) != 0):
        print(my_df)
    elif(len(my_df) == 0):
        print("Nothing")
sidiyahya
  • 31
  • 1
2

A DataFrame is mutable, so a better approach is to default to None and then assign the default value in the function body. See https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments

KPLauritzen
  • 1,719
  • 13
  • 23
0

The problem lies not in the creation of a new DataFrame but in the way the function was called. I use pycharm scientific. In which I had the function call written in a block. Executing this block called the function which was, i presume, not compiled.

Executing the whole programm made it possible to call the function

Bajellor
  • 257
  • 1
  • 4
  • 11