Time Series Data
Pandas Basics
3 min read
Published Sep 29 2025, updated Oct 24 2025
Guide Sections
Guide Comments
What is Time Series Data?
- A sequence of data points indexed (or labeled) by time.
- Examples:
- Daily stock prices
- Hourly temperature readings
- Monthly sales numbers
- In Pandas, time series data is usually represented with a DatetimeIndex.
Creating DatetimeIndex
pd.date_range() creates a sequence of datetime values to use as an index.
Example:
Output:
It generates 24 hourly timestamps starting at midnight on Oct 27, 2019. So, it’s basically one full day, hour by hour.
Parameters Used:
start='27/10/2019 00:00:00'→ the first datetime in the range.periods=24→ number of timestamps to generate.freq='h'→ frequency = hourly.
Other Common Parameters:
end→ specify an end date instead ofperiods.freq→ frequency of steps. Examples:'B'— Business day frequency'D'— Calendar day frequency'h'— Hourly frequency'min'— Minutely frequency's'— Secondly frequency'ms'— Millisecond frequency'us'— Microsecond frequency'ns'— Nanosecond frequency'ME'— Month end frequency'MS'— Month start frequency'QE'— Quarter end frequency'QS'— Quarter start frequency'YE'— Year end frequency'YS'— Year start frequency'bh'— Business hour frequency'2D'— Every 2 days'3W'— Every 3 weeks'1H30T'— Every 1 hour and 30 minutes
tz→ set a timezone.
Creating DataFrame example:
Output:
You can then access the different parts such as hour, year etc of the index. Here we add them as extra columns:
Output:
Accessing date parts from non-indexed columns:
Creates a table with a date range that is just a standard column.
Output:
Access the date parts of the column:
Add .dt before the date information when indicating the datetime data is a column.
Output:
Converting to datetime
- Converts strings to datetime objects.
- Benefits: allows arithmetic, slicing, and resampling.
Setting datetime as index:
Output:
Now df.index is a DatetimeIndex.
Accessing Time Series Data
By specific date:
By date range:
By year, month, or day (via string slicing):
Frequency and Resampling
Pandas supports regular time intervals (daily, monthly, yearly, etc.).
Common frequency strings:
- Time-based
"s"→ seconds"min"→ minutes"h"→ hours"D"→ calendar day"B"→ business day
- Week-based
"W"→ weekly (default Sunday end)"W-MON"→ weekly anchored to Monday
- Month/Quarter/Year
"ME"→ month end"MS"→ month start"QE"→ quarter end"QS"→ quarter start"YE"→ year end"YS"→ year start
- Custom
"2h"→ every 2 hours"15min"→ every 15 minutes"7D"→ every 7 days
Shifting and Lagging
Shift data forward/backward in time:
Useful for calculating differences or returns:
Rolling and Moving Windows
Compute statistics over a rolling window:
Time Series Arithmetic
Pandas allows operations on dates:
Supports resampling, shifting, and alignment.














