From daf05c6b59c8f264ac0c2288ef5c221ed042550b Mon Sep 17 00:00:00 2001 From: Richard Frank Date: Wed, 20 Jan 2016 06:52:10 -0500 Subject: [PATCH 1/3] BUG: Ensure that current_sids() returns Assets instead of identifiers Also batch lookup sids in algo.run --- zipline/algorithm.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/zipline/algorithm.py b/zipline/algorithm.py index a74ee984..bf11cae6 100644 --- a/zipline/algorithm.py +++ b/zipline/algorithm.py @@ -559,15 +559,11 @@ class TradingAlgorithm(object): # The sids field of the source is the reference for the universe at # the start of the run - self._current_universe = set() - for source in self.sources: - for sid in source.sids: - self._current_universe.add(sid) + sids = {sid for source in self.sources for sid in source.sids} # Check that all sids from the source are accounted for in # the AssetFinder. This retrieve call will raise an exception if the # sid is not found. - for sid in self._current_universe: - self.asset_finder.retrieve_asset(sid) + self._current_universe = set(self.asset_finder.retrieve_all(sids)) # force a reset of the performance tracker, in case # this is a repeat run of the algorithm. From 2eeb6e78f6f308817fddefa1d8b4ef02deea49cc Mon Sep 17 00:00:00 2001 From: Richard Frank Date: Wed, 20 Jan 2016 06:53:07 -0500 Subject: [PATCH 2/3] BUG: Enable comparison of an Asset to an int64 on 32-bit python We use a number of mappings keyed by int64, which otherwise raised KeyErrors for Assets. --- tests/test_assets.py | 12 ++++++++++-- zipline/assets/_assets.pyx | 8 ++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/tests/test_assets.py b/tests/test_assets.py index 11b92d31..1f90f5ff 100644 --- a/tests/test_assets.py +++ b/tests/test_assets.py @@ -29,7 +29,7 @@ from pandas.tseries.tools import normalize_date from pandas.util.testing import assert_frame_equal from nose_parameterized import parameterized -from numpy import full +from numpy import full, int32, int64 import sqlalchemy as sa from zipline.assets import ( @@ -39,7 +39,7 @@ from zipline.assets import ( AssetFinder, AssetFinderCachedEquities, ) -from six import itervalues +from six import itervalues, integer_types from toolz import valmap from zipline.assets.futures import ( @@ -212,6 +212,14 @@ class AssetTestCase(TestCase): self.assertEqual(s_23, s_23) self.assertEqual(s_23, 23) self.assertEqual(23, s_23) + self.assertEqual(int32(23), s_23) + self.assertEqual(int64(23), s_23) + self.assertEqual(s_23, int32(23)) + self.assertEqual(s_23, int64(23)) + # Check all int types (includes long on py2): + for int_type in integer_types: + self.assertEqual(int_type(23), s_23) + self.assertEqual(s_23, int_type(23)) self.assertNotEqual(s_23, s_24) self.assertNotEqual(s_23, 24) diff --git a/zipline/assets/_assets.pyx b/zipline/assets/_assets.pyx index fbac1d86..9ba13577 100644 --- a/zipline/assets/_assets.pyx +++ b/zipline/assets/_assets.pyx @@ -19,6 +19,8 @@ Cythonized Asset object. """ cimport cython +from numbers import Integral + import numpy as np import warnings cimport numpy as np @@ -86,14 +88,20 @@ cdef class Asset: if isinstance(x, Asset): x_as_int = x.sid elif isinstance(x, int): + # ints are Integral, but much faster to special case x_as_int = x + elif isinstance(x, (long, np.int64, np.int32, Integral)): + x_as_int = int(x) else: return NotImplemented if isinstance(y, Asset): y_as_int = y.sid elif isinstance(y, int): + # ints are Integral, but much faster to special case y_as_int = y + elif isinstance(y, (long, np.int64, np.int32, Integral)): + y_as_int = int(y) else: return NotImplemented From 46a47d407c5ae3547623b070c46021ac7c8e6ca0 Mon Sep 17 00:00:00 2001 From: Richard Frank Date: Wed, 20 Jan 2016 17:33:17 -0500 Subject: [PATCH 3/3] DOC: Added whatsnew --- docs/source/whatsnew/0.8.4.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/source/whatsnew/0.8.4.txt b/docs/source/whatsnew/0.8.4.txt index 67743dfc..adc5e9ef 100644 --- a/docs/source/whatsnew/0.8.4.txt +++ b/docs/source/whatsnew/0.8.4.txt @@ -127,6 +127,9 @@ Bug Fixes reported the total_positions_value when creating a :class:`~zipline.protocol.Account` (:issue:`950`). +* Fixed issues around KeyErrors coming from history and BarData on 32-bit + python, where Assets did not compare properly with int64s (:issue:`959`). + Performance ~~~~~~~~~~~