Create date variable

Asked

Viewed 212 times

-1

I’m starting with Python, I’ve been working with Qlikview for many years and I’m migrating some data processing activities to Python.

I would like to create a set with a variable of days and months. From the current date following a d-1 pattern as follows:

29/11/2020 
28/11/2020
27/11/2020
26/11/2020
25/11/2020
...

It was weighing on doing a for, something like that inside a loop.

2 answers

1

You can create a date list and then create your dataframe

Create date list

>>> import datetime

>>> initial_date = datetime.datetime(2020, 1, 1)

>>> initial_date
datetime.datetime(2020, 1, 1, 0, 0)

>>> time_delta = datetime.timedelta(1)

>>> lista = []
>>>
>>> for i in range(10):
...     lista.append(initial_date + (time_delta * i))
...
>>> lista
[datetime.datetime(2020, 1, 1, 0, 0), datetime.datetime(2020, 1, 2, 0, 0), datetime.datetime(2020, 1, 3, 0, 0), datetime.datetime(2020, 1, 4, 0, 0), datetime.datetime(2020, 1, 5, 0, 0), datetime.datetime(2020, 1, 6, 0, 0), datetime.datetime(2020, 1, 7, 0, 0), datetime.datetime(2020, 1, 8, 0, 0), datetime.datetime(2020, 1, 9, 0, 0), datetime.datetime(2020, 1, 10, 0, 0)]

Create dataframe

>>> import pandas as pd

>>> df = pd.DataFrame({"datas": lista})

>>> df
       datas
0 2020-01-01
1 2020-01-02
2 2020-01-03
3 2020-01-04
4 2020-01-05
5 2020-01-06
6 2020-01-07
7 2020-01-08
8 2020-01-09
9 2020-01-10
>>>

Placing each date in a column

>>> lista_vars = [f"vData{i}" for i in range(10)]

>>> to_df = dict(zip(lista_vars, lista_datas))

>>> df = pd.DataFrame(to_df, index=[1])

>>> df

      vData0     vData1     vData2     vData3     vData4     vData5     vData6     vData7     vData8     vData9
1 2020-01-01 2020-01-02 2020-01-03 2020-01-04 2020-01-05 2020-01-06 2020-01-07 2020-01-08 2020-01-09 2020-01-10

For the output in the format you want, use the strftime

>>> df['vData0'].dt.strftime('%d/%m/%Y')

1    01/01/2020
Name: vData0, dtype: object

I hope it helps

  • Thanks, it helped yes, but I need each date to be in a variable; vData1 = 01/12/2020.... vData90 = 03/09/2020, I need it to be in this format.

  • @Insert, updated the post in response to your comment

1

You can use the data_range of pandas:

import pandas as pd
from datetime import date
data_atual = date.today()

datas = pd.date_range(start = '11/05/2020' , end = data_atual, freq='D')[::-1]

The parameters in this case are the beginning and the end of the date range, note that at the end I invert the order using the dates with [::-1].

Exit:

pd.DataFrame({'Datas': datas})

         Datas
0   2020-12-01
1   2020-11-30
2   2020-11-29
3   2020-11-28
4   2020-11-27
5   2020-11-26
6   2020-11-25
7   2020-11-24
8   2020-11-23
           ...
22  2020-11-09
23  2020-11-08
24  2020-11-07
25  2020-11-06
26  2020-11-05

Update

To create multiple columns

Loading the packages

import pandas as pd
from datetime import date
data_atual = date.today()

Creating the date range

datas = pd.date_range(start = '11/05/2020' , end = data_atual, freq='D')[::-1]
datas = datas.strftime('%d/%m/%Y')

Creating the columns

colunas = [f'vData{i}' for i in range(len(datas))]

Creating the Data Frame in a transposed form

pd.DataFrame(datas, index = colunas).T

Exit:

      vData0       vData1     vData2      vData3      vData4      vData5    vData6      vData7       vData8     vData9      ...             
0   01/12/2020  30/11/2020  29/11/2020  28/11/2020  27/11/2020  26/11/2020  25/11/2020  24/11/2020  23/11/2020  22/11/2020  ... 

Code:

import pandas as pd
from datetime import date
data_atual = date.today()

datas = pd.date_range(start = '11/05/2020' , end = data_atual, freq='D')[::-1]
datas = datas.strftime('%d/%m/%Y')
colunas = [f'vData{i}' for i in range(len(datas))]
pd.DataFrame(datas, index = colunas).T

Browser other questions tagged

You are not signed in. Login or sign up in order to post.