DEV: Ensure there are no duplicates in the data passed into TradingAlgorithm.run

This commit is contained in:
Jean Bredeche
2016-05-05 11:54:39 -04:00
parent f3e436a1bf
commit 3f1b0f79f2
2 changed files with 58 additions and 1 deletions
+46
View File
@@ -0,0 +1,46 @@
#
# Copyright 2016 Quantopian, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pandas as pd
from zipline.data.us_equity_pricing import PanelDailyBarReader
from zipline.testing.fixtures import WithTradingEnvironment, ZiplineTestCase
class TestPanelDailyBarReader(WithTradingEnvironment, ZiplineTestCase):
def test_duplicate_values(self):
df = pd.DataFrame()
panel = pd.concat([pd.Panel({"X": df}), pd.Panel({"X": df})])
with self.assertRaises(ValueError) as e:
# panel's items has duplicates
PanelDailyBarReader(None, panel)
self.assertEqual("Duplicated items found: ['X']",
e.exception.message)
with self.assertRaises(ValueError) as e:
# panel's major axis has duplicates
PanelDailyBarReader(None, panel.swapaxes(0, 1))
self.assertEqual("Duplicated items found: ['X']",
e.exception.message)
with self.assertRaises(ValueError) as e:
# panel's minor axis has duplicates
PanelDailyBarReader(None, panel.swapaxes(0, 2))
self.assertEqual("Duplicated items found: ['X']",
e.exception.message)
+12 -1
View File
@@ -711,6 +711,17 @@ class PanelDailyBarReader(DailyBarReader):
The first trading day in the dataset.
"""
def __init__(self, calendar, panel):
# check duplicates on all indices of panel
for attr_name in ["items", "major_axis", "minor_axis"]:
index = getattr(panel, attr_name)
duplicates = index.duplicated()
if duplicates.any():
raise ValueError("Duplicated items found: {0}".format(
index[duplicates].values
))
panel = panel.copy()
if 'volume' not in panel.items:
# Fake volume if it does not exist.
@@ -760,7 +771,7 @@ class PanelDailyBarReader(DailyBarReader):
Returns -1 if the day is within the date range, but the price is
0.
"""
return self.panel[sid, day, colname]
return self.panel.loc[sid, day, colname]
def get_last_traded_dt(self, sid, dt):
"""