简体   繁体   English

获取此[周/月/季度/年]的最后一天的日期

[英]Getting the date of the last day of this [week/month/quarter/year]

有没有办法得到这个[周/月/季/年]的最后一天与datetimepandas或其他日期和时间工具的日期( datetimepd.Timestamp或等价物)?

You could add a pandas.DateOffset to a DateTimeIndex , Timestamp or datetime.date or datetime.datetime : 您可以将pandas.DateOffset添加到DateTimeIndexTimestampdatetime.datedatetime.datetime

dates = pd.date_range('2015-8-13', periods=4, freq='3D')
# DatetimeIndex(['2015-08-13', '2015-08-16', '2015-08-19', '2015-08-22'],
# dtype='datetime64[ns]', freq='3D', tz=None)

Snap to the last day of the week (for example, Sunday): 捕捉到一周的最后一天(例如,星期日):

In [232]: dates+offsets.Week(weekday=6)
Out[232]: DatetimeIndex(['2015-08-16', '2015-08-23', '2015-08-23', '2015-08-23'], dtype='datetime64[ns]', freq=None, tz=None)

Snap to the last day of the month: 截至本月的最后一天:

In [207]: dates+offsets.MonthEnd()
Out[207]: DatetimeIndex(['2015-08-31', '2015-08-31', '2015-08-31', '2015-08-31'], dtype='datetime64[ns]', freq=None, tz=None)

Snap to the last day in the quarter: 赶上本季度的最后一天:

In [212]: dates+offsets.QuarterEnd()
Out[215]: DatetimeIndex(['2015-09-30', '2015-09-30', '2015-09-30', '2015-09-30'], dtype='datetime64[ns]', freq=None, tz=None)

Snap to the last day of the year: 赶到一年的最后一天:

In [219]: dates+offsets.YearEnd()
Out[222]: DatetimeIndex(['2015-12-31', '2015-12-31', '2015-12-31', '2015-12-31'], dtype='datetime64[ns]', freq=None, tz=None)

Notice that adding an offset always advances the date. 请注意,添加偏移始终会使日期前进。 For example, 2015-08-16 is a Sunday, and adding an offsets.Week(weekday=6) advances it it 2015-08-23: 例如,2015-08-16是一个星期日,并添加一个offsets.Week(weekday=6)推进它2015-08-23:

In [233]: pd.Timestamp('2015-8-16')+offsets.Week(weekday=6)
Out[233]: Timestamp('2015-08-23 00:00:00')

To prevent that from happening, you could subtract one day from dates : 为防止这种情况发生,您可以从dates减去一天:

In [234]: dates - offsets.Day() + offsets.Week(weekday=6)
Out[237]: DatetimeIndex(['2015-08-16', '2015-08-16', '2015-08-23', '2015-08-23'], dtype='datetime64[ns]', freq=None, tz=None)

Using datetime only. 仅使用datetime

>>> d = datetime.date.today()

Last day of week: 一周的最后一天:

# This was wrong earlier.
>>> d + datetime.timedelta(days=5 - d.weekday())
datetime.date(2015, 8, 15)

Last day of month: 每月的最后一天:

>>> datetime.date(year=(d.year + int(d.month % 12 == 0)), month=(d.month + 1) % 12, day=1) - datetime.timedelta(days=1)
datetime.date(2015, 8, 31)

Last day of quarter: 季度的最后一天:

>>> datetime.date(year=d.year, month=((d.month % 3) + 1) * 3 + 1, day=1) - datetime.timedelta(days=1)
datetime.date(2015, 9, 30)

Last day of year: 一年的最后一天:

>>> datetime.date(year=d.year, month=12, day=31)
datetime.date(2015, 12, 31)

EDIT: This is all pretty ugly and using a higher level third party library is probably best, unless there is a compelling reason not to (and there does not seem to be here). 编辑:这一切都非常难看,使用更高级别的第三方库可能是最好的,除非有令人信服的理由不(并且似乎没有在这里)。

import datetime
from dateutil.relativedelta import relativedelta, SU

def last_day_of_week(year, week_num):
    # assuming sunday is the last day of week
    # weeks= is an offset, hence the minus one
    return datetime.date(year, 1, 1) + relativedelta(weekday=SU(1), weeks=week_num - 1)

def last_day_of_month(year, month):
    return datetime.date(year, month, 1) + relativedelta(months=1, days=-1)

def last_day_of_year(year):
    return datetime.date(year, 1, 1) + relativedelta(years=1, days=-1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM