Skip to content

Commit 33d0de2

Browse files
Apply ruff code formatter
1 parent 2245377 commit 33d0de2

37 files changed

Lines changed: 3026 additions & 1341 deletions

tests/test_add_lagged_columns.py

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ def test_exact_lag_with_by_returns_correct_lagged_values():
1818
data = pd.DataFrame(
1919
{
2020
"permno": [1] * 4 + [2] * 4,
21-
"date": list(
22-
pd.date_range("2023-01-01", periods=4, freq="MS")
23-
) * 2,
21+
"date": list(pd.date_range("2023-01-01", periods=4, freq="MS")) * 2,
2422
"size": [float(i) for i in range(1, 9)],
2523
}
2624
)
@@ -41,9 +39,7 @@ def test_exact_lag_without_by_returns_correct_values():
4139
"size": [1.0, 2.0, 3.0],
4240
}
4341
)
44-
result = add_lagged_columns(
45-
data, cols="size", lag=pd.DateOffset(months=1)
46-
)
42+
result = add_lagged_columns(data, cols="size", lag=pd.DateOffset(months=1))
4743
assert pd.isna(result["size_lag"].iloc[0])
4844
assert result["size_lag"].iloc[1] == 1
4945
assert result["size_lag"].iloc[2] == 2
@@ -53,9 +49,7 @@ def test_window_lag_handles_all_src_date_conditions():
5349
"""Test window lag: NA, in-window, and below-lower-bound cases."""
5450
data = pd.DataFrame(
5551
{
56-
"date": pd.to_datetime(
57-
["2023-01-01", "2023-02-01", "2023-06-01"]
58-
),
52+
"date": pd.to_datetime(["2023-01-01", "2023-02-01", "2023-06-01"]),
5953
"size": [1.0, 2.0, 3.0],
6054
}
6155
)
@@ -104,9 +98,7 @@ def test_ff_adjustment_without_by_uses_year_grouping_only():
10498
"""Test ff_adjustment without by uses year grouping only."""
10599
data = pd.DataFrame(
106100
{
107-
"date": pd.to_datetime(
108-
["2022-06-01", "2022-12-01", "2023-06-01"]
109-
),
101+
"date": pd.to_datetime(["2022-06-01", "2022-12-01", "2023-06-01"]),
110102
"size": [10.0, 20.0, 30.0],
111103
}
112104
)
@@ -152,18 +144,14 @@ def test_error_when_date_column_is_absent_from_data():
152144

153145
def test_error_when_lag_is_negative():
154146
"""Test error when lag is negative."""
155-
data = pd.DataFrame(
156-
{"date": [pd.Timestamp("2023-01-01")], "size": [1.0]}
157-
)
147+
data = pd.DataFrame({"date": [pd.Timestamp("2023-01-01")], "size": [1.0]})
158148
with pytest.raises(ValueError, match="non-negative"):
159149
add_lagged_columns(data, cols="size", lag=-1)
160150

161151

162152
def test_error_when_max_lag_is_less_than_lag():
163153
"""Test error when max_lag is less than lag."""
164-
data = pd.DataFrame(
165-
{"date": [pd.Timestamp("2023-01-01")], "size": [1.0]}
166-
)
154+
data = pd.DataFrame({"date": [pd.Timestamp("2023-01-01")], "size": [1.0]})
167155
with pytest.raises(ValueError, match="max_lag"):
168156
add_lagged_columns(
169157
data,
@@ -175,9 +163,7 @@ def test_error_when_max_lag_is_less_than_lag():
175163

176164
def test_error_when_requested_column_is_absent_from_data():
177165
"""Test error when a requested column is absent from data."""
178-
data = pd.DataFrame(
179-
{"date": [pd.Timestamp("2023-01-01")], "size": [1.0]}
180-
)
166+
data = pd.DataFrame({"date": [pd.Timestamp("2023-01-01")], "size": [1.0]})
181167
with pytest.raises(ValueError, match="missing"):
182168
add_lagged_columns(
183169
data, cols="no_such_col", lag=pd.DateOffset(months=1)
@@ -186,9 +172,7 @@ def test_error_when_requested_column_is_absent_from_data():
186172

187173
def test_error_when_by_column_is_absent_from_data():
188174
"""Test error when a by column is absent from data."""
189-
data = pd.DataFrame(
190-
{"date": [pd.Timestamp("2023-01-01")], "size": [1.0]}
191-
)
175+
data = pd.DataFrame({"date": [pd.Timestamp("2023-01-01")], "size": [1.0]})
192176
with pytest.raises(ValueError, match="missing"):
193177
add_lagged_columns(
194178
data,
@@ -207,9 +191,7 @@ def test_error_when_join_key_is_not_unique():
207191
}
208192
)
209193
with pytest.raises(ValueError, match="unique"):
210-
add_lagged_columns(
211-
data, cols="size", lag=pd.DateOffset(months=1)
212-
)
194+
add_lagged_columns(data, cols="size", lag=pd.DateOffset(months=1))
213195

214196

215197
def test_error_when_upper_helper_column_already_exists():
@@ -233,6 +215,7 @@ def test_error_when_upper_helper_column_already_exists():
233215
def test_data_options_specifies_date_column_name():
234216
"""Test data_options dict specifies the date column name."""
235217
from tidyfinance.core import data_options
218+
236219
data = pd.DataFrame(
237220
{
238221
"my_date": pd.date_range("2023-01-01", periods=3, freq="MS"),
@@ -249,6 +232,7 @@ def test_data_options_specifies_date_column_name():
249232
assert pd.isna(result["size_lag"].iloc[0])
250233
assert result["size_lag"].iloc[1] == 1
251234

235+
252236
if __name__ == "__main__":
253237
# Run all tests
254238
pytest.main([__file__])

tests/test_assign_portfolio.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
from tidyfinance.core import assign_portfolio # noqa: E402
1616

17-
1817
# %% mock breakpoint functions
1918

2019

@@ -31,9 +30,7 @@ def mock_breakpoint_percentiles(
3130
data, sorting_variable, bp_options=None, data_options=None
3231
):
3332
"""Percentile-based mock breakpoint function."""
34-
percentiles = (bp_options or {}).get(
35-
"percentiles", [0.2, 0.4, 0.6, 0.8]
36-
)
33+
percentiles = (bp_options or {}).get("percentiles", [0.2, 0.4, 0.6, 0.8])
3734
probs = [0] + list(percentiles) + [1]
3835
return np.quantile(data[sorting_variable].dropna().values, probs)
3936

@@ -156,9 +153,7 @@ def test_constant_sorting_variable_returns_vector_of_correct_length():
156153

157154
def test_two_distinct_values_with_2_portfolios_produces_two_groups():
158155
"""Test two distinct values with 2 portfolios produces two groups."""
159-
data = pd.DataFrame(
160-
{"id": range(100), "value": [1] * 50 + [2] * 50}
161-
)
156+
data = pd.DataFrame({"id": range(100), "value": [1] * 50 + [2] * 50})
162157
result = assign_portfolio(
163158
data,
164159
"value",
@@ -189,9 +184,7 @@ def test_single_row_data_frame_with_constant_variable_triggers_warning():
189184

190185
def test_warning_when_clusters_reduce_number_of_portfolios():
191186
"""Test warning is issued when clusters reduce the number of portfolios."""
192-
data = pd.DataFrame(
193-
{"id": range(100), "value": [1] * 50 + [100] * 50}
194-
)
187+
data = pd.DataFrame({"id": range(100), "value": [1] * 50 + [100] * 50})
195188

196189
def mock_bp_5(data, sv, bp_options=None, data_options=None):
197190
return np.array([1, 20, 40, 60, 80, 100])
@@ -335,9 +328,7 @@ def test_function_works_with_large_datasets():
335328
"""Test function works with large datasets."""
336329
rng = np.random.default_rng(1)
337330
n = 100_000
338-
data = pd.DataFrame(
339-
{"id": np.arange(n), "value": rng.standard_normal(n)}
340-
)
331+
data = pd.DataFrame({"id": np.arange(n), "value": rng.standard_normal(n)})
341332
result = assign_portfolio(
342333
data,
343334
"value",
@@ -416,9 +407,7 @@ def test_percentile_based_breakpoints_produce_correct_number_of_groups():
416407
def test_two_portfolios_split_data_roughly_in_half():
417408
"""Test two portfolios split data roughly in half."""
418409
rng = np.random.default_rng(10)
419-
data = pd.DataFrame(
420-
{"id": range(1000), "value": rng.standard_normal(1000)}
421-
)
410+
data = pd.DataFrame({"id": range(1000), "value": rng.standard_normal(1000)})
422411
result = assign_portfolio(
423412
data,
424413
"value",
@@ -444,9 +433,7 @@ def spy_bp(data, sorting_variable, bp_options=None, data_options=None):
444433
received["data_options"] = data_options
445434
return np.array([0.0, 0.5, 1.0])
446435

447-
data = pd.DataFrame(
448-
{"id": range(10), "value": np.linspace(0, 1, 10)}
449-
)
436+
data = pd.DataFrame({"id": range(10), "value": np.linspace(0, 1, 10)})
450437
my_bp_opts = {"n_portfolios": 2}
451438
my_data_opts = {"date": "date_col"}
452439
assign_portfolio(

tests/test_backend.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def _restore_backend():
3535

3636
# %% set_backend / get_backend
3737

38+
3839
def test_default_backend_is_pandas():
3940
assert get_backend() == "pandas"
4041

@@ -51,6 +52,7 @@ def test_set_backend_rejects_invalid_value():
5152

5253
# %% _convert_output
5354

55+
5456
def test_convert_output_passthrough_for_pandas_backend():
5557
df = pd.DataFrame({"a": [1, 2]})
5658
assert _convert_output(df) is df
@@ -92,6 +94,7 @@ def test_convert_output_leaves_dict_alone():
9294

9395
# %% _to_pandas_input
9496

97+
9598
def test_to_pandas_input_converts_polars_frame():
9699
out = _to_pandas_input(pl.DataFrame({"a": [1, 2]}))
97100
assert isinstance(out, pd.DataFrame)
@@ -109,6 +112,7 @@ def test_to_pandas_input_passes_through_pandas():
109112

110113
# %% download_data integration (network-free pseudo domain)
111114

115+
112116
def test_download_data_returns_pandas_by_default():
113117
with pytest.warns(UserWarning, match="pseudo data"):
114118
out = tf.download_data("Pseudo Data", "crsp_monthly")
@@ -124,12 +128,12 @@ def test_download_data_returns_polars_when_configured():
124128

125129
# %% core function honors the backend (input + output round-trip)
126130

131+
127132
def _lag_input():
128133
return pd.DataFrame(
129134
{
130135
"permno": [1] * 4 + [2] * 4,
131-
"date": list(pd.date_range("2023-01-01", periods=4, freq="MS"))
132-
* 2,
136+
"date": list(pd.date_range("2023-01-01", periods=4, freq="MS")) * 2,
133137
"size": [float(i) for i in range(1, 9)],
134138
}
135139
)
@@ -174,6 +178,7 @@ def test_series_returning_function_stays_pandas_under_polars():
174178

175179
# %% boundary-only wrapping protects internal cross-calls
176180

181+
177182
def test_in_module_implementations_remain_unwrapped():
178183
"""The wrapping is applied only at the public package boundary. The
179184
in-module implementations (which core functions call internally,
@@ -193,6 +198,7 @@ def test_in_module_implementations_remain_unwrapped():
193198
# argument through the polars backend without raising and produce the
194199
# expected output type.
195200

201+
196202
def _panel_with_returns():
197203
"""Five-asset, two-year panel — enough per cross-section to fit
198204
three breakpoints without ties."""
@@ -353,13 +359,9 @@ def test_process_trace_data_round_trips_polars():
353359
"cusip_id": ["00077D1AA"] * 2,
354360
"msg_seq_nb": [1, 2],
355361
"orig_msg_seq_nb": [1, 2],
356-
"trd_rpt_dt": pd.to_datetime(
357-
["2015-01-05", "2015-01-06"]
358-
),
362+
"trd_rpt_dt": pd.to_datetime(["2015-01-05", "2015-01-06"]),
359363
"trd_rpt_tm": ["09:31:00", "09:36:00"],
360-
"trd_exctn_dt": pd.to_datetime(
361-
["2015-01-04", "2015-01-05"]
362-
),
364+
"trd_exctn_dt": pd.to_datetime(["2015-01-04", "2015-01-05"]),
363365
"trd_exctn_tm": ["09:30:00", "09:35:00"],
364366
"rptd_pr": [100.0, 100.5],
365367
"entrd_vol_qt": [1000, 1500],
@@ -370,9 +372,7 @@ def test_process_trace_data_round_trips_polars():
370372
"asof_cd": [None, None],
371373
"wis_fl": ["N", "N"],
372374
"days_to_sttl_ct": [2, 2],
373-
"stlmnt_dt": pd.to_datetime(
374-
["2015-01-06", "2015-01-07"]
375-
),
375+
"stlmnt_dt": pd.to_datetime(["2015-01-06", "2015-01-07"]),
376376
"spcl_trd_fl": [None, None],
377377
}
378378
)

tests/test_breakpoint_options.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import sys
5+
56
import pytest
67

78
sys.path.insert(

0 commit comments

Comments
 (0)