BUG: Filter nans in DataFrame and Panel sources.

If a SID hasn't started trading yet, pandas' convention is to use nans.
Before this change, zipline would raise an exception if there were nans in the
input data.

We now skip events where the prices contains a nan and has not been traded
before (in which case forward fill).

Fixes #446.
This commit is contained in:
Thomas Wiecki
2015-04-08 17:00:22 +02:00
parent a257a43e99
commit d578d5825e
2 changed files with 55 additions and 9 deletions
+33
View File
@@ -79,6 +79,39 @@ class TestDataFrameSource(TestCase):
self.assertTrue(isinstance(event['volume'], (integer_types)))
self.assertEqual(next(stocks_iter), event['sid'])
def test_nan_filter_dataframe(self):
dates = pd.date_range('1/1/2000', periods=2, freq='B', tz='UTC')
df = pd.DataFrame(np.random.randn(2, 2),
index=dates,
columns=['A', 'B'])
df.loc[dates[0], 'A'] = np.nan # should be filtered
df.loc[dates[1], 'B'] = np.nan # should not be filtered
source = DataFrameSource(df)
event = next(source)
self.assertEqual('B', event.sid)
event = next(source)
self.assertEqual('A', event.sid)
event = next(source)
self.assertEqual('B', event.sid)
self.assertTrue(np.isnan(event.price))
def test_nan_filter_panel(self):
dates = pd.date_range('1/1/2000', periods=2, freq='B', tz='UTC')
df = pd.Panel(np.random.randn(2, 2, 2),
major_axis=dates,
items=['A', 'B'],
minor_axis=['price', 'volume'])
df.loc['A', dates[0], 'price'] = np.nan # should be filtered
df.loc['B', dates[1], 'price'] = np.nan # should not be filtered
source = DataPanelSource(df)
event = next(source)
self.assertEqual('B', event.sid)
event = next(source)
self.assertEqual('A', event.sid)
event = next(source)
self.assertEqual('B', event.sid)
self.assertTrue(np.isnan(event.price))
class TestRandomWalkSource(TestCase):
def test_minute(self):