Troubles creating automated incremental series

Hi I’m triying to create a series that contains the day in a column and the hour in another column and restart the counting of hours every day on a given dates range, however the best I could be done is the following:

import pandas as pd
from datetime import date as dt
fecha=pd.date_range(start=“2020-09-09”,end=“2020-09-10”).to_pydatetime().tolist()
for h in range(1,25):
print(‘HourlyminCapEmpI’,fecha,80,h)

that gives me:
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 1
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 2
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 3
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 4
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 5
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 6
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 7
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 8
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 9
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 10
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 11
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 12
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 13
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 14
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 15
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 16
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 17
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 18
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 19
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 20
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 21
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 22
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 23
HourlyminCapEmpI [datetime.datetime(2020, 9, 9, 0, 0), datetime.datetime(2020, 9, 10, 0, 0)] 80 24
but is not restarting the hour count every day, any support?
thanks!!!

Welcome, @trs ! This seems like more of a Pandas question than a Python one, so you might be better off asking on a Pandas-specific forum. Further, its a little unclear what your expected output looks like, and what data structure you intend to use. You say you want to create a “series”, but a Series can only contain one column, so I’d presume you want a DataFrame? However, you are explicitly converting your output to a list, and then doing a calculation in a for loop that just prints a list of two datetimes repeatidly. If you can provide an example of your expected output (as Python would print it), and anything given as input, that would be great. Thanks!

Hi yes I think i was not so clear, the desired output is the following:
HourlyminCapEmpI, 01/01/2021,1,80
HourlyminCapEmpI, 01/01/2021,2,80
HourlyminCapEmpI, 01/01/2021,3,80
HourlyminCapEmpI, 01/01/2021,4,80
HourlyminCapEmpI, 01/01/2021,5,80
HourlyminCapEmpI, 01/01/2021,6,80
HourlyminCapEmpI, 01/01/2021,7,80
HourlyminCapEmpI, 01/01/2021,8,80
HourlyminCapEmpI, 01/01/2021,9,80
HourlyminCapEmpI, 01/01/2021,10,70
HourlyminCapEmpI, 01/01/2021,11,50
HourlyminCapEmpI, 01/01/2021,12,40
HourlyminCapEmpI, 01/01/2021,13,38
HourlyminCapEmpI, 01/01/2021,14,38
HourlyminCapEmpI, 01/01/2021,15,38
HourlyminCapEmpI, 01/01/2021,16,38
HourlyminCapEmpI, 01/01/2021,17,40
HourlyminCapEmpI, 01/01/2021,18,50
HourlyminCapEmpI, 01/01/2021,19,60
HourlyminCapEmpI, 01/01/2021,20,80
HourlyminCapEmpI, 01/01/2021,21,80
HourlyminCapEmpI, 01/01/2021,22,80
HourlyminCapEmpI, 01/01/2021,23,80
HourlyminCapEmpI, 01/01/2021,24,80
HourlyminCapEmpI, 02/01/2021,1,80
HourlyminCapEmpI, 02/01/2021,2,80
HourlyminCapEmpI, 02/01/2021,3,80
HourlyminCapEmpI, 02/01/2021,4,80
HourlyminCapEmpI, 02/01/2021,5,80
HourlyminCapEmpI, 02/01/2021,6,80
HourlyminCapEmpI, 02/01/2021,7,80
HourlyminCapEmpI, 02/01/2021,8,80
HourlyminCapEmpI, 02/01/2021,9,80
HourlyminCapEmpI, 02/01/2021,10,70
HourlyminCapEmpI, 02/01/2021,11,50
HourlyminCapEmpI, 02/01/2021,12,40
HourlyminCapEmpI, 02/01/2021,13,38
HourlyminCapEmpI, 02/01/2021,14,38
HourlyminCapEmpI, 02/01/2021,15,38
HourlyminCapEmpI, 02/01/2021,16,38
HourlyminCapEmpI, 02/01/2021,17,40
HourlyminCapEmpI, 02/01/2021,18,50
HourlyminCapEmpI, 02/01/2021,19,60
HourlyminCapEmpI, 02/01/2021,20,80
HourlyminCapEmpI, 02/01/2021,21,80
HourlyminCapEmpI, 02/01/2021,22,80
HourlyminCapEmpI, 02/01/2021,23,80
HourlyminCapEmpI, 02/01/2021,24,80

Thanks for your comments.

Thanks. Its still unclear what the final column is supposed to represent, and the date format is ambiguous, but I’m guessing its DD/MM/YYYY (I suggest always using standard ISO 8601 YYYY-MM-DD instead, unless explicitly required otherwise). However, if you simply want to get a dataframe indexed by time with the day and hour as integer columns, you can create it with pd.date_range(), exactly as you do, with hour frequency:

datetimes = pd.date_range(start="2021-01-01", end="2021-01-03", freq="H")

Then, you can simply get the .day and .hour attributes from the resulting DateTimeIndex object, and wrap them (for example) into a dataframe:

df = pd.DataFrame({"day": datetimes.day, "hour": datetimes.hour}, index=datetimes)

You can add other data as desired to this dataframe, or print it, as desired. In general, its best to always avoid for loops unless they are explicitly necessary and use native Numpy and pandas methods and functions instead, as they are simpler, cleaner and perform much better.

See the Pandas user guide for more details.