Iterating through a range of dates in Python

后端 未结 23 1332
醉酒成梦
醉酒成梦 2020-11-22 04:40

I have the following code to do this, but how can I do it better? Right now I think it\'s better than nested loops, but it starts to get Perl-one-linerish when you have a ge

相关标签:
23条回答
  • 2020-11-22 04:57

    This might be more clear:

    from datetime import date, timedelta
    
    start_date = date(2019, 1, 1)
    end_date = date(2020, 1, 1)
    delta = timedelta(days=1)
    while start_date <= end_date:
        print (start_date.strftime("%Y-%m-%d"))
        start_date += delta
    
    0 讨论(0)
  • 2020-11-22 04:57

    Slightly different approach to reversible steps by storing range args in a tuple.

    def date_range(start, stop, step=1, inclusive=False):
        day_count = (stop - start).days
        if inclusive:
            day_count += 1
    
        if step > 0:
            range_args = (0, day_count, step)
        elif step < 0:
            range_args = (day_count - 1, -1, step)
        else:
            raise ValueError("date_range(): step arg must be non-zero")
    
        for i in range(*range_args):
            yield start + timedelta(days=i)
    
    0 讨论(0)
  • 2020-11-22 04:58

    Numpy's arange function can be applied to dates:

    import numpy as np
    from datetime import datetime, timedelta
    d0 = datetime(2009, 1,1)
    d1 = datetime(2010, 1,1)
    dt = timedelta(days = 1)
    dates = np.arange(d0, d1, dt).astype(datetime)
    

    The use of astype is to convert from numpy.datetime64 to an array of datetime.datetime objects.

    0 讨论(0)
  • 2020-11-22 04:59

    Why are there two nested iterations? For me it produces the same list of data with only one iteration:

    for single_date in (start_date + timedelta(n) for n in range(day_count)):
        print ...
    

    And no list gets stored, only one generator is iterated over. Also the "if" in the generator seems to be unnecessary.

    After all, a linear sequence should only require one iterator, not two.

    Update after discussion with John Machin:

    Maybe the most elegant solution is using a generator function to completely hide/abstract the iteration over the range of dates:

    from datetime import timedelta, date
    
    def daterange(start_date, end_date):
        for n in range(int((end_date - start_date).days)):
            yield start_date + timedelta(n)
    
    start_date = date(2013, 1, 1)
    end_date = date(2015, 6, 2)
    for single_date in daterange(start_date, end_date):
        print(single_date.strftime("%Y-%m-%d"))
    

    NB: For consistency with the built-in range() function this iteration stops before reaching the end_date. So for inclusive iteration use the next day, as you would with range().

    0 讨论(0)
  • 2020-11-22 04:59

    You can generate a series of date between two dates using the pandas library simply and trustfully

    import pandas as pd
    
    print pd.date_range(start='1/1/2010', end='1/08/2018', freq='M')
    

    You can change the frequency of generating dates by setting freq as D, M, Q, Y (daily, monthly, quarterly, yearly )

    0 讨论(0)
  • 2020-11-22 05:01
    > pip install DateTimeRange
    
    from datetimerange import DateTimeRange
    
    def dateRange(start, end, step):
            rangeList = []
            time_range = DateTimeRange(start, end)
            for value in time_range.range(datetime.timedelta(days=step)):
                rangeList.append(value.strftime('%m/%d/%Y'))
            return rangeList
    
        dateRange("2018-09-07", "2018-12-25", 7)  
    
        Out[92]: 
        ['09/07/2018',
         '09/14/2018',
         '09/21/2018',
         '09/28/2018',
         '10/05/2018',
         '10/12/2018',
         '10/19/2018',
         '10/26/2018',
         '11/02/2018',
         '11/09/2018',
         '11/16/2018',
         '11/23/2018',
         '11/30/2018',
         '12/07/2018',
         '12/14/2018',
         '12/21/2018']
    
    0 讨论(0)
提交回复
热议问题