求助,pandas已知一列不同的天数a和对应的close_0,怎么求相对应的shift(close_0,a)。

策略分享
标签: #<Tag:0x00007fa19d443450>

(tkbq66) #1
克隆策略

    {"Description":"实验创建于2020/8/11","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-27:instruments","SourceOutputPortId":"-92:data"},{"DestinationInputPortId":"-27:features","SourceOutputPortId":"-104:data"},{"DestinationInputPortId":"-34:features","SourceOutputPortId":"-104:data"},{"DestinationInputPortId":"-34:input_data","SourceOutputPortId":"-27:data"}],"ModuleNodes":[{"Id":"-92","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2020-08-10","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2020-08-11","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-92"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-92","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"Comment":"","CommentCollapsed":true},{"Id":"-104","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"a=9-ts_argmax(close_0, 10)\nclose_0\nb=shift(close_0, a)\n\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-104"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-104","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"Comment":"","CommentCollapsed":true},{"Id":"-27","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":"20","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-27"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-27"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-27","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true},{"Id":"-34","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-34"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-34"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-34","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"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='-92' Position='-124,-8,200,200'/><NodePosition Node='-104' Position='182,-1,200,200'/><NodePosition Node='-27' Position='-8,148,200,200'/><NodePosition Node='-34' Position='37,274.17791748046875,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":true}
    In [9]:
    # 本代码由可视化策略环境自动生成 2020年8月14日 15:58
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    m2 = M.instruments.v2(
        start_date='2020-08-10',
        end_date='2020-08-11',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m4 = M.input_features.v1(
        features="""a=9-ts_argmax(close_0, 10)
    close_0
    b=shift(close_0, a)
    
    """
    )
    
    m1 = M.general_feature_extractor.v7(
        instruments=m2.data,
        features=m4.data,
        start_date='',
        end_date='',
        before_start_days=20
    )
    
    m3 = M.derived_feature_extractor.v3(
        input_data=m1.data,
        features=m4.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=True,
        remove_extra_columns=False,
        user_functions={}
    )
    


    (lu0817) #2

    简单就是close_0.shift(a),不知道有帮助没


    (tkbq66) #3

    提示这个错误 b=close_0.shift(a): The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().


    (adhaha111) #4

    您好,shift(x, d),其中 d 只能是int类型的


    (tkbq66) #5

    有什么办法求shift(close_0,a)


    (adhaha111) #6

    您可以在衍生特征后添加个python自定义模块,手动进行特征抽取
    暂时不能在特征列表模块中这样定义


    (tkbq88) #7

    我不是专业程序员科班出身,有点难度啊


    (adhaha111) #8

    大概可以这样,不过由于效率的问题,还应该进行一些改动

    克隆策略

      {"Description":"实验创建于2020/8/11","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-27:instruments","SourceOutputPortId":"-92:data"},{"DestinationInputPortId":"-27:features","SourceOutputPortId":"-104:data"},{"DestinationInputPortId":"-34:features","SourceOutputPortId":"-104:data"},{"DestinationInputPortId":"-34:input_data","SourceOutputPortId":"-27:data"},{"DestinationInputPortId":"-46:input_1","SourceOutputPortId":"-34:data"}],"ModuleNodes":[{"Id":"-92","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2020-08-10","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2020-08-11","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-92"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-92","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"Comment":"","CommentCollapsed":true},{"Id":"-104","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"a=9-ts_argmax(close_0, 10)\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-104"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-104","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"Comment":"","CommentCollapsed":true},{"Id":"-27","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":"30","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-27"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-27"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-27","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true},{"Id":"-34","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-34"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-34"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-34","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"Comment":"","CommentCollapsed":true},{"Id":"-46","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 df = input_1.read()\n df = df.set_index(\"date\")\n list_c = []\n # 利用for循环加ix会导致效率很低,可以尝试使用apply或numpy原生进行计算\n for i in range(len(df)):\n series = df[\"close_0\"].shift(int(df.ix[i, \"a\"]))\n list_c.append(series[i])\n df[\"close_0_shifted\"] = list_c\n data_1 = DataSource.write_df(df)\n \n return Outputs(data_1=data_1, data_2=None, data_3=None)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"post_run","Value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"input_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-46"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-46"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-46"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-46","OutputType":null},{"Name":"data_2","NodeId":"-46","OutputType":null},{"Name":"data_3","NodeId":"-46","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"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='-92' Position='-124,-8,200,200'/><NodePosition Node='-104' Position='180,-1,200,200'/><NodePosition Node='-27' Position='-8,148,200,200'/><NodePosition Node='-34' Position='39,275,200,200'/><NodePosition Node='-46' Position='41.07087707519531,387.00001525878906,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":true}
      In [46]:
      # 本代码由可视化策略环境自动生成 2020年8月19日 09:59
      # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
      
      
      # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
      def m6_run_bigquant_run(input_1, input_2, input_3):
          # 示例代码如下。在这里编写您的代码
          df = input_1.read()
          df = df.set_index("date")
          list_c = []
          # 利用for循环加ix会导致效率很低,可以尝试使用apply或numpy原生进行计算
          for i in range(len(df)):
              series = df["close_0"].shift(int(df.ix[i, "a"]))
              list_c.append(series[i])
          df["close_0_shifted"] = list_c
          data_1 = DataSource.write_df(df)
          
          return Outputs(data_1=data_1, data_2=None, data_3=None)
      
      # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
      def m6_post_run_bigquant_run(outputs):
          return outputs
      
      
      m2 = M.instruments.v2(
          start_date='2020-08-10',
          end_date='2020-08-11',
          market='CN_STOCK_A',
          instrument_list='',
          max_count=0
      )
      
      m4 = M.input_features.v1(
          features="""a=9-ts_argmax(close_0, 10)
      """
      )
      
      m1 = M.general_feature_extractor.v7(
          instruments=m2.data,
          features=m4.data,
          start_date='',
          end_date='',
          before_start_days=30
      )
      
      m3 = M.derived_feature_extractor.v3(
          input_data=m1.data,
          features=m4.data,
          date_col='date',
          instrument_col='instrument',
          drop_na=True,
          remove_extra_columns=False,
          user_functions={}
      )
      
      m6 = M.cached.v3(
          input_1=m3.data,
          run=m6_run_bigquant_run,
          post_run=m6_post_run_bigquant_run,
          input_ports='',
          params='{}',
          output_ports=''
      )
      
      ---------------------------------------------------------------------------
      NameError                                 Traceback (most recent call last)
      <ipython-input-46-a13bdc52bd18> in <module>()
           59     input_ports='',
           60     params='{}',
      ---> 61     output_ports=''
           62 )
      
      <ipython-input-46-a13bdc52bd18> in m6_run_bigquant_run(input_1, input_2, input_3)
           15     data_1 = DataSource.write_df(df)
           16 
      ---> 17     return Outputs(data_1=data_1, data_2=data_2, data_3=None)
           18 
           19 # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
      
      NameError: name 'data_2' is not defined

      (tkbq66) #9

      好的,谢谢