mikedbjones / longtrends

Package to download long-term Google Trends
https://pypi.org/project/longtrends
MIT License
14 stars 1 forks source link

KeyError: "None of ['date'] are in the columns" #2

Closed asdsadsaasd12312132 closed 11 months ago

asdsadsaasd12312132 commented 12 months ago

I used the sample code provided and get this error.

Traceback (most recent call last):
  File "test.py", line 12, in <module>
    lt_built = longtrend.build()
  File "C:\Program Files\Python37\lib\site-packages\longtrends\__init__.py", line 283, in build
    ro = rescale_overlaps(ot)
  File "C:\Program Files\Python37\lib\site-packages\longtrends\__init__.py", line 265, in rescale_overlaps
    es = ext_scale(trends_list[i+1], trends_list_scaled[i])
  File "C:\Program Files\Python37\lib\site-packages\longtrends\__init__.py", line 245, in ext_scale
    raise ValueError('unable to scale: to_scale has range 0 in overlap with scale_by; this may be because of an extreme spike in trend data')
ValueError: unable to scale: to_scale has range 0 in overlap with scale_by; this may be because of an extreme spike in trend data

When I change the keyword, I get this error.

None found
Traceback (most recent call last):
  File "test.py", line 12, in <module>
    lt_built = longtrend.build() # use verbose=True for print output
  File "C:\Program Files\Python37\lib\site-packages\longtrends\__init__.py", line 284, in build
    rl = rescaled_longtrend(ro)
  File "C:\Program Files\Python37\lib\site-packages\longtrends\__init__.py", line 271, in rescaled_longtrend
    longtrend = pd.DataFrame(pd.concat(trends_list_scaled)).reset_index().drop_duplicates(subset='date').set_index('date').squeeze()
  File "C:\Program Files\Python37\lib\site-packages\pandas\core\frame.py", line 4727, in set_index
    raise KeyError(f"None of {missing} are in the columns")
KeyError: "None of ['date'] are in the columns"
asdsadsaasd12312132 commented 12 months ago

This might be an issue with pytrends.

https://github.com/GeneralMills/pytrends/issues/587

mikedbjones commented 12 months ago

Hi, thanks for the issue. Please can you copy your exact code here, and second keyword? I'll see if I can reproduce. In the meantime, I just ran this with no issue:

from datetime import datetime
import matplotlib.pyplot as plt
from longtrends import rescale_overlaps, get_overlapping_trends, rescaled_longtrend
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)

rescaled = rescale_overlaps(overlapping)
pd.concat(rescaled, axis=1).plot(figsize=(15,3), legend=False)

rescaled = rescaled_longtrend(rescaled)
rescaled.plot(figsize=(15,3), title='Rescaled long-term trend')

plt.show()

Thanks for the info on issues with pytrends. Possibly the results depend on where you are geographically. If you give me your code I'll try and run it here in the UK.

asdsadsaasd12312132 commented 12 months ago

I tried that code and it works, but I have an issue with many 0 values. Do you have this problem too? My location is Australia.

image

The code I was trying before stopped giving errors but no chart appears after successfully running it.

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))
ShannonWavelet commented 11 months ago

Hello there,

I am experiencing the same problem. My location is South Africa.

mikedbjones commented 11 months ago

Thanks both, this seems to be an ongoing underlying issue with pytrends. Closing for now, since the issue is not with longtrends.

@asdsadsaasd12312132 , regarding this:

The code I was trying before stopped giving errors but no chart appears after successfully running it.

Make sure you pip install matplotlib and import matplotlib.pyplot as plt