NP.不适用于模糊的日期时间参数吗?

发布于 2025-02-03 08:14:18 字数 923 浏览 2 评论 0原文

我有一个DF,试图通过NP来填充新列。如果“ sales_datetime”等于昨天,请在昨天的日期中寻找昨天的日期。如果不是这样,我只希望它显示0。

我放入了几个DT公式,它们正常工作,我的NP。在哪里工作,但即使“ sales_datetime”等于昨天,它也显示为所有零零。

将我的NP更改为特定日期可以产生我想要的结果,但我希望这能够每天迭代,而无需手动更改应检查的日期。

任何帮助将不胜感激!

  1. 设置昨天的值:

      today = dt.today()。date()
     昨天=今天-TimeDelta(天= 1)
     印刷(今天)
     印刷(昨天)
    
     2022-06-01
    
     2022-05-31
     
  2. np.Where语句

      df ['new_col'] = np.Where(df ['sale_datetime'] ==昨天,df ['total_count'],0)
     
  3. 当前结果

      sale_dateTime item1 item2 total_count new_col
     2022-05-27 3 9 12 0
     2022-05-31 0 7 7 0
     2022-05-05 6 8 14 0
     2022-05-31 5 5 10 0
     2022-05-25 0 16 16 0
     
  4. 所需/预期结果

      sale_dateTime item1 item2 total_count new_col
     2022-05-27 3 9 12 0
     2022-05-31 0 7 7 7
     2022-05-05 6 8 14 0
     2022-05-31 5 5 10 10
     2022-05-25 0 16 16 0
     

I have a df where I am trying to populate a new column by having np.where look for yesterday's date, then assign the value of "total_count" to new_col if the "sales_datetime" equals yesterday. If it doesn't, I just want it to show 0.

I put in a couple of dt formulas that are working properly, my np.where works BUT it is showing new_col as all zeros, even when "sales_datetime" is equal to yesterday.

Changing my np.where to be a specific date yields the result I'm looking for, but I'd like this to be able to iterate daily without having to manually change the date that should be checked against.

Any help would be greatly appreciated!

  1. Setting up yesterday value:

     today = dt.today().date()
     yesterday = today - timedelta(days=1)
     print(today)
     print(yesterday)
    
     2022-06-01
    
     2022-05-31
    
  2. np.where statement

    df['new_col'] = np.where(df['sale_datetime'] == yesterday, df['total_count'], 0)
    
  3. Current result

     sale_datetime   item1   item2   total_count new_col
     2022-05-27        3       9          12        0
     2022-05-31        0       7           7        0
     2022-05-05        6       8          14        0
     2022-05-31        5       5          10        0
     2022-05-25        0      16          16        0
    
  4. Desired/expected result

     sale_datetime   item1   item2   total_count new_col
     2022-05-27        3       9          12        0
     2022-05-31        0       7           7        7
     2022-05-05        6       8          14        0
     2022-05-31        5       5          10       10
     2022-05-25        0      16          16        0
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文