Pandas:创建具有不同频率的 pandas 日期时间序列
Pandas: Creating a pandas date-time series with different frequencies
我需要创建一个 pandas 列,其日期范围为 2015-12-01 至 2016-12-01,但具有不同的时间频率:
- 从 01:00:00 到 07:00:00 1 小时一班
- 从 07:00:00 到 22:00:00 30 分钟的频率
- 从 22:00:00 到 00:00:00 1 小时一班
第一天的输出应该如下所示,但是 objective 是在所有日期范围内执行此操作:
1 2015-12-01 02:00:00
2 2015-12-01 03:00:00
3 2015-12-01 04:00:00
4 2015-12-01 05:00:00
5 2015-12-01 06:00:00
6 2015-12-01 07:00:00
7 2015-12-01 07:30:00
8 2015-12-01 08:00:00
9 2015-12-01 08:30:00
10 2015-12-01 09:00:00
11 2015-12-01 09:30:00
12 2015-12-01 10:00:00
13 2015-12-01 10:30:00
14 2015-12-01 11:00:00
15 2015-12-01 11:30:00
16 2015-12-01 12:00:00
17 2015-12-01 12:30:00
18 2015-12-01 13:00:00
19 2015-12-01 13:30:00
20 2015-12-01 14:00:00
21 2015-12-01 14:30:00
22 2015-12-01 15:00:00
23 2015-12-01 15:30:00
24 2015-12-01 16:00:00
25 2015-12-01 16:30:00
26 2015-12-01 17:00:00
27 2015-12-01 17:30:00
28 2015-12-01 18:00:00
29 2015-12-01 18:30:00
30 2015-12-01 19:00:00
31 2015-12-01 19:30:00
32 2015-12-01 20:00:00
33 2015-12-01 20:30:00
34 2015-12-01 21:00:00
35 2015-12-01 21:30:00
36 2015-12-01 22:00:00
37 2015-12-01 23:00:00
38 2015-12-02 00:00:00
为此我使用了:
datetime_series_1 = pd.Series(pd.date_range("2015-12-01 01:00:00", periods=7 , freq="h"))
datetime_series_2 = pd.Series(pd.date_range("2015-12-01 07:30:00", periods=29 , freq="30min"))
datetime_series_3 = pd.Series(pd.date_range("2015-12-01 22:00:00", periods=3 , freq="h"))
datetime_series = pd.concat([datetime_series_1, datetime_series_2, datetime_series_3])
datetime_series.reset_index(inplace=True, drop=True)
print(datetime_series)
但是我不知道如何制作一个可以重现这个的 for 循环,但是在我上面提到的 2015-12-01 到 2016-12-01 的日期范围内。基本上我不知道如何在 for 循环中指示它更改 date_range 方法的字符串中的日期。
如有任何帮助,我们将不胜感激。
谢谢!
这应该可以解决问题:
#Your code
datetime_series_1 = pd.Series(pd.date_range("2015-12-01 01:00:00", periods=7 , freq="h"))
datetime_series_2 = pd.Series(pd.date_range("2015-12-01 07:30:00", periods=29 , freq="30min"))
datetime_series_3 = pd.Series(pd.date_range("2015-12-01 22:00:00", periods=3 , freq="h"))
datetime_series = pd.concat([datetime_series_1, datetime_series_2, datetime_series_3])
datetime_series.reset_index(inplace=True, drop=True)
#loop through the number of days and use a day delta adding to list
list_dates = [datetime_series]*366 #2016 was leap year :)
for i in range(0,366):
list_dates[i] = datetime_series + pd.Timedelta("{0} days".format(i))
#concat that list at the end
datetime_series = pd.concat(list_dates)
print(datetime_series)
我需要创建一个 pandas 列,其日期范围为 2015-12-01 至 2016-12-01,但具有不同的时间频率:
- 从 01:00:00 到 07:00:00 1 小时一班
- 从 07:00:00 到 22:00:00 30 分钟的频率
- 从 22:00:00 到 00:00:00 1 小时一班
第一天的输出应该如下所示,但是 objective 是在所有日期范围内执行此操作:
1 2015-12-01 02:00:00
2 2015-12-01 03:00:00
3 2015-12-01 04:00:00
4 2015-12-01 05:00:00
5 2015-12-01 06:00:00
6 2015-12-01 07:00:00
7 2015-12-01 07:30:00
8 2015-12-01 08:00:00
9 2015-12-01 08:30:00
10 2015-12-01 09:00:00
11 2015-12-01 09:30:00
12 2015-12-01 10:00:00
13 2015-12-01 10:30:00
14 2015-12-01 11:00:00
15 2015-12-01 11:30:00
16 2015-12-01 12:00:00
17 2015-12-01 12:30:00
18 2015-12-01 13:00:00
19 2015-12-01 13:30:00
20 2015-12-01 14:00:00
21 2015-12-01 14:30:00
22 2015-12-01 15:00:00
23 2015-12-01 15:30:00
24 2015-12-01 16:00:00
25 2015-12-01 16:30:00
26 2015-12-01 17:00:00
27 2015-12-01 17:30:00
28 2015-12-01 18:00:00
29 2015-12-01 18:30:00
30 2015-12-01 19:00:00
31 2015-12-01 19:30:00
32 2015-12-01 20:00:00
33 2015-12-01 20:30:00
34 2015-12-01 21:00:00
35 2015-12-01 21:30:00
36 2015-12-01 22:00:00
37 2015-12-01 23:00:00
38 2015-12-02 00:00:00
为此我使用了:
datetime_series_1 = pd.Series(pd.date_range("2015-12-01 01:00:00", periods=7 , freq="h"))
datetime_series_2 = pd.Series(pd.date_range("2015-12-01 07:30:00", periods=29 , freq="30min"))
datetime_series_3 = pd.Series(pd.date_range("2015-12-01 22:00:00", periods=3 , freq="h"))
datetime_series = pd.concat([datetime_series_1, datetime_series_2, datetime_series_3])
datetime_series.reset_index(inplace=True, drop=True)
print(datetime_series)
但是我不知道如何制作一个可以重现这个的 for 循环,但是在我上面提到的 2015-12-01 到 2016-12-01 的日期范围内。基本上我不知道如何在 for 循环中指示它更改 date_range 方法的字符串中的日期。
如有任何帮助,我们将不胜感激。
谢谢!
这应该可以解决问题:
#Your code
datetime_series_1 = pd.Series(pd.date_range("2015-12-01 01:00:00", periods=7 , freq="h"))
datetime_series_2 = pd.Series(pd.date_range("2015-12-01 07:30:00", periods=29 , freq="30min"))
datetime_series_3 = pd.Series(pd.date_range("2015-12-01 22:00:00", periods=3 , freq="h"))
datetime_series = pd.concat([datetime_series_1, datetime_series_2, datetime_series_3])
datetime_series.reset_index(inplace=True, drop=True)
#loop through the number of days and use a day delta adding to list
list_dates = [datetime_series]*366 #2016 was leap year :)
for i in range(0,366):
list_dates[i] = datetime_series + pd.Timedelta("{0} days".format(i))
#concat that list at the end
datetime_series = pd.concat(list_dates)
print(datetime_series)