我有日期时间索引
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)
我想创建一个新列,表示以自午夜以来的毫秒数来衡量的时间。
秒时间范围 Get the time spent since midnight in dataframe 有一种解决方案,但我无法将其转换为毫秒。
答案 0 :(得分:0)
试试
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
输出
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