From 97cbea25140a62989341f018ca16078988a334e7 Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Tue, 19 Feb 2013 11:20:04 -0500 Subject: [PATCH] Fixes crashes when using numpy log on a batch transform data panel. The recent change to the creation of the data panel ended up with a panel with the dtype of 'object', which was causing numpy ufuncs like `log` to crash out on an `AttributeError`. This forces all frames in the panel to use a dtype of 'float', we may want to look at seeting a dtype on a frame by frame basis, e.g. 'volume' may more accurately be 'int'. --- zipline/test_algorithms.py | 13 +++++++++++++ zipline/transforms/utils.py | 3 ++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/zipline/test_algorithms.py b/zipline/test_algorithms.py index 64d85d88..9dd896ab 100644 --- a/zipline/test_algorithms.py +++ b/zipline/test_algorithms.py @@ -267,6 +267,12 @@ def return_data(data, *args, **kwargs): return data +@batch_transform +def uses_ufunc(data, *args, **kwargs): + # ufuncs like np.log should not crash + return np.log(data) + + class BatchTransformAlgorithm(TradingAlgorithm): def initialize(self, *args, **kwargs): self.refresh_period = kwargs.pop('refresh_period', 1) @@ -348,6 +354,12 @@ class BatchTransformAlgorithm(TradingAlgorithm): compute_only_full=False ) + self.uses_ufunc = uses_ufunc( + refresh_period=self.refresh_period, + window_length=self.window_length, + clean_nans=False + ) + self.iter = 0 self.set_slippage(FixedSlippage()) @@ -364,6 +376,7 @@ class BatchTransformAlgorithm(TradingAlgorithm): self.return_ticks.handle_data(data)) self.history_return_not_full.append( self.return_not_full.handle_data(data)) + self.uses_ufunc.handle_data(data) new_data = deepcopy(data) for sid in new_data: diff --git a/zipline/transforms/utils.py b/zipline/transforms/utils.py index 11e7cece..9be939a6 100644 --- a/zipline/transforms/utils.py +++ b/zipline/transforms/utils.py @@ -472,7 +472,8 @@ class BatchTransform(EventWindow): # user-overloaded get_value() method. data_dict = {tick['dt']: tick['data'] for tick in self.ticks} data = pd.Panel(data_dict, major_axis=self.field_names, - minor_axis=self.sids) + minor_axis=self.sids, + dtype='float') if self.supplemental_data: # item will be a date stamp