liveBook · Manning

Datetime index

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

A pandas index whose labels are datetime values and which supports time-based selection and slicing.

A datetime index gives a pandas DataFrame date labels that support natural time-based selection. Selecting a year, month, or date range returns the matching rows, including the ending date of a slice. section 10.10

Useful operations include:

  • Select one date, month, year, or inclusive date range with .loc. section 10.10
  • Use index.is_quarter_end to mask rows occurring on the final day of each quarter. section 10.10
  • Use resample to aggregate values by periods such as weeks, months, or years; incomplete periods still appear. section 10.10
filename = '../data/wti-daily.csv'

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

Create the index while loading daily oil prices by parsing Date values and assigning them as the DataFrame index. section 10.10

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

How deep it goes
Demonstrates date slicing, quarter-end masking, and period resampling with concrete DataFrame results.
Examples
Uses daily WTI oil prices to find date ranges, quarter-end prices, and annual mean prices.
What it assumes
Assumes familiarity with pandas DataFrames, indexes, selection, and aggregation while showing how dates become an index.
What you can do after
Apply it via pandas to retrieve calendar ranges, identify quarter ends, and calculate period-based averages.
How it connects
Connects datetime indexes with loc selection, boolean masking, datetime attributes, and resampling.

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.