How do I filter trades by time of day in pandas?

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.

import pandas as pd

trades = pd.DataFrame({
    "utc_time": pd.to_datetime([
        "2026-03-27 08:00:02.417000000", "2026-03-27 08:00:59.123456789", "2026-03-27 08:00:59.123789000",
        "2026-03-27 08:00:59.999999999", "2026-03-27 08:01:00.000000001", "2026-03-27 11:30:15.500000000",
        "2026-03-27 16:23:59.999000000", "2026-03-27 16:24:00.000000000", "2026-03-27 16:29:47.103000000",
        "2026-03-30 07:00:03.882000000", "2026-03-30 07:01:00.000000000", "2026-03-30 09:45:10.250000000",
    ], utc=True),
    "price": [182.40, 182.55, 182.50, 182.50, 182.60, 183.10, 182.95, 183.00, 183.20, 184.00, 184.25, 183.90],
})
trades["local_time"] = trades["utc_time"].dt.tz_convert("Europe/Stockholm").dt.tz_localize(None)
print(trades.loc[[0, 9], ["utc_time", "local_time"]])
                          utc_time              local_time
0 2026-03-27 08:00:02.417000+00:00 2026-03-27 09:00:02.417
9 2026-03-30 07:00:03.882000+00:00 2026-03-30 09:00:03.882

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.

trades["seconds"] = (trades["local_time"] - trades["local_time"].dt.normalize()).dt.total_seconds()
inside = (trades["seconds"] >= 9 * 3600 + 60) & (trades["seconds"] < 17 * 3600 + 24 * 60)
print(trades.loc[inside, ["local_time", "price"]])
                      local_time   price
4  2026-03-27 09:01:00.000000001  182.60
5  2026-03-27 12:30:15.500000000  183.10
6  2026-03-27 17:23:59.999000000  182.95
10 2026-03-30 09:01:00.000000000  184.25
11 2026-03-30 11:45:10.250000000  183.90

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.

window = trades.set_index("local_time").between_time("09:01", "17:24", inclusive="left")
print(window["price"].tolist() == trades.loc[inside, "price"].tolist())
True

How can rounding move a trade across the cutoff?

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.

ticks = trades.loc[1:3, "local_time"]
print(pd.DataFrame({"original": ticks, "floor_ms": ticks.dt.floor("ms"), "round_ms": ticks.dt.round("ms")}))
                       original                floor_ms  \
1 2026-03-27 09:00:59.123456789 2026-03-27 09:00:59.123   
2 2026-03-27 09:00:59.123789000 2026-03-27 09:00:59.123   
3 2026-03-27 09:00:59.999999999 2026-03-27 09:00:59.999   

                 round_ms  
1 2026-03-27 09:00:59.123  
2 2026-03-27 09:00:59.124  
3 2026-03-27 09:01:00.000  

Filters and sorting belong on the original timestamps, and a rounded copy suits grouping.

Your turn

Count the trades from 09:00 up to 09:01 on each date. You should see 4 on 27 March and 1 on 30 March.

.groupby(early.index.date).size() counts the trades of each date.

early = trades.set_index("local_time").between_time("09:00", "09:01", inclusive="left")
print(early.groupby(early.index.date).size())
2026-03-27    4
2026-03-30    1
dtype: int64