Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

In the previous assignment on time series, the data came on a regular grid: one measure each hour, one day after the other.
Here we study a series where the timestamps are not regular: a catalog of earthquakes in California, where each row is an event that happens whenever it happens.

In this assignment we practice

here is the kind of picture we will end up with

using pandas 3.0.6
Loading...
Loading...

the data

there is one file data/quakes.csv, with one row per earthquake of magnitude 2.5 or more in California and surrounding areas, over one year (from 2025-10-01 to 2026-10-01)

columncontent
timewhen the earthquake happened, in UTC
latitude, longitudewhere
depthin km
magthe magnitude
placea text description, like "20 km ESE of Silver Springs, Nevada"
typethe kind of event: nearly always earthquake

1. loading

  1. load the file into a dataframe df, and look at it; what is the type of the time column ?

  2. turn the time column into timestamps, and use it as the index of df; make sure the index is sorted

  3. the column type is nearly always "earthquake"; keep only these rows
    (how many rows do you get rid of ? and what were they ?)

2. is it regular ?

in the previous assignment we have used asfreq() or date_range() to check that we had one row per hour
here we are in a different situation: the time between 2 consecutive rows is not constant

  1. compute the time that separates 2 consecutive earthquakes as a Series named gaps
    what is the type of the values in it ? (do we get durations ?)

  2. express these gaps in minutes, as floats; what are the shortest, the longest, and the median gaps ?

  3. draw a histogram of the gaps; what do you see ?

3. from events to a regular series

as we have seen, resample() is like a groupby where the groups are the time intervals
it is the tool of choice to go from events to a regular series

  1. compute daily, the number of earthquakes per day, and plot it
    what is the busiest day ?

  2. how many days have no earthquake at all ? is this a missing data issue, like in the previous assignment ?

  3. compute and plot the maximum magnitude per day; how many NaN do you get, and why ?

  4. same question (number of earthquakes) per week, and per month

4. zooming on a burst

in the daily counts, you can see a peak, that corresponds to one big earthquake followed by its aftershocks

  1. what is the date of the strongest earthquake, and what is its magnitude ?

  2. select the rows from 1 hour before this earthquake until 6 hours after it
    how many are they ? and what are the magnitudes of these ?

  3. compute the number of earthquakes per hour in that window; what do you see ?

5. rolling windows

the daily counts are quite noisy; let us use a rolling window to smooth them, as we could do on a regular series
but here a difference is that the rows are not evenly spaced in time, so a window of 7 rows is not a window of 7 days

  1. compute last_7d, a dataframe with 2 columns count and max, which give, for each earthquake, the number of earthquakes and the maximum magnitude over the last 7 days
    hint: rolling() accepts a duration, like "7D", in place of a number of rows

  2. plot the count column; what happens to it after the big earthquake ?

  3. for comparison, what would be the meaning of df.mag.rolling(7).max() ? how long, on average, is the period that it covers ?

6. back to a grid, with reindex() (optional)

sometimes, we need to know the state at some given times - for example, what was the magnitude of the latest earthquake at midnight each day ?
for this we need to build a regular index, and to “look up” in an irregular series

  1. build midnights, the DatetimeIndex with one timestamp per day at midnight (UTC), from the first to the last day of the series
    hint: pd.date_range()

  2. what do you get with df.mag.asfreq("D"), or with df.mag.reindex(midnights) ? how many values are defined ? why ?

  3. use reindex() with the option method="ffill" to get, for each midnight, the magnitude of the latest earthquake before that time

7. the week at a glance (optional)

do earthquakes occur more often at some hours, or some days ?
since we are in California, let us look at the local time rather than UTC

  1. convert the index to the time zone "America/Los_Angeles", and add columns hour and dayofweek to df

  2. build a dataframe with the 24 hours as rows and the 7 days of the week as columns, with the number of earthquakes in each cell
    once with pivot_table, and once with groupby + unstack; check that you get the same

  3. display that as a heatmap, using sns.heatmap() or plt.imshow(); what do you conclude ?

appendix: where the data comes from