将 datetime64[ns] 索引转换为日期 pandas 进行比较

Convert datetime64[ns] Index to date pandas for comparison

我有以下 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 datetime64[ns] 不直接与 datetime.date 进行比较。您需要转换:

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