<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> import pandas as pd <EOL> from zipline . gens . utils import hash_args <EOL> from zipline . sources . data_source import DataSource <EOL> class DataFrameSource ( DataSource ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data , ** kwargs ) : <EOL> assert isinstance ( data . index , pd . tseries . index . DatetimeIndex ) <EOL> assert isinstance ( data . columns , pd . Int64Index ) <EOL> self . data = data . fillna ( method = '<STR_LIT>' ) <EOL> self . start = kwargs . get ( '<STR_LIT:start>' , self . data . index [ <NUM_LIT:0> ] ) <EOL> self . end = kwargs . get ( '<STR_LIT:end>' , self . data . index [ - <NUM_LIT:1> ] ) <EOL> self . sids = self . data . columns <EOL> self . arg_string = hash_args ( data , ** kwargs ) <EOL> self . _raw_data = None <EOL> self . started_sids = set ( ) <EOL> @ property <EOL> def mapping ( self ) : <EOL> return { <EOL> '<STR_LIT>' : ( lambda x : x , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( lambda x : x , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( float , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( int , '<STR_LIT>' ) , <EOL> } <EOL> @ property <EOL> def instance_hash ( self ) : <EOL> return self . arg_string <EOL> def raw_data_gen ( self ) : <EOL> for dt , series in self . data . iterrows ( ) : <EOL> for sid , price in series . iteritems ( ) : <EOL> if np . isnan ( price ) and sid not in self . started_sids : <EOL> continue <EOL> self . started_sids . add ( sid ) <EOL> event = { <EOL> '<STR_LIT>' : dt , <EOL> '<STR_LIT>' : sid , <EOL> '<STR_LIT>' : price , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } <EOL> yield event <EOL> @ property <EOL> def raw_data ( self ) : <EOL> if not self . _raw_data : <EOL> self . _raw_data = self . raw_data_gen ( ) <EOL> return self . _raw_data <EOL> class DataPanelSource ( DataSource ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data , ** kwargs ) : <EOL> assert isinstance ( data . major_axis , pd . tseries . index . DatetimeIndex ) <EOL> assert isinstance ( data . items , pd . Int64Index ) <EOL> self . data = data . fillna ( value = { '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> self . start = kwargs . get ( '<STR_LIT:start>' , self . data . major_axis [ <NUM_LIT:0> ] ) <EOL> self . end = kwargs . get ( '<STR_LIT:end>' , self . data . major_axis [ - <NUM_LIT:1> ] ) <EOL> self . sids = self . data . items <EOL> self . arg_string = hash_args ( data , ** kwargs ) <EOL> self . _raw_data = None <EOL> self . started_sids = set ( ) <EOL> @ property <EOL> def mapping ( self ) : <EOL> mapping = { <EOL> '<STR_LIT>' : ( lambda x : x , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( lambda x : x , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( float , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( int , '<STR_LIT>' ) , <EOL> } <EOL> for field_name in self . data . minor_axis : <EOL> if field_name in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> continue <EOL> mapping [ field_name ] = ( lambda x : x , field_name ) <EOL> return mapping <EOL> @ property <EOL> def instance_hash ( self ) : <EOL> return self . arg_string <EOL> def raw_data_gen ( self ) : <EOL> for dt in self . data . major_axis : <EOL> df = self . data . major_xs ( dt ) <EOL> for sid , series in df . iteritems ( ) : <EOL> if np . isnan ( series [ '<STR_LIT>' ] ) : <EOL> continue <EOL> self . started_sids . add ( sid ) <EOL> event = { <EOL> '<STR_LIT>' : dt , <EOL> '<STR_LIT>' : sid , <EOL> } <EOL> for field_name , value in series . iteritems ( ) : <EOL> event [ field_name ] = value <EOL> yield event <EOL> @ property <EOL> def raw_data ( self ) : <EOL> if not self . _raw_data : <EOL> self . _raw_data = self . raw_data_gen ( ) <EOL> return self . _raw_data </s>
