-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_boolean_indexing_fix.py
More file actions
103 lines (82 loc) · 4.59 KB
/
test_boolean_indexing_fix.py
File metadata and controls
103 lines (82 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
"""
Simple test to verify the boolean indexing fix in the trading simulator.
This test creates a minimal scenario to reproduce the original error and verify the fix.
"""
import sys
import os
# Add src to path
sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
def test_boolean_indexing_fix():
"""Test the specific boolean indexing scenario that was causing errors."""
print("Testing Boolean Indexing Fix")
print("=" * 50)
try:
# Create test data similar to what was causing the error
# Market data with datetime index
start_time = datetime(2024, 1, 1, 9, 0)
market_times = [start_time + timedelta(minutes=i) for i in range(100)]
market_data = pd.DataFrame({
'price': np.random.randn(100).cumsum() + 100,
'volume': np.random.randint(1000, 10000, 100)
}, index=pd.DatetimeIndex(market_times))
# Signals data with some overlapping and some non-overlapping times
signal_times = [start_time + timedelta(minutes=i*5) for i in range(25)] # Every 5 minutes
signals_df = pd.DataFrame({
'signal': np.random.choice([1, -1, 0], 25)
}, index=pd.DatetimeIndex(signal_times))
print(f"Market data range: {market_data.index.min()} to {market_data.index.max()}")
print(f"Signals data range: {signals_df.index.min()} to {signals_df.index.max()}")
# Test the OLD way (that was causing the error)
print("\nTesting OLD approach (should work fine in isolation)...")
all_relevant_timestamps_old = pd.Index(market_data.index.tolist() + signals_df.index.tolist()).unique().sort_values()
# This is the problematic line that was causing the error in complex scenarios
try:
# This might work in simple cases but fails in complex boolean operations
filtered_old = all_relevant_timestamps_old[
(all_relevant_timestamps_old >= market_data.index.min()) &
(all_relevant_timestamps_old <= market_data.index.max())
]
print(f"✓ OLD approach worked: {len(filtered_old)} timestamps")
except Exception as e:
print(f"✗ OLD approach failed: {e}")
# Test the NEW way (our fix)
print("\nTesting NEW approach (our fix)...")
all_relevant_timestamps_new = pd.Index(market_data.index.tolist() + signals_df.index.tolist()).unique().sort_values()
# Our fix: separate the mask creation from indexing
min_time = market_data.index.min()
max_time = market_data.index.max()
mask = (all_relevant_timestamps_new >= min_time) & (all_relevant_timestamps_new <= max_time)
filtered_new = all_relevant_timestamps_new[mask]
print(f"✓ NEW approach worked: {len(filtered_new)} timestamps")
# Test with more complex scenario that would cause the original error
print("\nTesting complex scenario (where original error occurred)...")
# Create a scenario with misaligned indices that would cause the error
complex_market_data = pd.DataFrame({
'price': np.random.randn(50).cumsum() + 100,
}, index=pd.DatetimeIndex([start_time + timedelta(minutes=i*2) for i in range(50)]))
complex_signals = pd.DataFrame({
'signal': np.random.choice([1, -1, 0], 30),
}, index=pd.DatetimeIndex([start_time + timedelta(minutes=i*3) for i in range(30)]))
# This scenario with our NEW approach
all_timestamps_complex = pd.Index(complex_market_data.index.tolist() + complex_signals.index.tolist()).unique().sort_values()
min_time_complex = complex_market_data.index.min()
max_time_complex = complex_market_data.index.max()
mask_complex = (all_timestamps_complex >= min_time_complex) & (all_timestamps_complex <= max_time_complex)
filtered_complex = all_timestamps_complex[mask_complex]
print(f"✓ Complex scenario handled: {len(filtered_complex)} timestamps")
print("\n" + "=" * 50)
print("✓ Boolean indexing fix test PASSED!")
print("The fix successfully handles the problematic boolean indexing scenario.")
return True
except Exception as e:
print(f"\n✗ Boolean indexing fix test FAILED: {e}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
success = test_boolean_indexing_fix()
sys.exit(0 if success else 1)