只保留两支股票公共存在的日期的历史数据


(youke) #1

如题
data = D.history_data(‘601398.SHA’, conf.start_date, conf.end_date, conf.fields)
data2 = D.history_data(‘600999.SHA’, conf.start_date, conf.end_date, conf.fields)

我想把得到的data和data2,只保留两支股票公共存在的日期的历史数据,要怎么写?折腾了好几天都搞不定,python的dataframe操作还不够熟练

请大神解答。


(大胡子) #2

不知道这样是否解决了你的问题。

克隆策略

    {"Description":"实验创建于2018/5/1","Summary":"","Graph":{"EdgesInternal":[],"ModuleNodes":[{"Id":"-27","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n \n \n start_date = '2005-01-01'\n end_date = '2018-01-01'\n fields = ['open']\n data1 = D.history_data('601398.SHA', start_date, end_date, fields)\n data2 = D.history_data('600999.SHA', start_date, end_date, fields)\n data1.set_index('date',inplace=True)\n data2.set_index('date',inplace=True)\n data1_new = pd.DataFrame(data1, index=data2.index)\n assert len(data1_new) == len(data2)\n ds1 = DataSource.write_df(data1_new)\n ds2 = DataSource.write_df(data2)\n return Outputs(data_1=ds1, data_2=ds2, data_3=None)\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-27"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-27"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-27"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-27","OutputType":null},{"Name":"data_2","NodeId":"-27","OutputType":null},{"Name":"data_3","NodeId":"-27","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='-27' Position='106,266,200,200'/></NodePositions><NodeGroups /></DataV1>"},"IsDraft":true,"ParentExperimentId":null,"WebService":{"IsWebServiceExperiment":false,"Inputs":[],"Outputs":[],"Parameters":[{"Name":"交易日期","Value":"","ParameterDefinition":{"Name":"交易日期","FriendlyName":"交易日期","DefaultValue":"","ParameterType":"String","HasDefaultValue":true,"IsOptional":true,"ParameterRules":[],"HasRules":false,"MarkupType":0,"CredentialDescriptor":null}}],"WebServiceGroupId":null,"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions></NodePositions><NodeGroups /></DataV1>"},"DisableNodesUpdate":false,"Category":"user","Tags":[],"IsPartialRun":false}
    In [30]:
    # 本代码由可视化策略环境自动生成 2018年5月1日 23:22
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m1_run_bigquant_run(input_1, input_2, input_3):
         
            
        start_date = '2005-01-01'
        end_date = '2018-01-01'
        fields = ['open']
        data1 = D.history_data('601398.SHA',  start_date, end_date,  fields)
        data2 = D.history_data('600999.SHA',  start_date, end_date,  fields)
        data1.set_index('date',inplace=True)
        data2.set_index('date',inplace=True)
        data1_new = pd.DataFrame(data1, index=data2.index)
        assert len(data1_new) == len(data2)
        ds1 = DataSource.write_df(data1_new)
        ds2 = DataSource.write_df(data2)
        return Outputs(data_1=ds1, data_2=ds2, data_3=None)
    
    m1 = M.cached.v3(
        run=m1_run_bigquant_run
    )
    
    [2018-05-01 23:21:45.136415] INFO: bigquant: cached.v3 开始运行..
    [2018-05-01 23:21:45.293131] INFO: bigquant: cached.v3 运行完成[0.156667s].
    
    In [31]:
    m1.data_1.read_df().head()
    
    Out[31]:
    instrument open
    date
    2009-11-17 601398.SHA 5.853422
    2009-11-18 601398.SHA 5.885407
    2009-11-19 601398.SHA 5.949379
    2009-11-20 601398.SHA 5.874745
    2009-11-23 601398.SHA 5.842760