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.

import pandas as pd

trades = pd.DataFrame({"ticker": ["BBB", "AAA", "AAA", "CCC"], "text": ["04/03/2026", "30/01/2026", "31/03/2026", "31/02/2026"]})
trades["date"] = pd.to_datetime(trades["text"], format="%d/%m/%Y", errors="coerce")
print(trades)
print(pd.to_datetime(1772582400000, unit="ms", utc=True))
  ticker        text       date
0    BBB  04/03/2026 2026-03-04
1    AAA  30/01/2026 2026-01-30
2    AAA  31/03/2026 2026-03-31
3    CCC  31/02/2026        NaT
2026-03-04 00:00:00+00:00

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.

dates = trades["date"]
print(pd.DataFrame({"date": dates, "month": dates.dt.month, "quarter": dates.dt.quarter, "weekday": dates.dt.day_name(), "month_end": dates.dt.is_month_end}))
        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.

start = pd.Timestamp("2026-01-30")
for freq in ["D", "B", "W-FRI", "ME", "BME"]:
    print(freq, pd.date_range(start, periods=2, freq=freq).strftime("%Y-%m-%d").tolist())
print((start + pd.DateOffset(months=1)).date(), (start + pd.Timedelta(days=30)).date(), (start + pd.offsets.BDay(1)).date())
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.

print(trades.loc[dates.between("2026-03-01", "2026-03-31"), ["ticker", "date"]].sort_values("date"))
  ticker       date
0    BBB 2026-03-04
2    AAA 2026-03-31

Your turn

Print the ticker and date of the trades from 30 January to 4 March 2026 in date order. You should see AAA first and BBB second.

.sort_values("date") puts AAA’s January trade before BBB’s March trade.

print(trades.loc[dates.between("2026-01-30", "2026-03-04"), ["ticker", "date"]].sort_values("date"))
  ticker       date
1    AAA 2026-01-30
0    BBB 2026-03-04