error: "cannot convert the series to <class 'int'>" trying to do a for loop in a dataframe column

Viewed 37

I'm working with Pytrends and I want to loop through a column of a dataframe, but I get the error: "cannot convert the series to <class 'int'>". I don't know where the problem is, I have reviewed the dataframe and the columns I work with are of type int.

index         ICO_Name    ticker       ico_launch_month     year    month   day
4             micromoney    amm     2017-10-18  00:00:00    2017    10      18
3             worldcore     nan     2017-10-14  00:00:00    2017    10      14
2             xave coin     xvc     2022-11-01  00:00:00    2022    11      1
1             sf capital    sfcp    2018-01-06  00:00:00    2018    1       6
0             cryptostone   cps     2022-06-13  00:00:00    2022    6       13
<class 'pandas.core.frame.DataFrame'>
Int64Index: 199 entries, 0 to 198
Data columns (total 6 columns):
 #   Column            Non-Null Count  Dtype         
---  ------            --------------  -----         
 0   ICO_Name          199 non-null    object        
 1   ticker            199 non-null    object        
 2   ico_launch_month  199 non-null    datetime64[ns]
 3   year              199 non-null    int64         
 4   month             199 non-null    int64         
 5   day               199 non-null    int64         
dtypes: datetime64[ns](1), int64(3), object(2)
memory usage: 10.9+ KB

The code:

pytrend = TrendReq()

for id in df_full_1:
  g_trends = pytrend.get_historical_interest(df_full_1["ICO_Name"], year_start=df_full_1["year"], month_start=df_full_1["month"], 
                                             day_start=df_full_1["day"], year_end= df_full_1["year"], 
                                             month_end=df_full_1["month"], day_end=df_full_1["day"], sleep=0).drop(columns='isPartial') 

The error:

TypeError                                 Traceback (most recent call last)
<ipython-input-56-e3cb7e0b950c> in <module>()
      4   g_trends = pytrend.get_historical_interest(df_full_1["ICO_Name"], year_start=df_full_1["year"], month_start=df_full_1["month"], 
      5                                              day_start=df_full_1["day"], year_end= df_full_1["year"],
----> 6                                              month_end=df_full_1["month"], day_end=df_full_1["day"], sleep=0).drop(columns='isPartial')
      7 
      8 g_trends

1 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/series.py in wrapper(self)
    183         if len(self) == 1:
    184             return converter(self.iloc[0])
--> 185         raise TypeError(f"cannot convert the series to {converter}")
    186 
    187     wrapper.__name__ = f"__{converter.__name__}__"

TypeError: cannot convert the series to <class 'int'>
1 Answers

I think you should come back to the documentation to check the parameters types of the function get_historical_interest, you should set the columns with integer values, for instance:

pytrend.get_historical_interest(..., year_start=2020, month_start=10, day_start=1, hour_start=0, year_end=2021, month_end=10, day_end=1,...)

Because df_full_1["day"] is a series and the function expects to get an integer rather than a series.

Related