1
+ import warnings
1
2
from typing import Sequence
2
3
from numbers import Number
3
4
@@ -68,6 +69,11 @@ def __float__(self):
68
69
return super ().__float__ ()
69
70
70
71
def to_series (self ):
72
+ warnings .warn ("`.to_series()` is deprecated. For pd.Series conversion, use accessor `.s`" )
73
+ return self .s
74
+
75
+ @property
76
+ def s (self ) -> pd .Series :
71
77
return pd .Series (self , index = self ._opts ['data' ].index , name = self .name )
72
78
73
79
@@ -82,15 +88,13 @@ class _Data:
82
88
and the returned "series" are _not_ `pd.Series` but `np.ndarray`
83
89
for performance reasons.
84
90
"""
85
- def __init__ (self , df ):
91
+ def __init__ (self , df : pd .DataFrame ):
92
+ self .__df = df
86
93
self .__i = len (df )
87
94
self .__pip = None
88
95
self .__cache = {}
89
-
90
- self .__arrays = {col : _Array (arr , data = self )
91
- for col , arr in df .items ()}
92
- # Leave index as Series because pd.Timestamp nicer API to work with
93
- self .__arrays ['__index' ] = df .index .copy ()
96
+ self .__arrays = None
97
+ self ._update_arrays ()
94
98
95
99
def __getitem__ (self , item ):
96
100
return getattr (self , item )
@@ -105,17 +109,29 @@ def _set_length(self, i):
105
109
self .__i = i
106
110
self .__cache .clear ()
107
111
112
+ def _update_arrays (self ):
113
+ self .__arrays = {col : _Array (arr , data = self )
114
+ for col , arr in self .__df .items ()}
115
+ # Leave index as Series because pd.Timestamp nicer API to work with
116
+ self .__arrays ['__index' ] = self .__df .index .copy ()
117
+
108
118
def __len__ (self ):
109
119
return self .__i
110
120
121
+ @property
122
+ def df (self ) -> pd .DataFrame :
123
+ return (self .__df .iloc [:self .__i ]
124
+ if self .__i < len (self .__df )
125
+ else self .__df )
126
+
111
127
@property
112
128
def pip (self ):
113
129
if self .__pip is None :
114
130
self .__pip = 10 ** - np .median ([len (s .partition ('.' )[- 1 ])
115
131
for s in self .__arrays ['Close' ].astype (str )])
116
132
return self .__pip
117
133
118
- def __get_array (self , key ):
134
+ def __get_array (self , key ) -> _Array :
119
135
arr = self .__cache .get (key )
120
136
if arr is None :
121
137
arr = self .__cache [key ] = self .__arrays [key ][:self .__i ]
@@ -142,8 +158,8 @@ def Volume(self):
142
158
return self .__get_array ('Volume' )
143
159
144
160
@property
145
- def index (self ):
146
- return self .__get_array ('__index' )
161
+ def index (self ) -> pd . Index :
162
+ return self .__get_array ('__index' ) # type: ignore
147
163
148
164
# Make pickling in Backtest.optimize() work with our catch-all __getattr__
149
165
def __getstate__ (self ):
0 commit comments