0

I have datetime index

import pandas as pd
sample = ['2021-01-19 15:55:00-05:00',
          '2021-01-19 15:56:00-05:00',
          '2021-01-19 15:56:00-05:00']
sample = pd.to_datetime(sample)

I would like to create new column that represents time measured in the number of milliseconds since midnight.

There is one solution for seconds timeframe Get the time spent since midnight in dataframe but I was not able to convert it to miliseconds.

Mislav Sagovac
  • 185
  • 1
  • 8

1 Answers1

0

try

df = pd.DataFrame(sample, columns=['time'])
df['time'] = pd.to_datetime(df['time'])
df['milliseconds'] = df['time'].apply(lambda x: (x.hour * 3600 + x.minute * 60 + x.second)*1000)

df

output

    time                        milliseconds
0   2021-01-19 15:55:00-05:00   57300000
1   2021-01-19 15:56:00-05:00   57360000
2   2021-01-19 15:56:00-05:00   57360000
Epsi95
  • 8,832
  • 1
  • 16
  • 34