@@ -29,19 +29,35 @@ __INPUT_PRICE_SERIES_DEFAULTS = {'price': 'close',
29
29
' periods' : ' periods' , # only used by MAVP; not a price series!
30
30
}
31
31
32
+ __INPUT_ARRAYS_TYPES = [dict ]
33
+ __ARRAY_TYPES = [np.ndarray]
34
+
32
35
# allow use of pandas.DataFrame for input arrays
33
36
try :
34
37
import pandas
35
- __INPUT_ARRAYS_TYPES = ( dict , pandas.DataFrame)
36
- __ARRAY_TYPES = (np.ndarray, pandas.Series)
38
+ __INPUT_ARRAYS_TYPES.append( pandas.DataFrame)
39
+ __ARRAY_TYPES.append( pandas.Series)
37
40
__PANDAS_DATAFRAME = pandas.DataFrame
38
41
__PANDAS_SERIES = pandas.Series
39
42
except ImportError :
40
- __INPUT_ARRAYS_TYPES = (dict ,)
41
- __ARRAY_TYPES = (np.ndarray,)
42
43
__PANDAS_DATAFRAME = None
43
44
__PANDAS_SERIES = None
44
45
46
+ # allow use of polars.DataFrame for input arrays
47
+ try :
48
+ import polars
49
+ __INPUT_ARRAYS_TYPES.append(polars.DataFrame)
50
+ __ARRAY_TYPES.append(polars.Series)
51
+ __POLARS_DATAFRAME = polars.DataFrame
52
+ __POLARS_SERIES = polars.Series
53
+ except ImportError :
54
+ __POLARS_DATAFRAME = None
55
+ __POLARS_SERIES = None
56
+
57
+ __INPUT_ARRAYS_TYPES = tuple (__INPUT_ARRAYS_TYPES)
58
+ __ARRAY_TYPES = tuple (__ARRAY_TYPES)
59
+
60
+
45
61
if sys.version >= ' 3' :
46
62
47
63
def str2bytes (s ):
@@ -64,10 +80,10 @@ class Function(object):
64
80
intended to simplify using individual TALIB functions by providing a
65
81
unified interface for setting/controlling input data, setting function
66
82
parameters and retrieving results. Input data consists of a ``dict`` of
67
- ``numpy`` arrays (or a ``pandas.DataFrame``), one array for each of open,
68
- high, low, close and volume. This can be set with the set_input_arrays()
69
- method. Which keyed array(s) are used as inputs when calling the function
70
- is controlled using the input_names property.
83
+ ``numpy`` arrays (or a ``pandas.DataFrame`` or ``polars.DataFrame`` ), one
84
+ array for each of open, high, low, close and volume. This can be set with
85
+ the set_input_arrays() method. Which keyed array(s) are used as inputs when
86
+ calling the function is controlled using the input_names property.
71
87
72
88
This class gets initialized with a TALIB function name and optionally an
73
89
input_arrays object. It provides the following primary functions for
@@ -334,6 +350,13 @@ class Function(object):
334
350
return __PANDAS_DATAFRAME(numpy.column_stack(ret),
335
351
index = index,
336
352
columns = self .output_names)
353
+ elif __POLARS_DATAFRAME is not None and \
354
+ isinstance (self .__input_arrays, __POLARS_DATAFRAME):
355
+ if len (ret) == 1 :
356
+ return __POLARS_SERIES(ret[0 ])
357
+ else :
358
+ return __POLARS_DATAFRAME(numpy.column_stack(ret),
359
+ columns = self .output_names)
337
360
else :
338
361
return ret[0 ] if len (ret) == 1 else ret
339
362
@@ -382,6 +405,9 @@ class Function(object):
382
405
if __PANDAS_DATAFRAME is not None \
383
406
and isinstance (self .__input_arrays, __PANDAS_DATAFRAME):
384
407
no_existing_input_arrays = self .__input_arrays.empty
408
+ elif __POLARS_DATAFRAME is not None \
409
+ and isinstance (self .__input_arrays, __POLARS_DATAFRAME):
410
+ no_existing_input_arrays = self .__input_arrays.empty
385
411
else :
386
412
no_existing_input_arrays = not bool (self .__input_arrays)
387
413
@@ -432,6 +458,9 @@ class Function(object):
432
458
if __PANDAS_SERIES is not None and \
433
459
isinstance (series, __PANDAS_SERIES):
434
460
series = series.values.astype(float )
461
+ elif __POLARS_SERIES is not None and \
462
+ isinstance (series, __POLARS_SERIES):
463
+ series = series.to_numpy().astype(float )
435
464
args.append(series)
436
465
for opt_input in self .__opt_inputs:
437
466
value = self .__get_opt_input_value(opt_input)
0 commit comments