How to check if a variable is either a python list, numpy array or pandas series
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?
Solution 1:
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 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:
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:
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.