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

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.


Python type() should do the job here

l = [1,2]
s= pd.Series(l)
arr = np.array(l) 

When you print

type(l)
list

type(s)
pandas.core.series.Series

type(arr)
numpy.ndarray

The other answers are good but I sort of prefer this way:

if np.ndim(l)!=0:
    # this is something like a series, list, ndarray, etc.

It is nice because it provides more duck-typing flexibility compared to:

if isinstance(l, (pd.Series, list, np.ndarray)):
    # this is ONLY a pd.Series, list, or ndarray

...but it is better than this, which will allow a string or an iterator though-- both of which are often not wanted:

if isinstance(l, typing.Iterable):
    # this any iterable

...or this, which excludes a string but (weirdly) does not exclude an iterator:

if not np.isscalar(l):
    # this is something like a series, list, ndarray, etc.

However, if you truly only want a list, ndarray, or Series, the other answers are preferable.