【问题标题】:Convert DateTime to TimeStamp Pandas将 DateTime 转换为 TimeStamp Pandas
【发布时间】:2022-01-02 08:58:11
【问题描述】:

这篇文章的目的是能够将列 ['Open Date', 'Close date'] 转换为时间戳格式

我已尝试使用这些链接中的函数/示例并获得任何结果。

Convert datetime to timestamp in Neo4j

Convert datetime pandas

Pandas to_dict() converts datetime to Timestamp

非常感谢有关如何做到这一点的任何想法/cmets/示例。

Data Base Image

列特征:

开放日期 datetime64[ns] 和 pandas.core.series.Series

截止日期 datetime64[ns] 和 pandas.core.series.Series

我终于开始使用这些库了

将熊猫导入为 pd

将 numpy 导入为 np

从日期时间导入日期时间、日期、时间、时间增量

【问题讨论】:

    标签: pandas datetime timestamp


    【解决方案1】:

    您首先按值转换为 numpy 数组,然后转换(转换)为 int64 - 输出以纳秒为单位,这意味着除以 10 ** 9:

    df['open_ts'] = df['Open_Date'].datetime.values.astype(np.int64)
    df['close_ts'] = df['Close_Date'].datetime.values.astype(np.int64)
    

    如果你想避免使用numpy,你也可以试试:

    df['open_ts'] = pd.to_timedelta(df['Open_Date'], unit='ns').dt.total_seconds().astype(int)
    df['close_ts'] = pd.to_timedelta(df['Close_Date'], unit='ns').dt.total_seconds().astype(int)
    

    尝试并在此处报告

    【讨论】:

    • 您好,非常感谢您的帮助,在下一张图片中出现错误(“'Series' object has no attribute 'datetime'”和“dtype datetime64[ns] cannot be convert to timedelta64[ ns]") 我在运行不同的选项时得到的,我可能做错了什么? postimg.cc/gallery/G8djr4z
    • 好的,你必须先用你的系列创建一个 DF:pandas.pydata.org/docs/reference/api/… 然后,你可以使用我给你的代码,这样你就可以转换你需要的列。
    猜你喜欢
    • 2020-12-15
    • 1970-01-01
    • 2014-05-14
    • 2015-01-02
    相关资源
    最近更新 更多