@@ -29,19 +29,35 @@ __INPUT_PRICE_SERIES_DEFAULTS = {'price': 'close',
2929 ' periods' : ' periods' , # only used by MAVP; not a price series!
3030 }
3131
32+ __INPUT_ARRAYS_TYPES = [dict ]
33+ __ARRAY_TYPES = [np.ndarray]
34+
3235# allow use of pandas.DataFrame for input arrays
3336try :
3437 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)
3740 __PANDAS_DATAFRAME = pandas.DataFrame
3841 __PANDAS_SERIES = pandas.Series
3942except ImportError :
40- __INPUT_ARRAYS_TYPES = (dict ,)
41- __ARRAY_TYPES = (np.ndarray,)
4243 __PANDAS_DATAFRAME = None
4344 __PANDAS_SERIES = None
4445
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+
4561if sys.version >= ' 3' :
4662
4763 def str2bytes (s ):
@@ -64,10 +80,10 @@ class Function(object):
6480 intended to simplify using individual TALIB functions by providing a
6581 unified interface for setting/controlling input data, setting function
6682 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.
7187
7288 This class gets initialized with a TALIB function name and optionally an
7389 input_arrays object. It provides the following primary functions for
@@ -334,6 +350,13 @@ class Function(object):
334350 return __PANDAS_DATAFRAME(numpy.column_stack(ret),
335351 index = index,
336352 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)
337360 else :
338361 return ret[0 ] if len (ret) == 1 else ret
339362
@@ -382,6 +405,9 @@ class Function(object):
382405 if __PANDAS_DATAFRAME is not None \
383406 and isinstance (self .__input_arrays, __PANDAS_DATAFRAME):
384407 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
385411 else :
386412 no_existing_input_arrays = not bool (self .__input_arrays)
387413
@@ -432,6 +458,9 @@ class Function(object):
432458 if __PANDAS_SERIES is not None and \
433459 isinstance (series, __PANDAS_SERIES):
434460 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 )
435464 args.append(series)
436465 for opt_input in self .__opt_inputs:
437466 value = self .__get_opt_input_value(opt_input)
0 commit comments