【问题标题】:How do I convert a column to Pandas Timestamps?如何将列转换为 Pandas 时间戳?
【发布时间】:2023-01-25 00:29:07
【问题描述】:

我的 DataFrame 中有一列的值类似于 '2022-06-03T00:00:00.000Z',我想将它们(就地)转换为 pd.Timestamp。我看到很多关于如何转换为 np.datetime64 以及如何转换 DataFrames 的任意列的答案,但无法弄清楚如何将这些应用于覆盖 pd.Timestamp

【问题讨论】:

    标签: python pandas dataframe timestamp type-conversion


    【解决方案1】:

    使用 from pd.to_datetime 方法 我认为这可以解决您的问题 只需要在方法中激活 utc 参数

    
    import pandas as pd
     
    lst = {'a':['Geeks', 'For'],'b':['2022-06-03T00:00:00.000Z','2024-03-03T00:00:00.000Z']}
     
    df = pd.DataFrame(lst)
    
    df['b']=pd.to_datetime(df['b'],utc=True)
    
    type(df['b'][0])
    
    
    pandas._libs.tslibs.timestamps.Timestamp
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加更多详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写出好的答案的信息in the help center
    • 我编辑我的答案。我希望它对你有用
    • 实际上它看起来不需要utc=True
    • 我认为您的数据采用时区格式。对不起
    • 我也是这么想的,不过肯定是pandas._libs.tslibs.timestamps.Timestamp。可能是由于原始字符串值的格式(ISO8601 显然带有时区信息),例如 '2019-05-03T18:18:13.683Z'
    猜你喜欢
    • 2018-10-09
    • 2016-10-05
    • 1970-01-01
    • 2019-11-29
    • 2022-11-21
    • 2014-12-17
    • 1970-01-01
    • 2021-01-18
    • 2014-07-13
    相关资源
    最近更新 更多