如何找到一周的开始和结束(Python)

huangapple go评论41阅读模式
英文:

How to find the beginning and start of a week (Python)

问题

我正在尝试创建一个程序,该程序将获取今天的日期并查找最近的一周的历史开始和结束日期(不包括银行假日和周末)。想法是,它将获取今天的日期"02/26/23",并返回开始日期02/21/23(02/20/23是银行假日),以及本周的结束日期02/24/23。

我的当前代码如下:

import pandas as pd
import datetime as dt
from pandas.tseries.holiday import USFederalHolidayCalendar

end = dt.datetime.now()
start = dt.datetime(end.year, end.month, end.day-5)

dr = pd.date_range(start=start, end=end)
cal = USFederalHolidayCalendar()
holidays = cal.holidays(start=dr.min(), end=dr.max())
A = dr[~dr.isin(holidays)]
B = A[A.weekday != 5]
B = B[B.weekday != 6]

for year in set(B.year):
    tmp = B[B.year == year]
    for week in set(tmp.week):
        temp = tmp[tmp.week == week]
        print(temp[temp.weekday == temp.weekday.min()])  # 本周开始日期
        print(temp[temp.weekday == temp.weekday.max()])  # 本周结束日期

这将产生以下结果:

DatetimeIndex(['2023-02-21'], dtype='datetime64[ns]', freq=None)
DatetimeIndex(['2023-02-24'], dtype='datetime64[ns]', freq=None)

但也会出现"FutureWarning: weekofyear and week have been deprecated..."。有没有办法以datetime格式获取这些日期,以便如果打印出来,它会显示例如"2023-02-21"。如何避免这个FutureWarning警告?

(顺便说一下,我从https://stackoverflow.com/a/43674721/21292653获取了部分代码。)

英文:

I am trying to create a program that will take today's date and find the most recent historical beginning and end of the week (excluding bank holidays and weekends). The idea being is that it will take today's date "02/26/23" and return 02/21/23 for the beginning (02/20/23 was a bank holiday) and 02/24/23 for the end of the week.

My current code is

import pandas as pd
import datetime as dt
from pandas.tseries.holiday import USFederalHolidayCalendar

end = dt.datetime.now()
start = dt.datetime(end.year, end.month, end.day-5)

dr = pd.date_range(start=start, end=end)
cal = USFederalHolidayCalendar()
holidays = cal.holidays(start=dr.min(), end=dr.max())
A = dr[~dr.isin(holidays)]
B = A[A.weekday != 5]
B = B[B.weekday != 6]

for year in set(B.year):
    tmp = B[B.year == year]
    for week in set(tmp.week):
        temp = tmp[tmp.week == week]
        print(temp[temp.weekday == temp.weekday.min()])  # begining of week
        print(temp[temp.weekday == temp.weekday.max()])  # ending of week

This results in:

DatetimeIndex(['2023-02-21'], dtype='datetime64[ns]', freq=None)
DatetimeIndex(['2023-02-24'], dtype='datetime64[ns]', freq=None)

But also gives a "FutureWarning: weekofyear and week have been deprecated ..."

Is there any way to get this in the datetime format so if I printed it, it would say 2023-02-21 for example. How can I avoid the FutureWarning?

(BTW I had sourced some of this from https://stackoverflow.com/a/43674721/21292653

答案1

得分: 0

代替 tmp.week,你应该尝试使用 pd.Index(tmp.isocalendar().week),即:

import pandas as pd
import datetime as dt
from pandas.tseries.holiday import USFederalHolidayCalendar

end = dt.datetime.now()
start = dt.datetime(end.year, end.month, end.day-5)

dr = pd.date_range(start=start, end=end)
cal = USFederalHolidayCalendar()
holidays = cal.holidays(start=dr.min(), end=dr.max())
A = dr[~dr.isin(holidays)]
B = A[A.weekday != 5]
B = B[B.weekday != 6]

for year in set(B.year):
    tmp = B[B.year == year]
    for week in set(pd.Index(tmp.isocalendar().week)):
        temp = tmp[pd.Index(tmp.isocalendar().week) == week]
        print(temp[temp.weekday == temp.weekday.min()])  # 一周的开始
        print(temp[temp.weekday == temp.weekday.max()])  # 一周的结束
英文:

Instead of tmp.week, you should try pd.Index(tmp.isocalendar().week) that is:

import pandas as pd
import datetime as dt
from pandas.tseries.holiday import USFederalHolidayCalendar

end = dt.datetime.now()
start = dt.datetime(end.year, end.month, end.day-5)

dr = pd.date_range(start=start, end=end)
cal = USFederalHolidayCalendar()
holidays = cal.holidays(start=dr.min(), end=dr.max())
A = dr[~dr.isin(holidays)]
B = A[A.weekday != 5]
B = B[B.weekday != 6]

for year in set(B.year):
    tmp = B[B.year == year]
    for week in set(pd.Index(tmp.isocalendar().week)):
        temp = tmp[pd.Index(tmp.isocalendar().week) == week]
        print(temp[temp.weekday == temp.weekday.min()])  # begining of week
        print(temp[temp.weekday == temp.weekday.max()])  # ending of week

huangapple
  • 本文由 发表于 2023年2月27日 03:07:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574377.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定