2

I have a function defined like this:

def demand_cleaning(df=None, location, output,_input,tables):

And I would like to test if df was passed or not (df is a pandas DataFrame)

If df is not passed I want to do something like

if df is None:
    df = pd.read_pickle(tables + "Demand Raw")

But this test does not work now. and I get this

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
sophros
  • 14,672
  • 11
  • 46
  • 75
Nicolas
  • 392
  • 5
  • 14
  • I get the feeling that ValueError isn't coming from something you've shown us. – foxyblue Sep 15 '17 at 09:47
  • 1
    You understand that after changing your file, you need to save it before running? – cs95 Sep 15 '17 at 10:04
  • 1
    @GiantsLoveDeathMetal `bool(pd.DataFrame() == None)` raises that exact same ValueError. OP should change his test to `df is None`. `df== None` compares all `df`'s values to `None`, returning a DataFrame of booleans. The `if` then asks `bool()`, but `The truth value of a DataFrame is ambiguous` – Maarten Fabré Sep 15 '17 at 10:08
  • I reckon he is always passing a dataframe, so what he needs to do is check `if df.empty:` – foxyblue Sep 15 '17 at 10:12
  • `df is None` will always return `False` – foxyblue Sep 15 '17 at 10:13
  • @GiantsLoveDeathMetal obviously not, or he wouldn't try to overwrite it with the contents of a file if it were `None` – Maarten Fabré Sep 15 '17 at 10:14
  • I tried `bool(pd.DataFrame() == None)` and you're right about the ValueError. Neat to know. – foxyblue Sep 15 '17 at 10:15
  • 1
    @GiantsLoveDeathMetal if it is an optional argument to the function it can be `None`. What I do find strange is that this function signature doesn't give a `SyntaxError: non-default argument follows default argument` – Maarten Fabré Sep 15 '17 at 10:16
  • It finally worked, I forgot to re import the library is working on. Many thanks for all your support! – Nicolas Sep 15 '17 at 10:21

3 Answers3

7

You can say instead:

if df is None:

If you want to check for a dataframe contains data check:

if not df.empty:
foxyblue
  • 2,859
  • 2
  • 21
  • 29
  • If I try "if df is None:" but pass a dataframe i get this message ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). – Nicolas Sep 15 '17 at 09:48
  • 2
    @Nicolas there's no way you'd get that `ValueError` using `is` instead of `==`... – Jon Clements Sep 15 '17 at 09:55
  • 1
    @Nicolas You forgot to hit Ctrl+S and save your code. – cs95 Sep 15 '17 at 10:03
3

Try to perform a type check directly:

if isinstance(df, pandas.DataFrame):
    pass

Keep in mind, that the second argument of isinstance depends on the namespace you have pandas imported into. This usually is pd, which would yield to pd.DataFrame. Have a look at this article.

jhoepken
  • 1,842
  • 3
  • 17
  • 24
3

Something like this would do:

def g(var):
    if isinstance(var, pd.DataFrame):
        print("good to go")
    else:
        print("Nah!")
        print(type(var))

a = None 

b = pd.DataFrame()

print(g(a))

"""
output>>> 

Nah!
<type 'NoneType'>
"""

print(g(b))

"""
output>>> 

good to go
"""
Mohamed Ali JAMAOUI
  • 14,275
  • 14
  • 73
  • 117
  • I try this "if ~isinstance(df,pd.DataFrame):" but got "ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()." – Nicolas Sep 15 '17 at 09:58
  • Yeah, you're not helping with this repeated error message, show us the full traceback - see @Jon Clements♦ comment – foxyblue Sep 15 '17 at 09:59
  • That's the result i have "~(isinstance(d,pd.DataFrame)) Out[62]: -2" i guess this can't go in a if. How comes ~don't give back "false"? – Nicolas Sep 15 '17 at 10:09