Reputation: 27
I need to create a pandas column that has a date range from 2015-12-01 to 2016-12-01, but with different time frequencies:
The output for the first day should look like this, however the objective is to do it for all the date range:
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
For this I used:
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)
However I don't know how to make a for loop that can reproduce this but over the date range from 2015-12-01 to 2016-12-01 I mentioned above. Basically I don't know how in the for loop I can indicate it to change the date in the string of the date_range method.
Any help would be greatly appreciated.
Thank you !
Upvotes: 1
Views: 1152
Reputation: 2776
This should do the trick:
#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)
Upvotes: 3