给定时期的第一天的Python列表

时间:2014-03-27 18:53:45

标签: python list

我正在尝试找到一种有效的方法来创建日期列表,仅包括特定时期的月份的第一天。这样的东西,但更好:

import datetime
dates = [
  datetime.date (2014, 4, 1),
  datetime.date (2014, 5, 1),
  datetime.date (2014, 6, 1),
  datetime.date (2014, 7, 1),
  datetime.date (2014, 8, 1),
  datetime.date (2014, 9, 1),
  datetime.date (2014, 10, 1),
  datetime.date (2014, 11, 1),
  datetime.date (2014, 12, 1),
  datetime.date (2015, 1, 1),
  datetime.date (2015, 2, 1)]

或者,谷歌为此做些什么方向。干杯!

8 个答案:

答案 0 :(得分:6)

>>> startyear = 2014
>>> startmonth = 4
>>> endyear = 2015
>>> endmonth = 2
>>> [datetime.date(m/12, m%12+1, 1) for m in xrange(startyear*12+startmonth-1, endyear*12+endmonth)]
[datetime.date(2014, 4, 1), datetime.date(2014, 5, 1), datetime.date(2014, 6, 1), datetime.date(2014, 7, 1), datetime.date(2014, 8, 1), datetime.date(2014, 9, 1), datetime.date(2014, 10, 1), datetime.date(2014, 11, 1), datetime.date(2014, 12, 1), datetime.date(2015, 1, 1), datetime.date(2015, 2, 1)]

对于Python 3,您需要使用range而不是xrange//floor division)而不是/(它会浮动) Python中的划分3):

[datetime.date(m//12, m%12+1, 1) for m in range(startyear*12+startmonth-1, endyear*12+endmonth)]

答案 1 :(得分:3)

用熊猫:

   dates= pd.date_range('2018-01-01','2020-01-01' , freq='1M')-pd.offsets.MonthBegin(1)

结果:

`DatetimeIndex(['2018-01-01', '2018-02-01', '2018-03-01', '2018-04-01',
               '2018-05-01', '2018-06-01', '2018-07-01', '2018-08-01',
               '2018-09-01', '2018-10-01', '2018-11-01', '2018-12-01',
               '2019-01-01', '2019-02-01', '2019-03-01', '2019-04-01',
               '2019-05-01', '2019-06-01', '2019-07-01', '2019-08-01',
               '2019-09-01', '2019-10-01', '2019-11-01', '2019-12-01'],
              dtype='datetime64[ns]', freq='MS')

答案 2 :(得分:2)

如果您只创建了几年的列表,那么效率不应该成为一个问题。清晰的代码是最重要的方面。

dates = []
date = datetime.date.today()
while date.year < 2015:
    if date.day == 1:
        dates.append(date)
    date += datetime.timedelta(days=1)

答案 3 :(得分:2)

没有理由强行说明这一点:

import datetime
from pprint import pprint

dt = datetime.date.today()
end = datetime.date(2015, 2, 1)
dates = []

while dt < end:
    if not dt.month % 12:
        dt = datetime.date(dt.year+1, 1, 1)
    else:
        dt = datetime.date(dt.year, dt.month+1, 1)
    dates.append(dt)

pprint(dates)

输出:

[datetime.date(2014, 4, 1),
 datetime.date(2014, 5, 1),
 datetime.date(2014, 6, 1),
 datetime.date(2014, 7, 1),
 datetime.date(2014, 8, 1),
 datetime.date(2014, 9, 1),
 datetime.date(2014, 10, 1),
 datetime.date(2014, 11, 1),
 datetime.date(2014, 12, 1),
 datetime.date(2015, 1, 1),
 datetime.date(2015, 2, 1)]

答案 4 :(得分:1)

您可以使用dateutil中的relativedelta,然后创建一个函数以使用任何日期范围:

from datetime import date
from dateutil.relativedelta import relativedelta

def mthStList(start_date, end_date):
    stdt_list = []
    cur_date = start_date.replace(day=1) # sets date range to start of month
    while cur_date <= end_date:
        stdt_list.append(cur_date)
        cur_date += relativedelta(months=+1)
    return stdt_list

mthStList(date(2012, 5, 26), date.today())

答案 5 :(得分:1)

尝试使用“ MS”(月开始)频率别名的pandas.date_range():

import pandas as pd

start = '2020-01-01'
end = '2020-12-01'

dates = pd.date_range(start, end, freq='MS')

print(dates)

结果:

DatetimeIndex(['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01',
               '2020-05-01', '2020-06-01', '2020-07-01', '2020-08-01',
               '2020-09-01', '2020-10-01', '2020-11-01', '2020-12-01'],
              dtype='datetime64[ns]', freq='MS')

可以找到所有频率别名的列表here

答案 6 :(得分:0)

import datetime 
import calendar 
import re
from datetime import date

def findDay(date): 
    born = datetime.datetime.strptime(date, '%Y %m %d').weekday() 
    return (calendar.day_name[born]) 

def hyp(j):    
    no_hyphens = re.sub('-',' ',str(j))
    return no_hyphens

def send(year_start):    
    date = hyp(year_start)
    print (date)
    print(findDay(date))

epoch_year = 2019
for i in range(1,13):
    year_start = date(epoch_year, i, 1)
    send(year_start)

答案 7 :(得分:0)

这与@Andrew的答案相同,但速度更快:

dates = []
date = datetime.date.today()
while date.year < 2015:
    if date.day == 1:
        dates.append(date)
        # I added the following line:
        date += datetime.timedelta(days=27)
    date += datetime.timedelta(days=1)

使用武力,但不要残酷