Search code examples
pythonpython-3.xpandasanacondaminiconda

How can I get a list of the days in a year and follow this form?


This code not work at all, and I'm still confusing with the pandas datetime method...

def date_list():

    list = []

    for i in pd.date_range(datetime(2016, 1, 1), datetime(2016, 12, 31)):

        list.append(datetime[1], datetime[2])
    return list

This is the example for the list, and should I use zfill method for making this list?(If it's leap year) [0101, 0102, 0103, ..., 1230, 1231]


Solution

  • You can use strftime to directly do the formatting:

    dt_list = pd.date_range('2016-01-01', '2016-12-31').strftime('%m%d')
    

    Use tolist if you want a Python list instead of a numpy array:

    dt_list = pd.date_range('2016-01-01', '2016-12-31').strftime('%m%d').tolist()