A package to download long-term Google Trends.
Google Trends, downloadable by API using pytrends, limits the time period which can be downloaded using a single request. Each request is scaled between 0 and 100, making comparison between different time periods difficult. This package automatically downloads overlapping trends and rescales them, providing trend data across a long-term period.
pip install longtrends
Requires pytrends, installed automatically with pip
.
from longtrends import LongTrend
from datetime import datetime
keyword = 'suncream'
# Create LongTrend object
longtrend = LongTrend(
keyword=keyword,
start_date=datetime(2018, 1, 1),
end_date=datetime(2022, 3, 31)) # use verbose=True for print output
# Build long-term trends
lt_built = longtrend.build()
# Plot
lt_built.plot(title=f"Google Trends: {longtrend.keyword}", figsize=(15, 3))
First, longtrends downloads overlapping trends.
from longtrends import rescale_overlaps, get_overlapping_trends, rescaled_longtrend
from datetime import datetime
import pandas as pd
keyword = 'suncream'
overlapping = get_overlapping_trends(
keyword=keyword,
start_date=datetime(2018, 1, 1),
end_date=datetime(2022, 3, 31),
verbose=True)
pd.concat(overlapping, axis=1).plot(figsize=(15,3), legend=False)
Next, i+1th overlap is rescaled to ith overlap.
rescaled = rescale_overlaps(overlapping)
pd.concat(rescaled, axis=1).plot(figsize=(15,3), legend=False)
Finally, a single long-term trend is picked.
rescaled = rescaled_longtrend(rescaled)
rescaled.plot(figsize=(15,3), title='Rescaled long-term trend')
This is not an official or supported product. It is provided without warranty under MIT license.