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

PythonArraysListPandasNumpy

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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

Solution 3 - Python

This all really depends on what you are trying to achieve (will you allow a tuple, how about a range object?), but to be a bit less restrictive but still disallow strings (which I am guessing is what you are really trying to achieve) you can use the following code.

import collections
import pandas
import numpy

def myfunc(x):
    if not isinstance(x, collections.abc.Iterable) or isinstance(x, (str, bytes)):
        raise ValueError('A non-string iterable is required')
    return 'Yay!'

myfunc([9, 7])
myfunc((9, 7))
myfunc(numpy.arange(9))
myfunc(range(9))
myfunc(pandas.Series([9, 7]))
myfunc('Boo')  # THIS WILL RAISE A ValueError!!!!!

Solution 4 - Python

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.

Solution 5 - Python

You can use isinstance like this:

import pandas as pd
import numpy as np

#Simple List
simple_list = [1,2]  

#numpy array
np_array = np.array(simple_list) 

#Pandas series
pandas_series = pd.Series(simple_list)  


if isinstance(simple_list, list):
    print("This is a list: ", simple_list)

if isinstance(np_array, np.ndarray):
    print("This is a numpy array: ", np_array)

if isinstance(pandas_series, pd.core.series.Series):
    print("This is pandas series: ", pandas_series)

Solution 6 - Python

While the accepted answer uses the built-in isinstance, which itself should the preferred way when the accepted dtypes is/are known.

In my opinion, it is much more flexible and robust to use checks from pandas.api.types which cover a broader dtype checking. For example, pd.api.types.is_list_like checks for objects that are considered list-like are for example Python lists, tuples, sets, NumPy arrays, and Pandas Series.

Notice here that we can test for nested as well as flat list like objects also,

import pandas as pd

pd.api.types.is_list_like([1,2,3]), pd.api.types.is_list_like(np.array([[1], [2], [3]]))    

> (True, True)

While below gives,

pd.api.types.is_list_like("This is a string not a list like object")

> False

This is particularly useful when more wider checks are required, for example using pandas.api.types.is_re_compilable to check if input string can be compiled into valid regex pattern and using pandas.api.types.is_datetime64_ns_dtype to check if input is of datetime64[ns] dtype.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZhang18View Question on Stackoverflow
Solution 1 - PythonMiriam FarberView Answer on Stackoverflow
Solution 2 - PythonVaishaliView Answer on Stackoverflow
Solution 3 - PythonSethMMortonView Answer on Stackoverflow
Solution 4 - PythonRick supports MonicaView Answer on Stackoverflow
Solution 5 - Pythonshreesh kattiView Answer on Stackoverflow
Solution 6 - PythonMayank MahawarView Answer on Stackoverflow