0

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:

     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

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
SimonW.
  • 33
  • 6
  • so that is a column of string ? Because specific format only has sense for string – azro Jan 02 '22 at 11:38
  • 1
    Does this answer your question? [Creating a range of dates in Python](https://stackoverflow.com/questions/993358/creating-a-range-of-dates-in-python) – dimay Jan 02 '22 at 11:40
  • @azro At the end I will probably convert datetime into string if needed because I need this specific format – SimonW. Jan 02 '22 at 11:42

1 Answers1

2
import pandas as pd

df = pd.DataFrame([('A', 'Q'), ('Z', 'S'), ('E', 'D'), ('R', 'F')])
df['date'] = pd.date_range(start='2021-01-01', periods=len(df), freq='30min',
                           tz='Europe/Amsterdam').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
azro
  • 53,056
  • 7
  • 34
  • 70
  • Thanks, that's exactly what I need – SimonW. Jan 02 '22 at 11:49
  • If using pandas then you can use `date_range` to do the above in one step... eg: `df['date'] = pd.date_range(start='2021-01-01', periods=len(df), freq='30min', tz='Europe/Amsterdam').strftime('%Y%m%d %H%M%S%z')` – Jon Clements Jan 02 '22 at 11:59