How to check if a variable is either a python list, numpy array or pandas series
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
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 4
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 5
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)
Related videos on Youtube
Zhang18
Updated on June 23, 2021Comments
-
Zhang18 almost 3 years
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?
-
SethMMorton about 7 yearsWhat about a
tuple
? I ask because you might be over-restricting your input. -
Zhang18 about 7 yearsSo what do you suggest to check if the input is a list-like object, including np.ndarray and pd.Series? I was looking for something more elegant than the currently accepted answer anyway.
-
-
Zhang18 about 7 yearsWhat's the difference between
pd.core.series.Series
andpd.Series
? -
Miriam Farber about 7 years@Zhang18, you could use pd.Series as well. I used pd.core.series.Series just because type(pd.Series([1,2,3])) gives pd.core.series.Series. Yet, both of them work, as pd.Series==pd.core.series.Series is True
-
hBy2Py almost 3 yearsLikely,
Series
is actually defined in thepd.core.series
module, but then theSeries
class is imported from there into the top-level__init__.py
for convenience.