pandas 生成两个日期之间的日期列表的Python

ma8fv8wu  于 2023-02-27  发布在  Python
关注(0)|答案(9)|浏览(143)

我想生成一个介于两个日期之间的日期列表,并将它们以字符串格式存储在列表中。此列表有助于与我拥有的其他日期进行比较。
我的代码如下:

from datetime import date, timedelta

sdate = date(2019,3,22)   # start date
edate = date(2019,4,9)   # end date

def dates_bwn_twodates(start_date, end_date):
    for n in range(int ((end_date - start_date).days)):
        yield start_date + timedelta(n)
print(dates_bwn_twodates(sdate,edate))

我目前的产出:

<generator object dates_bwn_twodates at 0x000002A8E7929410>

我的预期输出:

['2019-03-22',.....,'2019-04-08']

我的代码有问题。

7vux5j2d

7vux5j2d1#

您可以使用pandas.date_range()来执行以下操作:

import pandas
pandas.date_range(sdate,edate-timedelta(days=1),freq='d')
DatetimeIndex(['2019-03-22', '2019-03-23', '2019-03-24', '2019-03-25',
           '2019-03-26', '2019-03-27', '2019-03-28', '2019-03-29',
           '2019-03-30', '2019-03-31', '2019-04-01', '2019-04-02',
           '2019-04-03', '2019-04-04', '2019-04-05', '2019-04-06',
           '2019-04-07', '2019-04-08'],
          dtype='datetime64[ns]', freq='D')
cgyqldqp

cgyqldqp2#

你的代码重写为列表解析:

[sdate+timedelta(days=x) for x in range((edate-sdate).days)]

结果:

[datetime.date(2019, 3, 22),
 datetime.date(2019, 3, 23),
 datetime.date(2019, 3, 24),
          :
 datetime.date(2019, 4, 7),
 datetime.date(2019, 4, 8)]
vd8tlhqk

vd8tlhqk3#

我很惊讶这不是datetime包中的标准函数。
下面是一个执行所请求操作的函数:

from datetime import timedelta

def date_range_list(start_date, end_date):
    # Return list of datetime.date objects (inclusive) between start_date and end_date (inclusive).
    date_list = []
    curr_date = start_date
    while curr_date <= end_date:
        date_list.append(curr_date)
        curr_date += timedelta(days=1)
    return date_list

用法:

from datetime import date, timedelta

def date_range_list(start_date, end_date):
    # Return list of datetime.date objects (inclusive) between start_date and end_date (inclusive).
    date_list = []
    curr_date = start_date
    while curr_date <= end_date:
        date_list.append(curr_date)
        curr_date += timedelta(days=1)
    return date_list

start_date = date(year=2021, month=12, day=20)
stop_date = date(year=2021, month=12, day=25)
date_list = date_range_list(start_date, stop_date)

date_list

输出:

[datetime.date(2021, 12, 20),
 datetime.date(2021, 12, 21),
 datetime.date(2021, 12, 22),
 datetime.date(2021, 12, 23),
 datetime.date(2021, 12, 24),
 datetime.date(2021, 12, 25)]

更新

虽然上面的代码简单易行,但最好是为列表提供一个生成器,而不是列表本身。这样,整个datetime数组就不需要生成并存储到内存中,除非它需要这样做。下面是这种方法的外观:

from datetime import timedelta

def date_range_list(start_date, end_date):
    # Return generator for a list datetime.date objects (inclusive) between start_date and end_date (inclusive).
    curr_date = start_date
    while curr_date <= end_date:
        yield curr_date 
        curr_date += timedelta(days=1)

用法:

from datetime import date, timedelta

def date_range_list(start_date, end_date):
    # Return generator for a list datetime.date objects (inclusive) between start_date and end_date (inclusive).
    curr_date = start_date
    while curr_date <= end_date:
        yield curr_date 
        curr_date += timedelta(days=1)

start_date = date(year=2021, month=12, day=20)
stop_date = date(year=2021, month=12, day=25)
date_list = date_range_list(start_date, stop_date)

for date in date_list:
   print(date)

输出:

2021-12-20
2021-12-21
2021-12-22
2021-12-23
2021-12-24
2021-12-25
sczxawaw

sczxawaw4#

from datetime import date, timedelta

sdate = date(2019,3,22)   # start date
edate = date(2019,4,9)   # end date
date_modified=sdate
list=[sdate] 

while date_modified<edate:
    date_modified+=timedelta(days=nbDaysbtw2dates) 
    list.append(date_modified)

print(list)
gcxthw6b

gcxthw6b5#

你需要把它转换成一个带有字符串的列表:

print([str(d) for d in dates_bwn_twodates(sdate,edate)])
sg3maiej

sg3maiej6#

有一种简单得多的方法,只需修改代码即可使用,如下所示;

from datetime import datetime, timedelta
from datetime import date

def date_bwn_two_dates(start_date, end_date):
    date_list = [] # The list where we want to store
    for i in range(int((end_date-start_date).days)+1): # Iterate between the range of dates
        year = (start_date+timedelta(i)).strftime("%Y") # Get the Year
        month = (start_date+timedelta(i)).strftime("%m") # Get the month
        date_a = (start_date+timedelta(i)).strftime("%d") # Get the day
        date_list.append([year, month, date_a]) # Append the Objects accquired
    return date_list # return the list

for i in date_bwn_two_dates(date(2020, 12, 1), date(2021, 12, 1)):
    print(i)
sbtkgmzw

sbtkgmzw7#

您可以使用https://github.com/zachwill/moment.git中的moment库来简化您的工作。

import moment

def dates_bwn_twodates(start_date, end_date):
    diff = abs(start_date.diff(end_date).days)
    
    for n in range(0,diff+1):
        yield start_date.strftime("%Y-%m-%d")
        start_date = (start_date).add(days=1)

sdate = moment.date('2019-03-22')   #start date
edate = moment.date('2019-04-09')   #end date

然后你就有了选择

dates = list(dates_bwn_twodates(sdate,edate)) #dates as a list

或者你可以迭代

for date in dates_bwn_twodates(sdate,edate):
    #do something with each date
s2j5cfk0

s2j5cfk08#

如果您想要日期列表的字符串,而不仅仅是日期时间索引,那么还需要使用strftime格式化

from datetime import date, timedelta

def timer():
    global datelist
    sdate = date(2022, 5, 1)
    edate = date(2022, 6, 1)

    delta = edate - sdate       
    datetimes = []
    for i in range(delta.days + 1):
        day = sdate + timedelta(days=i)
        datetimes.append(day)

    def formatting():
        global converted
        converted = pd.to_datetime(datetimes)
        return converted

    datelist = converted.strftime("%Y-%m-%d").tolist()

    formatting()
ar5n3qh5

ar5n3qh59#

这是一个有点老的问题,但我想我应该把我的建议,因为其中一些似乎过于复杂的答案:

from datetime import date, timedelta

#-- the actual method --#
def get_start_to_end(start_date, end_date):
    date_list = []
    for i in range(0, (end_date - start_date).days + 1):
        date_list.append(  str(start_date + timedelta(days=i))  ) #<-- here
    return date_list
#-- end of the actual method --#

# -- demonstrating it --#
sd = date(2022,8,12)
ed = date(2022,11,17)
dates = get_start_to_end(sd, ed)

for d in dates:
    print(d)

#-- You can just append the date object, the default string (iso)
#-- or use strftime for a different format
#-- (start_date + timedelta(days=i)) <-- date object
#-- str(start_date + timedelta(days=i))  <-- default string
#-- (start_date + timedelta(days=i)).strftime("%b %d, %Y") <-- other string format

相关问题