【问题标题】:convert pandas Series of dtype <- 'datetime64' into dtype <- 'np.int' without iterating将 pandas 系列的 dtype <- 'datetime64' 转换为 dtype <- 'np.int' 而无需迭代
【发布时间】:2020-11-25 17:22:45
【问题描述】:

考虑以下示例pandas DataFrame

df = pd.DataFrame({'date':[pd.to_datetime('2016-08-11 14:09:57.00'),pd.to_datetime('2016-08-11 15:09:57.00'),pd.to_datetime('2016-08-11 16:09:57.8700')]})

我可以将单个实例转换为np.int64 类型

print(df.date[0].value)
1470924597000000000

或将整个列 iteratively 转换为

df.date.apply(lambda x: x.value)

我如何不使用 iteration 实现这一目标?像

df.date.value

我还想在不使用迭代的情况下将np.int64 对象转换回pd.Timestamp 对象。我从 herehere 发布的解决方案中获得了一些见解,但这并不能解决我的问题。

【问题讨论】:

标签: python-3.x pandas numpy datetime epoch


【解决方案1】:

根据上面的@anky cmets,解决方案很简单。

df.date.astype('int64') >> to int64 dtype
pd.to_datetime(df.date) >> from int dtype to datetime64 dtype

【讨论】:

    猜你喜欢
    • 2016-11-25
    • 2022-08-15
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 2014-02-12
    相关资源
    最近更新 更多