How to check if a variable is either a python list, numpy array or pandas series

arrays, list, numpy, pandas, python

Solution

You can do it using `isinstance`:

import pandas as pd
import numpy as np
def f(l):
    if isinstance(l,(list,pd.core.series.Series,np.ndarray)):
        print(5)
    else:
        raise Exception('wrong type')

Then `f([1,2,3])` prints 5 while `f(3.34)` raises an error.

Problem

I have a function that takes in a variable that would work if it is any of the following three types ``` 1. pandas Series 2. numpy array (ndarray) 3. python list ``` Any other type should be rejected. What is the most efficient way to check this?

Original source