business-science / pytimetk

Time series easier, faster, more fun. Pytimetk.
https://business-science.github.io/pytimetk/
MIT License
696 stars 60 forks source link

augment_spline API #300

Open marcozanotti opened 2 months ago

marcozanotti commented 2 months ago

Hi @mdancho84 ,

thanks for this amazing porting of timetk in python. I would suggest a new API called augment_spline to add Basis Splines / Natural Splines / Cyclic Splines / etc. to the dataframe as new columns.

Here is a simple example I did using statsmodels.gam.api module

@pf.register_dataframe_method
def augment_bsplines(data, column_name = 'ds_index_num', df = 5, degree = 3):

    bs = BSplines(
        data[column_name], 
        df = df, 
        degree = degree, 
        include_intercept = False
    )
    bs_df = pd.DataFrame(bs.basis)

    col_names = []
    for i in range(1, (len(bs_df.columns) + 1)):
        col_names.append(f'bspline_{i}_degree_{degree}')
    bs_df.columns = col_names
    bs_df.index = data.index

    data_splines = pd.concat([data, bs_df], axis = 1)

    return data_splines

It is far from ideal, but it is a quick way of doing it. Thatnks a lot. Looking forward to have all the timetk APIs in python too.

mdancho84 commented 2 months ago

Will take a look during the next pytimetk dev sprint.