代码之家  ›  专栏  ›  技术社区  ›  1324241421

提取满足日期时间范围内条件的行

  •  0
  • 1324241421  · 技术社区  · 2 年前

    我在下面有一个数据框架,其中有一列特定操作的开始日期和结束日期。 日期都在datetime.date中,我遍历开始和结束日期列,并检查当前日期(也是日期时间类型)是否在该范围内。我在下面发布了我的代码,我尝试同时使用按位运算符和“and”运算符,但它们都会给我错误。

    df_current = [df_new[df_new['Start'] <= currentdate and currentdate <= df_new['End']]]
    

    &错误:类型错误:不支持&:'的操作数类型datetime.datetime”和“datetime.datatime”

    和Error:ValueError:序列的真值不明确。使用a.empty、a.bool()、a.item()、.any()或.all()。

    1 回复  |  直到 2 年前
        1
  •  0
  •   not_speshal    2 年前

    您应该使用 & 而不是 and 。请尝试:

    >>> df_current = df_new[df_new['Start'].le(currentdate) & df_new['End'].ge(currentdate)]