pytrends
pytrends copied to clipboard
KeyError: "['geoCode'] not in index"
hello, I'm trying to make a research on city level. I've started with classic case 'Blockchain' as test. My code is:
from pytrends.request import TrendReq
pytrends = TrendReq(hl='en-US', tz=360)
kw_list = ["Blockchain"]
pytrends.build_payload(kw_list, cat=0, timeframe='all', geo='', gprop='')
a = pytrends.interest_by_region(resolution='CITY', inc_low_vol=True, inc_geo_code=False)
print(a)
This reqeust gives me
Traceback (most recent call last):
File "C:/Users/***/Lib/venv/scripts/Prova.google.py", line 7, in <module>
a = pytrends.interest_by_region(resolution='CITY', inc_low_vol=True, inc_geo_code=False)
File "C:\Users\***\lib\site-packages\pytrends\request.py", line 282, in interest_by_region
df = df[['geoName', 'geoCode', 'value']].set_index(
File "C:\Users\***\lib\site-packages\pandas\core\frame.py", line 2934, in __getitem__
raise_missing=True)
File "C:\Users\***\lib\site-packages\pandas\core\indexing.py", line 1354, in _convert_to_indexer
return self._get_listlike_indexer(obj, axis, **kwargs)[1]
File "C:\Users\***\lib\site-packages\pandas\core\indexing.py", line 1161, in _get_listlike_indexer
raise_missing=raise_missing)
File "C:\Users\***\lib\site-packages\pandas\core\indexing.py", line 1252, in _validate_read_indexer
raise KeyError("{} not in index".format(not_found))
KeyError: "['geoCode'] not in index"
So does not work. If I specify in payload geo='IT
only gives me regional level (even if I specify "CITY" resolution) (I have to search interest only on city in italy) while if I use "COUNTRY" resolution works well, even without geo specification (but I do not need it). I saw can be related to #189 (no trend data)) but I do not know how to fix request file. Any advice?
Thank you
A quick fix would be in request.py
change
if self.geo == '':
self.interest_by_region_widget['request'][
'resolution'] = resolution
elif self.geo == 'US' and resolution in ['DMA', 'CITY', 'REGION']:
self.interest_by_region_widget['request'][
'resolution'] = resolution
to
if self.geo == '':
self.interest_by_region_widget['request'][
'resolution'] = resolution
elif resolution in ['DMA', 'CITY', 'REGION']:
self.interest_by_region_widget['request'][
'resolution'] = resolution
and change
if (df.empty):
return df
# rename the column with the search keyword
df = df[['geoName', 'geoCode', 'value']].set_index(
['geoName']).sort_index()
to
if (df.empty):
return df
# rename the column with the search keyword
df = df[['geoName', 'value']].set_index(
['geoName']).sort_index()
# split list columns into seperate ones, remove brackets and split on comma
I am considering creating a pull request.
Thanks
In my context (asking for a non US place), the solution worked (only change the second part: removed the geocode)
Just wonder does anyone get this fixed? I had change these 2 section as mentioned above, but still getting the same error:
from pytrends.request import TrendReq pytrends = TrendReq(hl='en-US', tz=360) kw_list = ["Blockchain"] pytrends.build_payload(kw_list, cat=0, timeframe='all', geo='', gprop='') a = pytrends.interest_by_region(resolution='CITY', inc_low_vol=True, inc_geo_code=False) print(a)
KeyError Traceback (most recent call last)
D:\ProgramData\Anaconda3\lib\site-packages\pytrends\request.py in interest_by_region(self, resolution, inc_low_vol, inc_geo_code) 284 285 # rename the column with the search keyword --> 286 df = df[['geoName', 'value']].set_index( 287 ['geoName']).sort_index() 288 # split list columns into seperate ones, remove brackets and split on comma
D:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in getitem(self, key) 2804 if is_iterator(key): 2805 key = list(key) -> 2806 indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1] 2807 2808 # take() does not accept boolean indexers
D:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py in _get_listlike_indexer(self, key, axis, raise_missing) 1550 1551 self._validate_read_indexer( -> 1552 keyarr, indexer, o._get_axis_number(axis), raise_missing=raise_missing 1553 ) 1554 return keyarr, indexer
D:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing) 1643 if not (self.name == "loc" and not raise_missing): 1644 not_found = list(set(key) - set(ax)) -> 1645 raise KeyError(f"{not_found} not in index") 1646 1647 # we skip the warning on Categorical/Interval
KeyError: "['geoCode'] not in index"
I've tried to replicate the issue but the original code from @giuspataro now works:
from pytrends.request import TrendReq
pytrends = TrendReq(hl='en-US', tz=360)
kw_list = ["Blockchain"]
pytrends.build_payload(kw_list, cat=0, timeframe='all', geo='', gprop='')
a = pytrends.interest_by_region(resolution='CITY', inc_low_vol=True, inc_geo_code=False)
print(a)
Output:
Blockchain
geoName
Abeokuta 21
Abidjan 4
Abuja 7
Accra 10
Addis Ababa 2
... ...
Woluwe-Saint-Pierre 5
Yaounde 5
Yonkers 4
Zoetermeer 2
tx. An Nhơn 0
[200 rows x 1 columns]