Pandas · Compare with the previous period
Google Search Analytics
Course overviewGoogle Search · Analyze activity over time

Compare with the previous period

Align yesterday's count and compute changes without inventing growth from a zero baseline.

Step 1 of 3 · Learn

Align the previous calendar day

Sort by date before .shift(1). Shift uses the previous row, not a calendar lookup. On a complete daily calendar, that row is yesterday; without the calendar, it could be several days earlier. The first row has no previous value.

indexdaysearches
0Mon10
1Tue0
2Wed15
indexdaysearchespreviouschange
0Mon10NANA
1Tue010-10
2Wed15015
Shift aligns the previous count; diff subtracts it from the current count.

Keep zero-baseline growth missing

.pct_change(fill_method=None) returns fractional change, not a percentage: 0.5 means 50% growth. A zero previous count has no defined relative growth here. Use .where(previous > 0) to leave those rows and the first row missing; a drop from a positive count to zero is -1.

indexsearches
010
10
215
330
indexsearchesgrowth
010NA
10-1
215NA
3301
Growth from zero stays missing; a fall from 10 to 0 is -100%.

Do not invent earlier activity

Retain every date in the reporting calendar. Do not fill the first prior count with 0 or forward-fill gaps to create a growth rate. For this report, an absent event date has a known count of zero, while the day before the report is unavailable.

▷ Your turn

From daily_searches, return date, searches, and previous_searches in ascending date order. previous_searches is the preceding calendar day's count within this report. Leave it missing on the first date. Preserve UTC timestamps and save the DataFrame as result.

daily_searches contains date and searches for every UTC date from July 6 through August 30, 2026, with 0 on dates without searches. It is built from searches inside that interval; earlier dates are not available for the first comparison or rolling window.

LANGUAGEPython · Pandas

Loading Python and Pandas…

Run the code to see DataFrame results here.