---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-31-29405b792f60> in <module>
2 for yr in yr_list[:2]:
3 print(type(yr))
----> 4 temp_df = nifty_data[nifty_data['year'].isin(yr)]
5 print(temp_df.head())
~\.conda\envs\py36\lib\site-packages\pandas\core\series.py in isin(self, values)
4683 Name: animal, dtype: bool
4684 """
-> 4685 result = algorithms.isin(self, values)
4686 return self._constructor(result, index=self.index).__finalize__(
4687 self, method="isin"
~\.conda\envs\py36\lib\site-packages\pandas\core\algorithms.py in isin(comps, values)
416 if not is_list_like(values):
417 raise TypeError(
--> 418 "only list-like objects are allowed to be passed "
419 f"to isin(), you passed a [{type(values).__name__}]"
420 )
TypeError: only list-like objects are allowed to be passed to isin(), you passed a [int]
This occurs when trying to perform an operation that requires a list as an input but you pass an element of a list.
Can typically occur when trying to subset a dataframe multiple times in a loop by using each element of the list a variable to subset upon.
Solution is to input the variable in square brackets - list format. See example below of correct and incorrect code
yr_list = [2005, 2006, 2007,2008]
# Incorect example - look for ".isin(yr)"
for yr in yr_list:
print(type(yr))
temp_df = nifty_data[nifty_data['year'].isin(yr)]
print(temp_df.head())
This will result in the error above
# Correct example - with square brackets added ".isin([yr])"
for yr in yr_list:
print(type(yr))
temp_df = nifty_data[nifty_data['year'].isin([yr])]
print(temp_df.head())
Comments
Post a Comment