6

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?

3
  • 3
    I run the code you posted, and it works fine. Your error comes from something you didn't post. Commented Nov 18, 2019 at 9:49
  • 1
    your code works have a look at it for better practice stackoverflow.com/questions/13784192/… Commented Nov 18, 2019 at 9: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 Commented Nov 18, 2019 at 10:41

3 Answers 3

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")
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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.