Search code examples
pythonpandastimestampmilliseconds

pandas difference between datetime64[ns] in milliseconds


How to get the difference in milliseconds of these two columns:

import numpy as np
from datetime import datetime
import pandas as pd


def datetime_generator():
    day = np.random.randint(1, 7 + 1)
    hour = np.random.randint(0, 23 + 1)
    minute = np.random.randint(0, 59 + 1)
    second = np.random.randint(0, 59 + 1)
    millisecond = np.random.randint(0, 999999 + 1)

    return datetime(2018, 1, day, hour, minute, second, millisecond)

df = pd.DataFrame({'ts1': [datetime_generator() for i in range(10)],
                         'ts2': [datetime_generator() for i in range(10)]})

Solution

  • Edit: Get the timedeltas, then read & convert their microseconds.

    df["new"] = df.ts1 - df.ts2
    df["new"] = df.new.apply(lambda x: x.microseconds/1000)
    

    This old answer only gets the microseconds

    .dt.microsecond is the way

    df["new"] = df.ts1.dt.microsecond - df.ts2.dt.microsecond
    

    Note the conversion ratio between micro and mili seconds is 1000:1 so...

    df["new"] = (df.ts1.dt.microsecond - df.ts2.dt.microsecond)/1000
    

    There is no .dt.milisecond equivalent as far as I can tell.