MoMo
MoMo

Reputation: 107

have a list from the results of a print (python datetime)

I did a function that display the dates between two dates.

I want to affect the result displayed in a list or df.

I have this code

next_day = 2018-09-26 01:07:00
while True:
      if next_day > 2018-09-26 01:20:00:
          break
        print(next_day)
        next_day += timedelta(minutes=1)

I have this result :

2018-09-26 01:07:00
2018-09-26 01:08:00
2018-09-26 01:09:00
2018-09-26 01:10:00
2018-09-26 01:11:00
2018-09-26 01:12:00
2018-09-26 01:13:00
2018-09-26 01:14:00
2018-09-26 01:15:00
2018-09-26 01:16:00
2018-09-26 01:17:00
2018-09-26 01:18:00
2018-09-26 01:19:00
2018-09-26 01:20:00

I want to put this in a list or df.

Upvotes: 0

Views: 33

Answers (1)

jezrael
jezrael

Reputation: 862641

Use date_range with DataFrame constructor:

next_day = '2018-09-26 01:07:00'

df = pd.DataFrame({'date': pd.date_range(next_day, '2018-09-26 01:20:00', freq='1Min')})
#alternative
#df = pd.DataFrame({'date': pd.date_range(next_day, '2018-09-26 01:20:00', freq='T')})
print (df)
                  date
0  2018-09-26 01:07:00
1  2018-09-26 01:08:00
2  2018-09-26 01:09:00
3  2018-09-26 01:10:00
4  2018-09-26 01:11:00
5  2018-09-26 01:12:00
6  2018-09-26 01:13:00
7  2018-09-26 01:14:00
8  2018-09-26 01:15:00
9  2018-09-26 01:16:00
10 2018-09-26 01:17:00
11 2018-09-26 01:18:00
12 2018-09-26 01:19:00
13 2018-09-26 01:20:00

Upvotes: 1

Related Questions