复制链接
克隆策略

    {"description":"实验创建于2017/8/26","graph":{"edges":[{"to_node_id":"-539:input_1","from_node_id":"-185:data"},{"to_node_id":"-32:input_ds","from_node_id":"-185:data"}],"nodes":[{"node_id":"-539","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"# 修改数据列名\ndef bigquant_run(input_ds, columns, keep_old_columns):\n # 解析列映射为dict, TODO: 验证输入是否有效\n columns = dict(c.split(':') for c in columns.split('|'))\n print('列名映射:', columns)\n # 输出数据源\n df = input_ds.read()\n old_column_set = set(df.columns)\n for old_col, new_col in columns.items():\n if old_col not in old_column_set:\n print('警告:列 %s 不存在' % old_col)\n if keep_old_columns:\n for old_col, new_col in columns.items():\n if old_col in old_column_set:\n df[new_col] = df[old_col]\n else:\n df.columns = [columns.get(c, c) for c in df.columns]\n\n return Outputs(data=DataSource.write_df(df))\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"input_ds","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{\n 'columns': 'deal_number:new_deal_number|close:new_close',\n 'keep_old_columns': True\n}\n","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"data","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-539"},{"name":"input_2","node_id":"-539"},{"name":"input_3","node_id":"-539"}],"output_ports":[{"name":"data_1","node_id":"-539"},{"name":"data_2","node_id":"-539"},{"name":"data_3","node_id":"-539"}],"cacheable":true,"seq_num":19,"comment":"","comment_collapsed":true},{"node_id":"-185","module_id":"BigQuantSpace.use_datasource.use_datasource-v1","parameters":[{"name":"datasource_id","value":"bar1d_CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"start_date","value":"2018-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2018-02-01","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-185"},{"name":"features","node_id":"-185"}],"output_ports":[{"name":"data","node_id":"-185"}],"cacheable":true,"seq_num":4,"comment":"","comment_collapsed":true},{"node_id":"-32","module_id":"BigQuantSpace.columns_rename.columns_rename-v1","parameters":[{"name":"columns","value":"deal_number:new_deal_number|close:new_close","type":"Literal","bound_global_parameter":null},{"name":"keep_old_columns","value":"True","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_ds","node_id":"-32"}],"output_ports":[{"name":"data","node_id":"-32"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-539' Position='1037,79,200,200'/><node_position Node='-185' Position='761,-15,200,200'/><node_position Node='-32' Position='742.6492309570312,111.48371887207031,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [48]:
    # 本代码由可视化策略环境自动生成 2021年12月16日 17:34
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 修改数据列名
    def m19_run_bigquant_run(input_ds, columns, keep_old_columns):
        # 解析列映射为dict, TODO: 验证输入是否有效
        columns = dict(c.split(':') for c in columns.split('|'))
        print('列名映射:', columns)
        # 输出数据源
        df = input_ds.read()
        old_column_set = set(df.columns)
        for old_col, new_col in columns.items():
            if old_col not in old_column_set:
                print('警告:列 %s 不存在' % old_col)
        if keep_old_columns:
            for old_col, new_col in columns.items():
                if old_col in old_column_set:
                    df[new_col] = df[old_col]
        else:
            df.columns = [columns.get(c, c) for c in df.columns]
    
        return Outputs(data=DataSource.write_df(df))
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m19_post_run_bigquant_run(outputs):
        return outputs
    
    
    m4 = M.use_datasource.v1(
        datasource_id='bar1d_CN_STOCK_A',
        start_date='2018-01-01',
        end_date='2018-02-01'
    )
    
    m19 = M.cached.v3(
        input_1=m4.data,
        run=m19_run_bigquant_run,
        post_run=m19_post_run_bigquant_run,
        input_ports='input_ds',
        params="""{
        'columns': 'deal_number:new_deal_number|close:new_close',
        'keep_old_columns': True
    }
    """,
        output_ports='data'
    )
    
    m3 = M.columns_rename.v1(
        input_ds=m4.data,
        columns='deal_number:new_deal_number|close:new_close',
        keep_old_columns=True
    )
    
    列名映射: {'deal_number': 'new_deal_number', 'close': 'new_close'}
    
    In [49]:
    m3.data.read_df().head()
    
    Out[49]:
    turn adjust_factor instrument date open volume low deal_number close high amount new_deal_number new_close
    0 0.315558 5.008553 002472.SZA 2018-01-02 49.234077 1717932.0 48.883476 2942.0 49.584675 49.734932 16917760.0 2942.0 49.584675
    1 0.480442 6.317195 300211.SZA 2018-01-02 45.673321 892200.0 44.473053 539.0 44.915257 45.673321 6337774.0 539.0 44.915257
    2 6.872656 1.020020 601375.SHA 2018-01-02 6.273123 48107386.0 6.211922 22878.0 6.385325 6.446527 298756008.0 22878.0 6.385325
    3 0.833823 3.926112 600266.SHA 2018-01-02 51.392807 13066347.0 51.392807 6388.0 53.041775 53.277340 176102119.0 6388.0 53.041775
    4 2.923437 1.012552 300637.SZA 2018-01-02 21.324345 877031.0 20.949701 762.0 21.516729 21.769869 18593165.0 762.0 21.516729
    In [50]:
    m19.data.read_df().head()
    
    Out[50]:
    turn adjust_factor instrument date open volume low deal_number close high amount new_deal_number new_close
    0 0.315558 5.008553 002472.SZA 2018-01-02 49.234077 1717932.0 48.883476 2942.0 49.584675 49.734932 16917760.0 2942.0 49.584675
    1 0.480442 6.317195 300211.SZA 2018-01-02 45.673321 892200.0 44.473053 539.0 44.915257 45.673321 6337774.0 539.0 44.915257
    2 6.872656 1.020020 601375.SHA 2018-01-02 6.273123 48107386.0 6.211922 22878.0 6.385325 6.446527 298756008.0 22878.0 6.385325
    3 0.833823 3.926112 600266.SHA 2018-01-02 51.392807 13066347.0 51.392807 6388.0 53.041775 53.277340 176102119.0 6388.0 53.041775
    4 2.923437 1.012552 300637.SZA 2018-01-02 21.324345 877031.0 20.949701 762.0 21.516729 21.769869 18593165.0 762.0 21.516729