【问题标题】:Converting dataframe date column to timestamp in Python在Python中将数据框日期列转换为时间戳
【发布时间】:2020-05-23 02:11:53
【问题描述】:

我有一个 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'

知道如何解决这个问题吗?

【问题讨论】:

标签: python pandas dataframe date timestamp


【解决方案1】:

您可以使用应用。 St 是一个系列,因此不能直接传递给时间戳函数。您需要将时间戳功能应用于系列的元素。

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

【讨论】:

  • 列类型为object,但数据为strings,因此无法正常工作。
  • 你试过了吗?您不是已经使用 pd.to_datetime 将字符串转换为日期时间了吗?
猜你喜欢
  • 2018-02-09
  • 1970-01-01
  • 2016-02-10
  • 1970-01-01
  • 2022-11-30
  • 2019-02-03
  • 1970-01-01
  • 2019-02-16
  • 1970-01-01
相关资源
最近更新 更多