'how to check if a None is not passed as an argument where a pandas dataframe is expected

I have a function which looks like below.

def some_func(df:pd.Dataframe=pd.Dataframe()):
  if not df or df.empty:
    //some dataframe operations

I want to ensure that someone hasn't called the above function with : some_func(None).

But with the above check, I get ValueError: The truth value of a DataFrame is ambiguous.

How do I ensure that I do a None check before starting dataframe operations ?



Solution 1:[1]

You can check if the argument is None like so:

def some_func(df:pd.Dataframe=pd.Dataframe()):
  if df is None:
    # some dataframe operations

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1