nodes: 0, module; 1, function_definition; 2, function_name:apply; 3, parameters; 4, block; 5, identifier:self; 6, identifier:func; 7, default_parameter; 8, default_parameter; 9, dictionary_splat_pattern; 10, expression_statement; 11, if_statement; 12, identifier:mapping; 13, None; 14, identifier:new_dtype; 15, None; 16, identifier:kwargs; 17, comment:"""Apply an element-wise UDF to the Series.
  
          There are currently 6 options for using a UDF. First 4 are lazy,
          other 2 are eager and require the use of the raw decorator:
  
          - One of the predefined functions in baloo.functions.
          - Implementing a function which encodes the result. kwargs are automatically passed to it.
          - Pure Weld code and mapping.
          - Weld code and mapping along with a dynamically linked C++ lib containing the UDF.
          - Using a NumPy function, which however is EAGER and hence requires self.values to be raw. Additionally, NumPy
              does not support kwargs in (all) functions so must use raw decorator to strip away weld_type.
          - Implementing an eager function with the same precondition as above. Use the raw decorator to check this.
  
          Parameters
          ----------
          func : function or str
              Weld code as a str to encode or function from baloo.functions.
          mapping : dict, optional
              Additional mappings in the weld_template to replace on execution.
              self is added by default to reference to this Series.
          new_dtype : numpy.dtype, optional
              Specify the new dtype of the result Series.
              If None, it assumes it's the same dtype as before the apply.
  
          Returns
          -------
          Series
              With UDF result.
  
          Examples
          --------
          >>> import baloo as bl
          >>> sr = bl.Series([1, 2, 3])
          >>> weld_template = 'map({self}, |e| e + {scalar})'
          >>> mapping = {'scalar': '2L'}
          >>> print(sr.apply(weld_template, mapping).evaluate())
          <BLANKLINE>
          ---  --
            0   3
            1   4
            2   5
          >>> weld_template2 = 'map({self}, |e| e + 3L)'
          >>> print(sr.apply(weld_template2).evaluate())
          <BLANKLINE>
          ---  --
            0   4
            1   5
            2   6
          >>> print(bl.Series([1., 4., 100.]).apply(bl.sqrt).evaluate())  # lazy predefined function
          <BLANKLINE>
          ---  --
            0   1
            1   2
            2  10
          >>> sr = bl.Series([4, 2, 3, 1])
          >>> print(sr.apply(bl.sort, kind='q').evaluate())  # eager wrapper over np.sort (which uses raw decorator)
          <BLANKLINE>
          ---  --
            0   1
            1   2
            2   3
            3   4
          >>> print(sr.apply(bl.raw(np.sort, kind='q')).evaluate())  # np.sort directly
          <BLANKLINE>
          ---  --
            0   1
            1   2
            2   3
            3   4
          >>> print(sr.apply(bl.raw(lambda x: np.sort(x, kind='q'))).evaluate())  # lambda also works, with x = np.array
          <BLANKLINE>
          ---  --
            0   1
            1   2
            2   3
            3   4
  
          # check tests/core/cudf/* and tests/core/test_series.test_cudf for C UDF example
  
          """; 18, call; 19, block; 20, elif_clause; 21, else_clause; 22, identifier:callable; 23, argument_list; 24, return_statement; 25, call; 26, block; 27, block; 28, identifier:func; 29, call; 30, identifier:isinstance; 31, argument_list; 32, expression_statement; 33, expression_statement; 34, expression_statement; 35, if_statement; 36, if_statement; 37, return_statement; 38, raise_statement; 39, identifier:Series; 40, argument_list; 41, identifier:func; 42, identifier:str; 43, call; 44, call; 45, assignment; 46, comparison_operator:mapping is None; 47, block; 48, else_clause; 49, comparison_operator:new_dtype is None; 50, block; 51, call; 52, call; 53, call; 54, attribute; 55, attribute; 56, attribute; 57, identifier:check_type; 58, argument_list; 59, identifier:check_dtype; 60, argument_list; 61, identifier:default_mapping; 62, dictionary; 63, identifier:mapping; 64, None; 65, expression_statement; 66, block; 67, identifier:new_dtype; 68, None; 69, expression_statement; 70, identifier:Series; 71, argument_list; 72, identifier:TypeError; 73, argument_list; 74, identifier:func; 75, argument_list; 76, identifier:self; 77, identifier:index; 78, identifier:self; 79, identifier:dtype; 80, identifier:self; 81, identifier:name; 82, identifier:mapping; 83, identifier:dict; 84, identifier:new_dtype; 85, pair; 86, assignment; 87, expression_statement; 88, assignment; 89, call; 90, attribute; 91, identifier:new_dtype; 92, attribute; 93, string; 94, attribute; 95, keyword_argument; 96, dictionary_splat; 97, string; 98, attribute; 99, identifier:mapping; 100, identifier:default_mapping; 101, call; 102, identifier:new_dtype; 103, attribute; 104, identifier:weld_udf; 105, argument_list; 106, identifier:self; 107, identifier:index; 108, identifier:self; 109, identifier:name; 110, string_content:Expected function or str defining a weld_template; 111, identifier:self; 112, identifier:values; 113, identifier:weld_type; 114, attribute; 115, identifier:kwargs; 116, string_content:self; 117, identifier:self; 118, identifier:values; 119, attribute; 120, argument_list; 121, identifier:self; 122, identifier:dtype; 123, identifier:func; 124, identifier:mapping; 125, identifier:self; 126, identifier:weld_type; 127, identifier:mapping; 128, identifier:update; 129, identifier:default_mapping
code: def apply(self, func, mapping=None, new_dtype=None, **kwargs):
          """Apply an element-wise UDF to the Series.
  
          There are currently 6 options for using a UDF. First 4 are lazy,
          other 2 are eager and require the use of the raw decorator:
  
          - One of the predefined functions in baloo.functions.
          - Implementing a function which encodes the result. kwargs are automatically passed to it.
          - Pure Weld code and mapping.
          - Weld code and mapping along with a dynamically linked C++ lib containing the UDF.
          - Using a NumPy function, which however is EAGER and hence requires self.values to be raw. Additionally, NumPy
              does not support kwargs in (all) functions so must use raw decorator to strip away weld_type.
          - Implementing an eager function with the same precondition as above. Use the raw decorator to check this.
  
          Parameters
          ----------
          func : function or str
              Weld code as a str to encode or function from baloo.functions.
          mapping : dict, optional
              Additional mappings in the weld_template to replace on execution.
              self is added by default to reference to this Series.
          new_dtype : numpy.dtype, optional
              Specify the new dtype of the result Series.
              If None, it assumes it's the same dtype as before the apply.
  
          Returns
          -------
          Series
              With UDF result.
  
          Examples
          --------
          >>> import baloo as bl
          >>> sr = bl.Series([1, 2, 3])
          >>> weld_template = 'map({self}, |e| e + {scalar})'
          >>> mapping = {'scalar': '2L'}
          >>> print(sr.apply(weld_template, mapping).evaluate())
          <BLANKLINE>
          ---  --
            0   3
            1   4
            2   5
          >>> weld_template2 = 'map({self}, |e| e + 3L)'
          >>> print(sr.apply(weld_template2).evaluate())
          <BLANKLINE>
          ---  --
            0   4
            1   5
            2   6
          >>> print(bl.Series([1., 4., 100.]).apply(bl.sqrt).evaluate())  # lazy predefined function
          <BLANKLINE>
          ---  --
            0   1
            1   2
            2  10
          >>> sr = bl.Series([4, 2, 3, 1])
          >>> print(sr.apply(bl.sort, kind='q').evaluate())  # eager wrapper over np.sort (which uses raw decorator)
          <BLANKLINE>
          ---  --
            0   1
            1   2
            2   3
            3   4
          >>> print(sr.apply(bl.raw(np.sort, kind='q')).evaluate())  # np.sort directly
          <BLANKLINE>
          ---  --
            0   1
            1   2
            2   3
            3   4
          >>> print(sr.apply(bl.raw(lambda x: np.sort(x, kind='q'))).evaluate())  # lambda also works, with x = np.array
          <BLANKLINE>
          ---  --
            0   1
            1   2
            2   3
            3   4
  
          # check tests/core/cudf/* and tests/core/test_series.test_cudf for C UDF example
  
          """
          if callable(func):
              return Series(func(self.values,
                                 weld_type=self.weld_type,
                                 **kwargs),
                            self.index,
                            self.dtype,
                            self.name)
          elif isinstance(func, str):
              check_type(mapping, dict)
              check_dtype(new_dtype)
  
              default_mapping = {'self': self.values}
              if mapping is None:
                  mapping = default_mapping
              else:
                  mapping.update(default_mapping)
  
              if new_dtype is None:
                  new_dtype = self.dtype
  
              return Series(weld_udf(func,
                                     mapping),
                            self.index,
                            new_dtype,
                            self.name)
          else:
              raise TypeError('Expected function or str defining a weld_template')