Python: Loop over datetimeindex based on different periods

Question:

I have a DataFrame and I am trying to loop over the datetmeindex based on different frequencies:

data = [[99330,12,122],[1123,1230,1287],[123,101,812739],[1143,12301230,252],[234,342,4546],[2445,3453,3457],[7897,8657,5675],   [46,5675,453],[76,484,3735],   [363,93,4568],   [385,568,367],   [458,846,4847],   [574,45747,658468],   [57457,46534,4675]]
df1 = pd.DataFrame(data, index=['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04',
                       '2022-01-05', '2022-01-06', '2022-01-07', '2022-01-08',
                       '2022-01-09', '2022-01-10', '2022-01-11', '2022-01-12',
                       '2022-01-13', '2022-01-14'], 
          columns=['col_A', 'col_B', 'col_C'])
df1.index = pd.to_datetime(df1.index)

periodicity_dict = {'1D':'daily', '1W':'weekly'}
for key in periodicity_dict:
    for col in df1.columns:
        df1[col+'_rolling']= np.nan
        for i in pd.date_range(start=df1[col].first_valid_index(), end=df1[col].last_valid_index(), freq=key):
            print(i)

But I am getting the following error:

ValueError: Of the four parameters: start, end, periods, and freq, exactly three must be specified.

I haven’t been able to figure out how to solve it.

Any suggestions?

Asked By: MathMan 99

||

Answers:

you are adding a new column as Nan by this line :

df1[col+'_rolling']= np.nan

and df1.columns is evaluated each time .
you can put that into a variable before your loop like this :

eriodicity_dict = {'1D':'daily', '1W':'weekly'}
df_columns = df1.columns
for key in periodicity_dict:
    for col in df_columns:
        df1[col+'_rolling']= np.nan
        for i in pd.date_range(start=df1[col].first_valid_index(), end=df1[col].last_valid_index(), freq=key):
            print(i)
Answered By: eshirvana