Repository: GeneralMills/pytrends

Issue:
"['geoCode'] not in index" when request CITY LEVEL interest_by_region Google trends
Hi, the pytrends API developers and maintainers,

Thank you for developing this great API and I'm now just starting to use it for my research. 
I recently ran into a key error when I tried to get Google treands data on city level.
My code is as follows:

import pandas as pd                        
from pytrends.request import TrendReq
pytrend = TrendReq()

kw_list=['Coronavirus']
pytrend.build_payload(kw_list, timeframe='today 3-m', geo='US', gprop='')
pytrend.interest_by_region(resolution='CITY',inc_low_vol=True, inc_geo_code=False)

**This is the error message.**
KeyError                                  Traceback (most recent call last)
<ipython-input-19-91b520252342> in <module>
----> 1 pytrend.interest_by_region(resolution='CITY',inc_low_vol=False, inc_geo_code=False)

~/anaconda3/envs/pytorch_env/lib/python3.6/site-packages/pytrends/request.py in interest_by_region(self, resolution, inc_low_vol, inc_geo_code)
    282 
    283         # rename the column with the search keyword
--> 284         df = df[['geoName', 'geoCode', 'value']].set_index(
    285             ['geoName']).sort_index()
    286         # split list columns into seperate ones, remove brackets and split on comma

~/anaconda3/envs/pytorch_env/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2999             if is_iterator(key):
   3000                 key = list(key)
-> 3001             indexer = self.loc._convert_to_indexer(key, axis=1, raise_missing=True)
   3002 
   3003         # take() does not accept boolean indexers

~/anaconda3/envs/pytorch_env/lib/python3.6/site-packages/pandas/core/indexing.py in _convert_to_indexer(self, obj, axis, is_setter, raise_missing)
   1283                 # When setting, missing keys are not allowed, even with .loc:
   1284                 kwargs = {"raise_missing": True if is_setter else raise_missing}
-> 1285                 return self._get_listlike_indexer(obj, axis, **kwargs)[1]
   1286         else:
   1287             try:

~/anaconda3/envs/pytorch_env/lib/python3.6/site-packages/pandas/core/indexing.py in _get_listlike_indexer(self, key, axis, raise_missing)
   1090 
   1091         self._validate_read_indexer(
-> 1092             keyarr, indexer, o._get_axis_number(axis), raise_missing=raise_missing
   1093         )
   1094         return keyarr, indexer

~/anaconda3/envs/pytorch_env/lib/python3.6/site-packages/pandas/core/indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
   1183             if not (self.name == "loc" and not raise_missing):
   1184                 not_found = list(set(key) - set(ax))
-> 1185                 raise KeyError("{} not in index".format(not_found))
   1186 
   1187             # we skip the warning on Categorical/Interval

KeyError: "['geoCode'] not in index"

I actually tried all API methods, and they worked very well, except for this one.

Thank you for your time!

Best,
Han






Comments:

Patch:
diff --git a/pytrends/request.py b/pytrends/request.py
index 6cb9f8d..446cf41 100644
--- a/pytrends/request.py
+++ b/pytrends/request.py
@@ -299,13 +299,17 @@ class TrendReq(object):
             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):


Test patch:
diff --git a/pytrends/test_trendReq.py b/pytrends/test_trendReq.py
index 3e774b9..6a9f178 100644
--- a/pytrends/test_trendReq.py
+++ b/pytrends/test_trendReq.py
@@ -60,6 +60,11 @@ class TestTrendReq(TestCase):
         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'])