How do I find the real month-end trading days with an exchange calendar?
Get an exchange’s trading days with exchange_calendars, flag data rows on days it was shut, find the last trading day of each month, and take each month’s closes on that day with reindex.
After this lesson, you can check a price table against an exchange’s trading days and take each stock’s close on the last trading day of the month.
Which weekdays was the exchange open?
cal.sessions_in_range(start, end) lists the days an exchange was open, its sessions, and the weekdays missing from that list are its holidays. Nasdaq Stockholm, calendar code "XSTO", had 40 sessions on the 42 weekdays of January and February 2026 and was shut on New Year’s Day and Epiphany, 6 January.
The package exchange_calendars, version 4.13.4, gives the sessions as dates at midnight. pd.bdate_range lists all weekdays, and .difference() keeps the weekdays that are not sessions.
The following box lists the sessions and the closed weekdays.
import exchange_calendars as xcalsimport numpy as npimport pandas as pdcal = xcals.get_calendar("XSTO")sessions = cal.sessions_in_range("2026-01-01", "2026-02-28")weekdays = pd.bdate_range("2026-01-01", "2026-02-28")print(len(sessions), len(weekdays), weekdays.difference(sessions).strftime("%Y-%m-%d").tolist())
40 42 ['2026-01-01', '2026-01-06']
Which data rows fall outside the sessions?
~panel["date"].isin(sessions) flags every row whose date is not a session, and in the made-up table only CCC’s row on Saturday 31 January fails. The table still has 120 rows, because BBB’s row of 27 February is also absent, so the row count alone misses both errors.
The table holds three made-up stocks’ closes in kronor per share on the 40 sessions, one row per stock and date. Dates stamped in UTC need .dt.tz_convert("Europe/Stockholm") before the date is taken, because 23:00 UTC on 30 January is already 31 January in Stockholm.
date ticker close invalid_date
120 2026-01-31 CCC 151.0 True
120
How do I take each month’s close on its last session?
Grouping the sessions by month and taking each month’s latest session gives the month-end trading days, here Friday 30 January and Friday 27 February, and wide.reindex(month_ends) takes the closes on exactly those days. BBB has no close on 27 February, so its cell is NaN and its February return is missing, while .resample("ME").last() silently takes its close of 189.29 from 26 February.
.to_period("M") labels each session with its month, and .to_numpy() passes the dates without those labels. Real data also need a check against the requested coverage, because a month missing at either end of the table leaves no row to flag.
The following box finds the month-end sessions and compares BBB’s closes from both methods.
month_end resample_last_BBB last_session_BBB
0 2026-01-30 193.90 193.9
1 2026-02-27 189.29 NaN
ticker AAA BBB CCC
date
2026-01-30 NaN NaN NaN
2026-02-27 -0.0077 NaN 0.011
Your turn
Print the last session of January, February and March 2026. You should see 30 January, 27 February and 31 March.