【问题标题】:Convert datetime64[ns] Index to date pandas for comparison将 datetime64[ns] 索引转换为日期 pandas 以进行比较
【发布时间】:2023-03-09 22:10:01
【问题描述】:

我有以下 python 代码:

current_ts = datetime.datetime.now()
current_date = current_ts.date()

new_df = df[df.index >= current_date]

df.index 是 datetime64[ns],当我运行代码时,我得到 Invalid comparison between dtype=datetime64[ns] and date

如何将索引转换为日期以便进行比较?

【问题讨论】:

    标签: pandas datetime datetime-format python-datetime


    【解决方案1】:

    Pandas datetime64[ns] 不能直接与 datetime.date 进行比较。你需要转换:

    df[df.index >= pd.Timestamp(current_date)]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-07
      • 2020-11-05
      • 2020-08-20
      • 2021-10-01
      • 2019-01-20
      • 2020-04-28
      • 2020-09-04
      相关资源
      最近更新 更多