diff --git a/pytrends/request.py b/pytrends/request.py index 28499c43..4879ae83 100644 --- a/pytrends/request.py +++ b/pytrends/request.py @@ -179,7 +179,9 @@ def interest_over_time(self): result_df = df['value'].apply(lambda x: pd.Series(str(x).replace('[', '').replace(']', '').split(','))) # rename each column with its search term, relying on order that google provides... for idx, kw in enumerate(self.kw_list): - result_df[kw] = result_df[idx].astype('int') + # there is currently a bug with assigning columns that may be + # parsed as a date in pandas: use explicit insert column method + result_df.insert(len(result_df.columns), kw, result_df[idx].astype('int')) del result_df[idx] if 'isPartial' in df: @@ -377,29 +379,29 @@ def categories(self): def get_historical_interest(self, keywords, year_start=2018, month_start=1, day_start=1, hour_start=0, year_end=2018, month_end=2, day_end=1, hour_end= 0, cat=0, geo='', gprop='', sleep=0): """Gets historical hourly data for interest by chunking requests to 1 week at a time (which is what Google allows)""" - # construct datetime obejcts - raises ValueError if invalid parameters + # construct datetime obejcts - raises ValueError if invalid parameters start_date = datetime(year_start, month_start, day_start, hour_start) end_date = datetime(year_end, month_end, day_end, hour_end) - # the timeframe has to be in 1 week intervals or Google will reject it + # the timeframe has to be in 1 week intervals or Google will reject it delta = timedelta(days=7) df = pd.DataFrame() date_iterator = start_date - date_iterator += delta + date_iterator += delta while True: if (date_iterator > end_date): - # has retrieved all of the data + # has retrieved all of the data break - # format date to comply with API call + # format date to comply with API call start_date_str = start_date.strftime('%Y-%m-%dT%H') date_iterator_str = date_iterator.strftime('%Y-%m-%dT%H') tf = start_date_str + ' ' + date_iterator_str - try: + try: self.build_payload(keywords,cat, tf, geo, gprop) week_df = self.interest_over_time() df = df.append(week_df) @@ -408,10 +410,10 @@ def get_historical_interest(self, keywords, year_start=2018, month_start=1, day_ pass start_date += delta - date_iterator += delta + date_iterator += delta - # just in case you are rate-limited by Google. Recommended is 60 if you are. + # just in case you are rate-limited by Google. Recommended is 60 if you are. if sleep > 0: time.sleep(sleep) - return df \ No newline at end of file + return df