Search code examples
pythongoogle-trends

Use pytrend to return results over the past 12 months


The function pytrend.interest_over_time() returns results for every week. How can I return results for the past year? Here is my input:

pytrend.build_payload(
    kw_list=[companies[0], companies[1], companies[2], companies[3], companies[4]])
df = pytrend.interest_over_time()
print(df.head(10))

Output:

Walmart  Amazon  Apple Inc.  CVS Health  ExxonMobil  isPartial
date                                                                      
2016-07-10       18      54           0           0           0      False
2016-07-17       17      47           0           0           0      False
2016-07-24       18      49           0           0           0      False
2016-07-31       18      49           0           0           0      False
2016-08-07       16      48           0           0           0      False
2016-08-14       16      45           0           0           0      False
2016-08-21       16      48           0           0           0      False
2016-08-28       17      49           0           0           0      False
2016-09-04       17      49           0           0           0      False
2016-09-11       16      48           0           0           0      False

[Done] exited with code=0 in 3.963 seconds

I want the output just for 2020. I haven't been able to find this answer through google searches or in pytrend's documentation: https://pypi.org/project/pytrends/


Solution

  • Use timeframe parameter to specify start and end date, Following code snippet will solve your problem

    from pytrends.request import TrendReq
    
    pytrend = TrendReq()
    pytrend.build_payload(kw_list=['Walmart','Amazon','Apple Inc.','CVS Health','ExxonMobil'],timeframe = '2020-01-01 2020-12-31')
    df = pytrend.interest_over_time()
    print(df.head(10))
    

    More details about the parameter is available in documentation of pytrends(link attached in question) check this attached screenshot for reference enter image description here