Skip to content

Commit

Permalink
Merge pull request #509 from punchagan/fix-no-geocode
Browse files Browse the repository at this point in the history
Fix fetching interest by region with CITY resolution
  • Loading branch information
emlazzarin authored Mar 26, 2022
2 parents e1eec7a + 9d143a0 commit 0e887b8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pytrends/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,17 @@ def interest_by_region(self, resolution='COUNTRY', inc_low_vol=False,
return df

# rename the column with the search keyword
df = df[['geoName', 'geoCode', 'value']].set_index(
['geoName']).sort_index()
geo_column = 'geoCode' if 'geoCode' in df.columns else 'coordinates'
columns = ['geoName', geo_column, 'value']
df = df[columns].set_index(['geoName']).sort_index()
# split list columns into separate ones, remove brackets and split on comma
result_df = df['value'].apply(lambda x: pd.Series(
str(x).replace('[', '').replace(']', '').split(',')))
if inc_geo_code:
result_df['geoCode'] = df['geoCode']
if geo_column in df.columns:
result_df[geo_column] = df[geo_column]
else:
print('Could not find geo_code column; Skipping')

# rename each column with its search term
for idx, kw in enumerate(self.kw_list):
Expand Down
5 changes: 5 additions & 0 deletions pytrends/test_trendReq.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ def test_interest_by_region(self):
pytrend.build_payload(kw_list=['pizza', 'bagel'])
self.assertIsNotNone(pytrend.interest_by_region())

def test_interest_by_region_city_resolution(self):
pytrend = TrendReq()
pytrend.build_payload(kw_list=['pizza', 'bagel'])
self.assertIsNotNone(pytrend.interest_by_region(resolution='CITY'))

def test_related_topics(self):
pytrend = TrendReq()
pytrend.build_payload(kw_list=['pizza', 'bagel'])
Expand Down

0 comments on commit 0e887b8

Please sign in to comment.