Chris Waller
Chris Waller

Reputation: 81

python for Google Trends: "ValueError: year is out of range" when using interest_over_time

I am new to using the pytrends module in python that allows to you pull data from Google Trends. This site gives a good introduction to the module: https://github.com/GeneralMills/pytrends

I am getting the message "ValueError: year is out of range" when using pytrend.interest_over_time(). Key parts of my code are:

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()

I then get the error message "ValueError: year is out of range"

Upvotes: 2

Views: 5379

Answers (3)

Lukas
Lukas

Reputation: 163

As recommended in the documentation you can convert timestamps like this: tstamp.to_datetime64().astype('O')

Upvotes: 0

ralfr
ralfr

Reputation: 166

I had the same problem, i added astype('int') in request.py in line 180

from:

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

to:

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

(not sure why my line number is different, but looks like the same issue)

Upvotes: 1

Change the following in python...\Lib\site-packages\pytrends\request.py

Line 3:

from datetime import datetime

Around Line 128:

#df['date'] = pd.to_datetime(df['time'],unit='s')
df['date'] = df['time'].map(lambda d: datetime.fromtimestamp(int(d)))

This worked for me :)

Upvotes: 0

Related Questions