You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法提取DataFrame唯一日期?df.timestamp.dt.date.unique()失效求助

解决方法

问题核心是你的timestamp列并非datetime64类型,dt属性仅对datetime类型的Series可用,按以下步骤处理即可:

  • 先确认列类型(验证问题)

    print(df['timestamp'].dtype)
    

    若输出为object(即字符串类型),就需要先转换数据类型。

  • 将字符串列转为datetime类型

    import pandas as pd
    df['timestamp'] = pd.to_datetime(df['timestamp'])
    

    若你的时间格式是非标准格式,可手动指定格式参数:

    df['timestamp'] = pd.to_datetime(df['timestamp'], format='%Y-%m-%d %H:%M:%S')
    
  • 提取唯一日期

    unique_dates = df['timestamp'].dt.date.unique()
    

    如需排序后的结果:

    sorted_unique_dates = sorted(unique_dates)
    

内容的提问来源于stack exchange,提问作者David

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 19:39:23