liveBook · Manning

Time series

As explained in Pandas Workout: 200 exercises to make you a stronger data analyst

A time series is a pandas data structure whose index contains timestamp values ordered or organized by time.

A time series is a pandas DataFrame whose timestamp column becomes the index. This arrangement turns dates into a practical way to select, slice, group, and summarize observations. section 10.8 section 10.17

filename = '../data/wti-daily.csv'

df = pd.read_csv(filename,
                parse_dates=['Date'],
                index_col=['Date'])

Parse the Date column while loading the CSV, then make it the index so date-aware selection and aggregation are immediately available. section 10.10

Querying historical oil prices

With a DateTimeIndex, df.loc['1992-06-15'] selects one date, df.loc['1992-06'] selects every row in June, and df.loc['1987'] selects the entire year. Applying mean to the selected Price values produces period-specific averages. A date slice such as df.loc['2003-09':'2014-07'] includes both endpoints. section 10.10

Useful time-series operations include:

  • Select rows by complete or partial dates with loc. section 10.8
  • Slice an interval by specifying starting and ending dates. section 10.8
  • Resample observations into periods such as days, months, years, or six-month intervals, then apply aggregations. section 10.8
  • Combine resampling with sorting and dropna to find unusually high daily or monthly averages. section 10.14

Comparing tipping rates

After setting pickup time as the index, resample the tip_percentage series by day, sort the resulting means, and inspect the highest-tipping dates. Restricting the data to January and July before resampling avoids unrelated months and supports a direct seasonal comparison. section 10.14

The transferable pattern is simple: convert timestamps, place them in the index, select the relevant period, resample when needed, and aggregate or inspect the result. section 10.17

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

How deep it goes
Explains construction, partial-date indexing, inclusive slicing, and resampling, without descending into datetime implementation internals.
Examples
Uses historical oil prices, Apollo mission dates, and New York taxi tipping percentages as practical datasets.
What it assumes
Assumes familiarity with pandas DataFrames, columns, indexes, loc, aggregation, and CSV loading while demonstrating datetime conversion.
What you can do after
Apply pandas date indexing and resampling to calculate period-specific statistics and compare observations across time.
How it connects
Connects pandas DataFrame, datetime, DatetimeIndex, indexing, aggregation, groupby, and resampling.

Also explained in

Compare how 9 books explain time series →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.