How do I convert timestamps between seconds, milliseconds, microseconds and nanoseconds?

Change a timestamp’s resolution with as_unit, read integer timestamps in four units and turn timestamps back into integers, and measure the gaps between trades that share a millisecond.

After this lesson, you can read a timestamp in whatever unit a feed sends and measure gaps between trades in the same millisecond.

How do I change a timestamp’s resolution?

A second holds 1,000 milliseconds, 1,000,000 microseconds or 1,000,000,000 nanoseconds, which pandas writes as ms, us and ns, and .dt.as_unit("ms") keeps a timestamp to the millisecond and drops the finer digits for good. Trade B at 09:01:00.123456789 becomes 09:01:00.123 in milliseconds and 09:01:00.123456 in microseconds.

The trades are four made-up trades of one stock, A to D in the order the feed sent them, with UTC times to the nanosecond. B arrived after A but has the earlier time.

The following box builds the trades and prints B at four resolutions.

import pandas as pd

trades = pd.DataFrame({
    "trade": ["A", "B", "C", "D"],
    "time": pd.to_datetime(["2026-03-02 09:01:00.123706789", "2026-03-02 09:01:00.123456789", "2026-03-02 09:01:00.123706789", "2026-03-02 09:01:00.125206789"]),
})
print(trades)
for unit in ["s", "ms", "us", "ns"]:
    print(unit, trades["time"].dt.as_unit(unit).iloc[1])
  trade                          time
0     A 2026-03-02 09:01:00.123706789
1     B 2026-03-02 09:01:00.123456789
2     C 2026-03-02 09:01:00.123706789
3     D 2026-03-02 09:01:00.125206789
s 2026-03-02 09:01:00
ms 2026-03-02 09:01:00.123000
us 2026-03-02 09:01:00.123456
ns 2026-03-02 09:01:00.123456789

Converting a millisecond timestamp back to nanoseconds cannot bring back the dropped digits.

How do I read and write integer timestamps?

A feed can send a timestamp as an integer count since 1 January 1970 at midnight UTC, and pd.to_datetime(count, unit="ms") reads it, as long as the unit matches the feed. The other way round, .dt.as_unit("ns").astype("int64") turns timestamps into nanosecond counts, and // 10**6 turns those into whole milliseconds, which makes A and B equal.

10**6 is a million, the nanoseconds in a millisecond, and // divides and drops the remainder.

The following box reads B from a millisecond count and turns all four times into counts.

print(pd.to_datetime(1772442060123, unit="ms"), pd.to_datetime(1772442060123456789, unit="ns"))
trades["epoch_ns"] = trades["time"].dt.as_unit("ns").astype("int64")
trades["epoch_ms"] = trades["epoch_ns"] // 10**6
print(trades[["trade", "epoch_ns", "epoch_ms"]])
2026-03-02 09:01:00.123000 2026-03-02 09:01:00.123456789
  trade             epoch_ns       epoch_ms
0     A  1772442060123706789  1772442060123
1     B  1772442060123456789  1772442060123
2     C  1772442060123706789  1772442060123
3     D  1772442060125206789  1772442060125

How do I measure gaps when trades share a time?

Sorted by time, .diff() gives each trade’s gap to the one before, and dividing by pd.Timedelta(1, "ms") turns it into milliseconds. A follows B by 0.25 milliseconds, and C, with A’s exact time, follows A by zero, so the times alone cannot tell which of the two traded first.

kind="stable" keeps the feed’s order for equal times, so A stays before C. The first trade has no gap.

The following box sorts the trades and computes the gaps.

ordered = trades.sort_values("time", kind="stable")
ordered["gap_ms"] = ordered["time"].diff() / pd.Timedelta(1, "ms")
print(ordered[["trade", "time", "gap_ms"]])
  trade                          time  gap_ms
1     B 2026-03-02 09:01:00.123456789     NaN
0     A 2026-03-02 09:01:00.123706789    0.25
2     C 2026-03-02 09:01:00.123706789    0.00
3     D 2026-03-02 09:01:00.125206789    1.50

Gaps belong on the full timestamps, because rounding to milliseconds would set A, B and C to the same time.

Your turn

Print the trades in the same whole millisecond as B. You should see B, A and C.

The millisecond counts compare the trades at millisecond resolution.

print(ordered.loc[ordered["epoch_ms"] == ordered.loc[1, "epoch_ms"], ["trade", "time"]])
  trade                          time
1     B 2026-03-02 09:01:00.123456789
0     A 2026-03-02 09:01:00.123706789
2     C 2026-03-02 09:01:00.123706789