Skip to content

Commit

Permalink
finalize sorting method, add tests #290 #286
Browse files Browse the repository at this point in the history
  • Loading branch information
mdancho84 committed Jul 17, 2024
1 parent 9a33256 commit 86951fe
Show file tree
Hide file tree
Showing 20 changed files with 172 additions and 21 deletions.
3 changes: 1 addition & 2 deletions src/pytimetk/core/anomalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,7 @@ def anomalize(

if reduce_memory:
result = reduce_memory_usage(result)

result.index = idx_unsorted

result = result.sort_index()

return result
Expand Down
6 changes: 5 additions & 1 deletion src/pytimetk/feature_engineering/diffs.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,17 @@ def augment_diffs(
ret = _augment_diffs_pandas(data, date_column, value_column, periods, normalize=normalize)
elif engine == 'polars':
ret = _augment_diffs_polars(data, date_column, value_column, periods, normalize=normalize)

# Polars Index to Match Pandas
ret.index = idx_unsorted

else:
raise ValueError("Invalid engine. Use 'pandas' or 'polars'.")

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
# Sort index
ret = ret.sort_index()

return ret
Expand Down
5 changes: 4 additions & 1 deletion src/pytimetk/feature_engineering/expanding.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,16 @@ def augment_expanding(
min_periods,
**kwargs
)

# Polars Index to Match Pandas
ret.index = idx_unsorted

else:
raise ValueError("Invalid engine. Use 'pandas' or 'polars'.")

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret
Expand Down
4 changes: 3 additions & 1 deletion src/pytimetk/feature_engineering/fourier.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,13 @@ def augment_fourier(
ret = _augment_fourier_pandas(data, date_column, periods, max_order)
else:
ret = _augment_fourier_polars(data, date_column, periods, max_order)

# Polars Index to Match Pandas
ret.index = idx_unsorted

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret
Expand Down
3 changes: 2 additions & 1 deletion src/pytimetk/feature_engineering/hilbert.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,14 @@ def augment_hilbert(
ret = _augment_hilbert_pandas(data, date_column, value_column)
elif engine == 'polars':
ret = _augment_hilbert_polars(data, date_column, value_column)
# Polars Index to Match Pandas
ret.index = idx_unsorted
else:
raise ValueError("Invalid engine. Use 'pandas' or 'polars'.")

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret
Expand Down
3 changes: 2 additions & 1 deletion src/pytimetk/feature_engineering/holiday_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,14 @@ def augment_holiday_signature(
ret = _augment_holiday_signature_pandas(data, date_column, country_name)
elif engine == 'polars':
ret = _augment_holiday_signature_polars(data, date_column, country_name)
# Polars Index to Match Pandas
ret.index = idx_unsorted
else:
raise ValueError("Invalid engine. Use 'pandas' or 'polars'.")

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret
Expand Down
3 changes: 2 additions & 1 deletion src/pytimetk/feature_engineering/lags.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,14 @@ def augment_lags(
ret = _augment_lags_pandas(data, date_column, value_column, lags)
elif engine == 'polars':
ret = _augment_lags_polars(data, date_column, value_column, lags)
# Polars Index to Match Pandas
ret.index = idx_unsorted
else:
raise ValueError("Invalid engine. Use 'pandas' or 'polars'.")

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret
Expand Down
3 changes: 2 additions & 1 deletion src/pytimetk/feature_engineering/leads.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,14 @@ def augment_leads(
ret = _augment_leads_pandas(data, date_column, value_column, leads)
elif engine == 'polars':
ret = _augment_leads_polars(data, date_column, value_column, leads)
# Polars Index to Match Pandas
ret.index = idx_unsorted
else:
raise ValueError("Invalid engine. Use 'pandas' or 'polars'.")

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret
Expand Down
5 changes: 4 additions & 1 deletion src/pytimetk/feature_engineering/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,16 @@ def augment_rolling(
show_progress,
**kwargs
)

# Polars Index to Match Pandas
ret.index = idx_unsorted

else:
raise ValueError("Invalid engine. Use 'pandas' or 'polars'.")

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret
Expand Down
1 change: 0 additions & 1 deletion src/pytimetk/feature_engineering/wavelet.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ def _apply_cwt(df):
if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret
Expand Down
3 changes: 2 additions & 1 deletion src/pytimetk/finance/atr.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,14 @@ def augment_atr(
ret = _augment_atr_pandas(data, date_column, high_column, low_column, close_column, periods, normalize)
elif engine == 'polars':
ret = _augment_atr_polars(data, date_column, high_column, low_column, close_column, periods, normalize)
# Polars Index to Match Pandas
ret.index = idx_unsorted
else:
raise ValueError("Invalid engine. Use 'pandas' or 'polars'.")

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret
Expand Down
3 changes: 2 additions & 1 deletion src/pytimetk/finance/bbands.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,14 @@ def augment_bbands(
ret = _augment_bbands_pandas(data, date_column, close_column, periods, std_dev)
elif engine == 'polars':
ret = _augment_bbands_polars(data, date_column, close_column, periods, std_dev)
# Polars Index to Match Pandas
ret.index = idx_unsorted
else:
raise ValueError("Invalid engine. Use 'pandas' or 'polars'.")

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret
Expand Down
3 changes: 2 additions & 1 deletion src/pytimetk/finance/cmo.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,14 @@ def augment_cmo(
ret = _augment_cmo_pandas(data, date_column, close_column, periods)
elif engine == 'polars':
ret = _augment_cmo_polars(data, date_column, close_column, periods)
# Polars Index to Match Pandas
ret.index = idx_unsorted
else:
raise ValueError("Invalid engine. Use 'pandas' or 'polars'.")

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret
Expand Down
3 changes: 2 additions & 1 deletion src/pytimetk/finance/macd.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,14 @@ def augment_macd(
ret = _augment_macd_pandas(data, date_column, close_column, fast_period, slow_period, signal_period)
elif engine == 'polars':
ret = _augment_macd_polars(data, date_column, close_column, fast_period, slow_period, signal_period)
# Polars Index to Match Pandas
ret.index = idx_unsorted
else:
raise ValueError("Invalid engine. Use 'pandas' or 'polars'.")

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret
Expand Down
3 changes: 2 additions & 1 deletion src/pytimetk/finance/ppo.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,14 @@ def augment_ppo(
ret = _augment_ppo_pandas(data, date_column, close_column, fast_period, slow_period)
elif engine == 'polars':
ret = _augment_ppo_polars(data, date_column, close_column, fast_period, slow_period)
# Polars Index to Match Pandas
ret.index = idx_unsorted
else:
raise ValueError("Invalid engine. Use 'pandas' or 'polars'.")

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret
Expand Down
7 changes: 4 additions & 3 deletions src/pytimetk/finance/qsmomentum.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,14 @@ def f(close):
)

ret.rename(columns={ret.columns[-1]: f'{close_column}_qsmom_{fp}_{sp}_{np}'}, inplace=True)



# if engine == 'polars':
# # Polars Index to Match Pandas
# ret.index = idx_unsorted

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret
Expand Down
3 changes: 2 additions & 1 deletion src/pytimetk/finance/roc.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,14 @@ def augment_roc(
ret = _augment_roc_pandas(data, date_column, close_column, periods, start_index=start_index)
elif engine == 'polars':
ret = _augment_roc_polars(data, date_column, close_column, periods, start_index=start_index)
# Polars Index to Match Pandas
ret.index = idx_unsorted
else:
raise ValueError("Invalid engine. Use 'pandas' or 'polars'.")

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret
Expand Down
3 changes: 2 additions & 1 deletion src/pytimetk/finance/rsi.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,14 @@ def augment_rsi(
ret = _augment_rsi_pandas(data, date_column, close_column, periods)
elif engine == 'polars':
ret = _augment_rsi_polars(data, date_column, close_column, periods)
# Polars Index to Match Pandas
ret.index = idx_unsorted
else:
raise ValueError("Invalid engine. Use 'pandas' or 'polars'.")

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret
Expand Down
3 changes: 3 additions & 0 deletions tests/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def test_example_1_polars():
window = [2,7], # Specifying multiple window sizes
window_func = [
'mean', # Built-in mean function
# 'std'
('std', lambda x: x.std()) # Lambda function to compute standard deviation
],
engine = 'polars' # Using pandas engine
Expand Down Expand Up @@ -211,6 +212,8 @@ def test_sort_pandas_lambda():
stocks_df = tk.load_dataset("stocks_daily")
stocks_df['date'] = pd.to_datetime(stocks_df['date'])

# stocks_df = stocks_df.sort_values('date').reset_index()

rolled_df_pandas_fast = stocks_df[['symbol','date','adjusted']] \
.groupby('symbol') \
.augment_rolling(
Expand Down
Loading

0 comments on commit 86951fe

Please sign in to comment.