Skip to content

Commit

Permalink
#216 - Add tests - augment_expanding still failing
Browse files Browse the repository at this point in the history
  • Loading branch information
mdancho84 committed Nov 5, 2023
1 parent a134a07 commit c809f88
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 1 deletion.
103 changes: 102 additions & 1 deletion tests/test_expanding.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,105 @@ def test_example_3_parallel():
assert expanded_df.shape[1] == 4
assert expanded_df.shape[0] == df.shape[0]



def test_sort_pandas():

import pandas as pd
import pytimetk as tk
import numpy as np

stocks_df = tk.load_dataset("stocks_daily")
stocks_df['date'] = pd.to_datetime(stocks_df['date'])

result = stocks_df[['symbol','date','adjusted']] \
.groupby('symbol') \
.augment_expanding(
date_column = 'date',
value_column = 'adjusted',
window_func = 'max',
engine = 'pandas',
show_progress = False
)

result = result.groupby('symbol').apply(lambda x: x.tail(1))

result['test'] = np.abs(result['adjusted'] - result['adjusted_expanding_max']) / result['adjusted']

assert result['test'].mean() < 0.35


def test_sort_pandas_lambda():

import pandas as pd
import pytimetk as tk
import numpy as np

stocks_df = tk.load_dataset("stocks_daily")
stocks_df['date'] = pd.to_datetime(stocks_df['date'])

result = stocks_df[['symbol','date','adjusted']] \
.groupby('symbol') \
.augment_expanding(
date_column = 'date',
value_column = 'adjusted',
window_func = ('max', lambda x: x.max()),
engine = 'pandas',
show_progress = False
)

result = result.groupby('symbol').apply(lambda x: x.tail(1))

result['test'] = np.abs(result['adjusted'] - result['adjusted_expanding_max']) / result['adjusted']

assert result['test'].mean() < 0.35

def test_sort_pandas_parallel():

import pandas as pd
import pytimetk as tk
import numpy as np

stocks_df = tk.load_dataset("stocks_daily")
stocks_df['date'] = pd.to_datetime(stocks_df['date'])

result = stocks_df[['symbol','date','adjusted']] \
.groupby('symbol') \
.augment_expanding(
date_column = 'date',
value_column = 'adjusted',
window_func = 'max',
engine = 'pandas',
show_progress = False,
threads = 2,
)

result = result.groupby('symbol').apply(lambda x: x.tail(1))

result['test'] = np.abs(result['adjusted'] - result['adjusted_expanding_max']) / result['adjusted']

assert result['test'].mean() < 0.35

def test_sort_polars():

import pandas as pd
import pytimetk as tk
import numpy as np

stocks_df = tk.load_dataset("stocks_daily")
stocks_df['date'] = pd.to_datetime(stocks_df['date'])

result = stocks_df[['symbol','date','adjusted']] \
.groupby('symbol') \
.augment_expanding(
date_column = 'date',
value_column = 'adjusted',
window_func = 'max',
engine = 'polars',
show_progress = False
)

result = result.groupby('symbol').apply(lambda x: x.tail(1))

result['test'] = np.abs(result['adjusted'] - result['adjusted_expanding_max']) / result['adjusted']

assert result['test'].mean() < 0.35
107 changes: 107 additions & 0 deletions tests/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,110 @@ def test_example_1_polars():

assert rolled_df.shape[1] == 7
assert rolled_df.shape[0] == df.shape[0]


def test_sort_pandas():

import pandas as pd
import pytimetk as tk
import numpy as np

stocks_df = tk.load_dataset("stocks_daily")
stocks_df['date'] = pd.to_datetime(stocks_df['date'])

rolled_df_pandas_fast = stocks_df[['symbol','date','adjusted']] \
.groupby('symbol') \
.augment_rolling(
date_column = 'date',
value_column = 'adjusted',
window = [20],
window_func = 'mean',
engine = 'pandas',
show_progress = False
)

result = rolled_df_pandas_fast.groupby('symbol').apply(lambda x: x.tail(1))

result['test'] = np.abs(result['adjusted'] - result['adjusted_rolling_mean_win_20']) / result['adjusted']

assert result['test'].mean() < 0.10


def test_sort_pandas_lambda():

import pandas as pd
import pytimetk as tk
import numpy as np

stocks_df = tk.load_dataset("stocks_daily")
stocks_df['date'] = pd.to_datetime(stocks_df['date'])

rolled_df_pandas_fast = stocks_df[['symbol','date','adjusted']] \
.groupby('symbol') \
.augment_rolling(
date_column = 'date',
value_column = 'adjusted',
window = [20],
window_func = ('mean', lambda x: x.mean()),
engine = 'pandas',
show_progress = False
)

result = rolled_df_pandas_fast.groupby('symbol').apply(lambda x: x.tail(1))

result['test'] = np.abs(result['adjusted'] - result['adjusted_rolling_mean_win_20']) / result['adjusted']

assert result['test'].mean() < 0.10

def test_sort_pandas_parallel():

import pandas as pd
import pytimetk as tk
import numpy as np

stocks_df = tk.load_dataset("stocks_daily")
stocks_df['date'] = pd.to_datetime(stocks_df['date'])

rolled_df_pandas_fast = stocks_df[['symbol','date','adjusted']] \
.groupby('symbol') \
.augment_rolling(
date_column = 'date',
value_column = 'adjusted',
window = [20],
window_func = 'mean',
engine = 'pandas',
show_progress = False,
threads = 2
)

result = rolled_df_pandas_fast.groupby('symbol').apply(lambda x: x.tail(1))

result['test'] = np.abs(result['adjusted'] - result['adjusted_rolling_mean_win_20']) / result['adjusted']

assert result['test'].mean() < 0.10

def test_sort_polars():

import pandas as pd
import pytimetk as tk
import numpy as np

stocks_df = tk.load_dataset("stocks_daily")
stocks_df['date'] = pd.to_datetime(stocks_df['date'])

rolled_df_pandas_fast = stocks_df[['symbol','date','adjusted']] \
.groupby('symbol') \
.augment_rolling(
date_column = 'date',
value_column = 'adjusted',
window = [20],
window_func = 'mean',
engine = 'polars',
show_progress = False
)

result = rolled_df_pandas_fast.groupby('symbol').apply(lambda x: x.tail(1))

result['test'] = np.abs(result['adjusted'] - result['adjusted_rolling_mean_win_20']) / result['adjusted']

assert result['test'].mean() < 0.10

0 comments on commit c809f88

Please sign in to comment.