Which date commands will I meet in finance scripts?
Read trade dates from text or epoch numbers, read their calendar fields, build daily, weekly and month-end schedules, add months, days and weekdays, and select the trades between two dates.
After this lesson, you can read the dates of a trade log, build date schedules and select the trades in a reporting window.
How do I read dates from text and from numbers?
pd.to_datetime(text, format="%d/%m/%Y", errors="coerce") reads dates written day first, with %d for the day, %m for the month and %Y for the year, and gives NaT for an impossible date such as 31 February. A feed that counts seconds or milliseconds since 1 January 1970 needs unit="s" or unit="ms", and utc=True labels the result as UTC time.
The trades are a made-up log of four trades with dates as day-first text.
The following box reads the text dates and one epoch count in milliseconds.
The count of milliseconds is midnight UTC at the start of 4 March 2026.
How do I read the calendar fields of a date?
.dt gives a column of dates its calendar fields, such as .dt.month, .dt.quarter, .dt.day_name() and .dt.is_month_end. AAA’s 31 March trade is in quarter 1, on a Tuesday and on the last day of its month, while 30 January, a Friday, is not the month’s last day.
A single pd.Timestamp has the same fields without .dt, such as pd.Timestamp("2026-03-31").quarter.
The following box prints the fields of the trade dates.
date month quarter weekday month_end
0 2026-03-04 3.0 1.0 Wednesday False
1 2026-01-30 1.0 1.0 Friday False
2 2026-03-31 3.0 1.0 Tuesday True
3 NaT NaN NaN NaN False
How do I build schedules and move dates?
pd.date_range(start, periods=2, freq=...) builds a schedule whose dates the frequency picks. "D" takes every day, "B" every weekday, "W-FRI" every Friday, "ME" each month’s last day and "BME" each month’s last weekday. Moving a date has three tools. pd.DateOffset(months=1) moves by a calendar month, pd.Timedelta(days=30) by a fixed duration and pd.offsets.BDay(1) by one weekday, so from 30 January they reach 28 February, 1 March and 2 February.
Weekday rules can land on an exchange holiday, which the lesson on the trading calendar handles.
The following box builds five schedules from 30 January and moves the date three ways.
D ['2026-01-30', '2026-01-31']
B ['2026-01-30', '2026-02-02']
W-FRI ['2026-01-30', '2026-02-06']
ME ['2026-01-31', '2026-02-28']
BME ['2026-01-30', '2026-02-27']
2026-02-28 2026-03-01 2026-02-02
The last day of January 2026 is a Saturday, so "ME" and "BME" differ.
How do I select the trades in a window?
.between(start, end) tests whether each date lies in the window, both ends included, and gives False for a missing date. Fixed dates make the report the same every time it runs, while pd.Timestamp.today() would change it every day. The March window holds the trades of 4 and 31 March.
The following box selects the March trades in date order.