Skip to content

Commit

Permalink
Fixed bug where requests for date-like keywords would raise a broadcast
Browse files Browse the repository at this point in the history
error.
  • Loading branch information
richardwu committed Jul 29, 2018
1 parent 88e6b6a commit 2774482
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions pytrends/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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
return df

0 comments on commit 2774482

Please sign in to comment.