Skip to content

Commit

Permalink
Merge pull request quantopian#1162 from quantopian/handle-missing-fields
Browse files Browse the repository at this point in the history
DEV: Don't log an error if we can't find a matching asset/field/day triple in Fetcher
  • Loading branch information
jbredeche committed Apr 25, 2016
2 parents 19128fa + 02ded43 commit ba20235
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
33 changes: 33 additions & 0 deletions tests/test_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,3 +595,36 @@ def before_trading_start(context, data):
np.testing.assert_array_almost_equal(values[55:64], [2.50233] * 9)
np.testing.assert_array_almost_equal(values[64:75], [2.550829] * 11)
np.testing.assert_array_almost_equal(values[75:], [2.64484] * 35)

def test_fetcher_bad_data(self):
self.responses.add(
self.responses.GET,
'https://fake.urls.com/fetcher_nflx_data.csv',
body=NFLX_DATA,
content_type='text/csv',
)

sim_params = factory.create_simulation_parameters(
start=pd.Timestamp("2013-06-12", tz='UTC'),
end=pd.Timestamp("2013-06-14", tz='UTC'),
data_frequency="minute"
)

results = self.run_algo("""
from zipline.api import fetch_csv, symbol
import numpy as np
def initialize(context):
fetch_csv('https://fake.urls.com/fetcher_nflx_data.csv',
date_column = 'Settlement Date',
date_format = '%m/%d/%y')
context.nflx = symbol('NFLX')
context.aapl = symbol('AAPL')
def handle_data(context, data):
assert np.isnan(data.current(context.nflx, 'invalid_column'))
assert np.isnan(data.current(context.aapl, 'invalid_column'))
assert np.isnan(data.current(context.aapl, 'dtc'))
""", sim_params=sim_params, data_frequency="minute")

self.assertEqual(3, len(results))
13 changes: 3 additions & 10 deletions zipline/data/data_portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,8 @@ def _is_extra_source(asset, field, map):
# If we have an extra source with a column called "price", only look
# at it if it's on something like palladium and not AAPL (since our
# own price data always wins when dealing with assets).
return field in map and not (field in BASE_FIELDS and
isinstance(asset, Asset))

return not (field in BASE_FIELDS and isinstance(asset, Asset))

def get_spot_value(self, asset, field, dt, data_frequency):
"""
Expand Down Expand Up @@ -680,14 +680,7 @@ def get_spot_value(self, asset, field, dt, data_frequency):
try:
return \
self._augmented_sources_map[field][asset].loc[day, field]
except:
log.error(
"Could not find value for asset={0}, day={1},"
"column={2}".format(
str(asset),
str(day),
str(field)))

except KeyError:
return np.NaN

if field not in BASE_FIELDS:
Expand Down

0 comments on commit ba20235

Please sign in to comment.