Convert UTC trade times to exchange time, keep a daily trading window with seconds after midnight or between_time, and see how rounding a timestamp can move a trade across the cutoff.
After this lesson, you can keep the trades inside an exchange’s trading window on its own clock.
How do I put UTC trades on the exchange’s clock?
A trade feed often stamps times in UTC, while an exchange’s trading window follows its local clock, so .dt.tz_convert("Europe/Stockholm") shows each trade on Stockholm’s clock and .dt.tz_localize(None) keeps that clock reading without the zone. Two openings near 09:00 local time are near 08:00 and 07:00 in UTC, because Stockholm moved to summer time on 29 March 2026.
The trades are made-up trades of one stock on Nasdaq Stockholm, with times to the nanosecond and prices in kronor. pd.to_datetime(..., utc=True) reads the text as UTC times.
The following box converts the times and prints the two openings.
UTC still identifies each instant once, also when the local clock repeats an hour in the autumn.
How do I keep the trades between 09:01 and 17:24?
The time since local midnight in seconds, compared with the window’s ends, keeps the trades from 09:01, after the opening auction, up to 17:24, before the closing auction starts at 17:25. Five trades pass, among them one at 09:01:00.000000001, which passes by one nanosecond.
.dt.normalize() gives each date’s midnight, so the timestamp minus midnight is the time of day, and .dt.total_seconds() turns it into seconds. 09:01 is \(9 \times 3{,}600 + 60 = 32{,}460\) seconds and 17:24 is 62,640. >= includes the start and < leaves out the end.
The following box keeps the trades inside the window.
On a time index, .between_time("09:01", "17:24", inclusive="left") keeps the same five trades. It compares whole microseconds, so for a cutoff finer than that, the seconds are safer.
Rounding a timestamp moves it, so a trade at 09:00:59.999999999 fails the 09:01 cutoff and passes after .dt.round("ms"), which gives 09:01:00.000. .dt.floor("ms") cuts instead, so two trades at 09:00:59.123456789 and 09:00:59.123789 both become 09:00:59.123 and lose their order.
The following box compares the original and rounded times of three trades.