Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update fourier.py #82

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/pytimetk/feature_engineering/fourier.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def date_to_seq_scale_factor(data: pd.DataFrame, date_var: str) -> pd.DataFrame:
def date_to_seq_scale_factor_polars(data: pl.DataFrame, date_var: str) -> pl.DataFrame:
return ts_summary_polars(data, date_column=date_var)['diff_median']


def _augment_fourier_pandas(
data: pd.DataFrame,
date_column: str,
Expand All @@ -42,15 +41,27 @@ def _augment_fourier_pandas(
min_date = df[date_column].min()
df['radians'] = 2 * np.pi * (df[date_column] - min_date).dt.total_seconds() / scale_factor

for type_val in ("sin", "cos"):
for K_val in range(1, max_order + 1):
for period_val in periods:
df[f'{date_column}_{type_val}_{K_val}_{period_val}'] = calc_fourier(x = df['radians'], period = period_val, type = type_val, K = K_val)

type_vals = ["sin", "cos"]
K_vals = range(1, max_order + 1)

new_cols = [
f'{date_column}_{type_val}_{K_val}_{period_val}'
for type_val in type_vals
for K_val in K_vals
for period_val in periods
]

df_new = np.array([
calc_fourier(x=df['radians'], period=period_val, type=type_val, K=K_val)
for type_val in type_vals
for K_val in K_vals
for period_val in periods
]).reshape(-1, len(df)).T

df[new_cols] = df_new
# Drop the temporary 'radians' column
return df.drop(columns=['radians'])


def _augment_fourier_polars(
data: pd.DataFrame,
date_column: str,
Expand Down Expand Up @@ -279,4 +290,4 @@ def augment_fourier(

# Monkey patch the method to pandas groupby objects
pd.core.groupby.generic.DataFrameGroupBy.augment_fourier = augment_fourier
pd.core.groupby.generic.DataFrameGroupBy.augment_fourier_v2 = augment_fourier_v2
pd.core.groupby.generic.DataFrameGroupBy.augment_fourier_v2 = augment_fourier_v2