从Python中的日期时间对象中查找年份和季度 [英] finding the year and quarter from a datetime object in python
问题描述
我有一个给定的日期,要求找到完整的季度(从1月到12月的1到4)。请注意,这不是当前季度,而是完整季度。我的意思是,如果输入日期为1月1日,则完整季度为4,而不是1。我的小代码段如下:
>>导入日期时间
>>导入数学
>>>现在= datetime.datetime(2015,1,1,0,0,0,0)
>> present_quarter = int(math.ceil(now.month / 3。))
>>如果present_quarter!= 1,则completed_quarter =(present_quarter-1)否则4
>> complete_quarter
4
但是我需要的也是年份,所以输出是:
2014-4
执行此操作的最佳方法是什么?输出是否可以保留为datetime对象而不是字符串,我的意思是Python是否支持 Year-Quarter
您只需将月份映射到季度和年份增量(第四季度需要去年):
def完成的季度(dt):
prev_quarter_map =((4,-1),(1,0),(2,0),(3,0))
季度,yd = prev_quarter_map [(dt.month-1)// 3]
的收益(dt.year + yd,Quarter)
这会将月份转换为0到3之间的值,然后将其转换为四分之一数字和年份增量。在当前年份中加上年份增量,可以得出上一季度已完成年份的正确年份。
>> def complete_quarter(dt):
... prev_quarter_map =((4,-1),(1,0),(2,0),(3,0))
...季度, yd = prev_quarter_map [(dt.month-1)// 3]
...返回(dt.year + yd,季度)
...
>>从日期时间导入日期
>> complete_quarter(date(2015,1,12))
(2014,4)
>> complete_quarter(date(2015,8,31))
(2015,2)
> complete_quarter(date(2015,12,31))
(2015,3)
datetime
模块没有可以模拟四分之一的对象;您可以具有 date (必须具有有效的年,月和日值)或日期和时间(添加有效的时,分,秒和微秒值) ,并带有可选的时区。
I have a requirement to find the completed quarter(1 to 4 from Jan to Dec), given a certain date. Note that it is not the current quarter but the completed quarter. By that I mean that if 1st Jan is the input date, then the completed quarter is 4 and not 1. The small code snippet that I have is as follows:
>>> import datetime
>>> import math
>>> now = datetime.datetime(2015, 1, 1, 0, 0, 0, 0)
>>> present_quarter = int(math.ceil(now.month/3.))
>>> completed_quarter = (present_quarter - 1) if present_quarter != 1 else 4
>>> completed_quarter
4
However what I need is the year as well so that the output is :
2014-4
What is the best way to do this ? Can the output remain a datetime object and not a string by which I mean does Python support a datetime object like Year-Quarter
You could simply map the month to the quarter and a year delta (you need last year for Q4):
def completed_quarter(dt):
prev_quarter_map = ((4, -1), (1, 0), (2, 0), (3, 0))
quarter, yd = prev_quarter_map[(dt.month - 1) // 3]
return (dt.year + yd, quarter)
This translates the month to a value between 0 and 3, then translates that into a quarter number and a year delta. Adding the year delta to the current year gives you the correct year for that completed previous quarter.
Demo:
>>> def completed_quarter(dt):
... prev_quarter_map = ((4, -1), (1, 0), (2, 0), (3, 0))
... quarter, yd = prev_quarter_map[(dt.month - 1) // 3]
... return (dt.year + yd, quarter)
...
>>> from datetime import date
>>> completed_quarter(date(2015, 1, 12))
(2014, 4)
>>> completed_quarter(date(2015, 8, 31))
(2015, 2)
>>> completed_quarter(date(2015, 12, 31))
(2015, 3)
The datetime
module has no objects that can model a quarter; you can have a date (which has to have valid year, month and day values) or a date and time (adding on a valid hour, minute, second and microsecond value, with optional timezone).
这篇关于从Python中的日期时间对象中查找年份和季度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!