在Python中将数据框日期列转换为时间戳

时间:2020-02-06 23:19:58

标签: python pandas dataframe date timestamp

我有一个dataframe列,用于说明需要转换为timestamp的日期。现在的数据如下:

6 1970/01/01 01:00:12.273928
7 1970/01/01 01:00:12.459902
8 1970/01/01 01:00:12.569948
11 1970/01/01 01:00:14.292992
13 1970/01/01 01:00:14.825224

我尝试了以下

st = pd.to_datetime(df['StartTime'])
timestamp = datetime.timestamp(st)
print("timestamp =", timestamp)  

但收到此错误

TypeError: descriptor 'timestamp' requires a 'datetime.datetime' object but received a 'Series'

有什么办法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

您可以使用Apply。 St是一个序列,因此不能直接传递给timestamp函数。您需要将timestamp函数应用于系列元素。

st.apply(datetime.timestamp)

6     1.580998e+09
7     1.580998e+09
8     1.580998e+09
11    1.580998e+09
13    1.580998e+09
Name: StartTime, dtype: float64