Pytrends - Interest over time - return column with None when there is no data

889 Views Asked by At

Pytrends for Google Trends data does not return a column if there is no data for a search parameter on a specific region.

The code below is from pytrends.request

def interest_over_time(self):
        """Request data from Google's Interest Over Time section and return a dataframe"""

        over_time_payload = {
            # convert to string as requests will mangle
            'req': json.dumps(self.interest_over_time_widget['request']),
            'token': self.interest_over_time_widget['token'],
            'tz': self.tz
        }

        # make the request and parse the returned json
        req_json = self._get_data(
            url=TrendReq.INTEREST_OVER_TIME_URL,
            method=TrendReq.GET_METHOD,
            trim_chars=5,
            params=over_time_payload,
        )

        df = pd.DataFrame(req_json['default']['timelineData'])
        if (df.empty):
            return df

        df['date'] = pd.to_datetime(df['time'].astype(dtype='float64'),
                                    unit='s')
        df = df.set_index(['date']).sort_index()

From the code above, if there is no data, it just returns df, which will be empty.

My question is, how can I make it return a column with "No data" on every line and the search term as header, so that I can clearly see for which search terms there is no data?

Thank you.

1

There are 1 best solutions below

0
On

I hit this problem, then I hit this web page. My solution was to ask Google trends for data on a search item it would have data for, then rename the column and 0 the data.

I used the ".drop" method to get rid of the "isPartial" column and the ".rename" method to change the column name. To zero the data in the column, I did the following, I created a function:

#Make value zero
def MakeZero(x):
    return x *0

Then using the ".apply" method on the dataframe to 0 the column.

        ThisYrRslt=BlankResult.apply(MakeZero)

: ) But the question is, what search term do you ask google trends about that will always return a value? I chose "Google". : )

I'm sure you can think of some better ones, but it's hard to leave those words in commercial code.