Skip to main content
 首页 » 编程设计

用于谷歌趋势的 python : "ValueError: year is out of range" when using interest_over_time

2025年02月15日9lidabo

我不熟悉在 python 中使用 pytrends 模块,它允许您从 Google 趋势中提取数据。该站点很好地介绍了该模块:https://github.com/GeneralMills/pytrends

我在使用 pytrend.interest_over_time() 时收到消息“ValueError:year is out of range”。我的代码的关键部分是:

import pytrends 
from pytrends.request import TrendReq 
 
google_username = "" #my username 
google_password = "" #my password 
 
pytrend = TrendReq(google_username, google_password, custom_useragent=None) 
 
pytrend.build_payload(kw_list=['Chipotle'], timeframe = 'today 5-y') 
pytrend.interest_over_time() 

然后我收到错误消息“ValueError:年份超出范围”

请您参考如下方法:

我有同样的问题,我在第180行的request.py中添加了astype('int')

来自:

    df['date'] = pd.to_datetime(df['time'], unit='s') 

到:

    df['date'] = pd.to_datetime(df['time'].astype('int'), unit='s') 

(不知道为什么我的行号不同,但看起来是同一个问题)