从 python 中的日期范围创建数据框

Create a dataframe from a date range in python

给定两个日期的间隔,这将是一个 Python 时间戳。

create_interval('2022-01-12', '2022-01-17', 'Holidays')

创建以下数据框:

date interval_name
2022-01-12 00:00:00 Holidays
2022-01-13 00:00:00 Holidays
2022-01-14 00:00:00 Holidays
2022-01-15 00:00:00 Holidays
2022-01-16 00:00:00 Holidays
2022-01-17 00:00:00 Holidays

如果能在几行代码中,我将不胜感激。非常感谢您的帮助。

如果您愿意使用 Pandas,这应该可以满足您的要求

import pandas as pd

def create_interval(start, end, field_val):
    #setting up index date range
    idx = pd.date_range(start, end)
    #create the dataframe using the index above, and creating the empty column for interval_name
    df = pd.DataFrame(index = idx, columns = ['interval_name'])
    #set the index name
    df.index.names = ['date']
    #filling out all rows in the 'interval_name' column with the field_val parameter
    df.interval_name = field_val
    return df

create_interval('2022-01-12', '2022-01-17', 'holiday')

希望我编写的代码正是您所需要的。

import pandas as pd

def create_interval(ts1, ts2, interval_name):
    ts_list_dt = pd.date_range(start=ts1, end=ts2).to_pydatetime().tolist()
    ts_list = list(map(lambda x: ''.join(str(x)), ts_list_dt))
    d = {'date': ts_list, 'interval_name': [interval_name]*len(ts_list)}
    df = pd.DataFrame(data=d)
    return df

df = create_interval('2022-01-12', '2022-01-17', 'Holidays')
print(df)

输出:

         date             interval_name
0  2022-01-12 00:00:00      Holidays
1  2022-01-13 00:00:00      Holidays
2  2022-01-14 00:00:00      Holidays
3  2022-01-15 00:00:00      Holidays
4  2022-01-16 00:00:00      Holidays
5  2022-01-17 00:00:00      Holidays

如果你想要没有索引列的DataFrame,在创建DataFrame df = pd.DataFrame(data=d)之后使用df = df.set_index('date')。然后你会得到:

    date             interval_name      
2022-01-12 00:00:00      Holidays
2022-01-13 00:00:00      Holidays
2022-01-14 00:00:00      Holidays
2022-01-15 00:00:00      Holidays
2022-01-16 00:00:00      Holidays
2022-01-17 00:00:00      Holidays