Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a list of datetime with knowing only the start, the number of iteration and timedelta

I have multiple dataframe with various number of rows.
I want to generate differents lists in order to create a column of datetime in every dataframe.

Each row of dataframe correspond to a half-hourly step, and the datetime format is YYYYMMDD HHMMSS+0100

At the end my output should be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

     Date
0    20210101 000000+0100
1    20210101 003000+0100
2    20210101 010000+0100
3    20210101 013000+0100
....

The difference with the problems I encounter on the internet is that I do not enter the end date.

Thanks for your help

>Solution :

import pandas as pd
from datetime import datetime, timedelta
import pytz

df = pd.DataFrame([('A', 'Q'), ('Z', 'S'), ('E', 'D'), ('R', 'F')])

start = pytz.timezone('Europe/Amsterdam').localize(datetime(2021, 1, 1))
delta = timedelta(minutes=30)

# column of datetime
df['date'] = [start + delta * i for i in range(len(df))]

# column of string
df['date'] = df['date'].apply(lambda x: x.strftime("%Y%m%d %H%M%S%z"))
   0  1                  date
0  A  Q  20210101 000000+0100
1  Z  S  20210101 003000+0100
2  E  D  20210101 010000+0100
3  R  F  20210101 013000+0100
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading