parameters: (self, modify_reply=True, initialize_first=True)
docstring: Transforms a BigQuery reply with a pivot.
  
      Note that the pivot is "ragged", in that rows will only contain values up
      until the "last" column with data.  This format is acceptable for GViz, and
      the data will load as though all "missing" columns existed with a value of
      None.
  
      Args:
        modify_reply: If True, the 'rows' and 'schema' attributes will be
            modified by this method.  Otherwise, they are still accessible after
            Transform() by self.transformed_schema and self.transformed_rows.
        initialize_first: If True, transformed_schema and _rows will be erased
            before processing begins.
  
      Raises:
        ValueError: Raised if a column value matches the row name.
        DuplicateValueError: Raised if a value already exists for a provided
            row/column.
docstring_summary: Transforms a BigQuery reply with a pivot.
docstring_tokens: Transforms a BigQuery reply with a pivot .
function: def Transform(self, modify_reply=True, initialize_first=True):
      """Transforms a BigQuery reply with a pivot.
  
      Note that the pivot is "ragged", in that rows will only contain values up
      until the "last" column with data.  This format is acceptable for GViz, and
      the data will load as though all "missing" columns existed with a value of
      None.
  
      Args:
        modify_reply: If True, the 'rows' and 'schema' attributes will be
            modified by this method.  Otherwise, they are still accessible after
            Transform() by self.transformed_schema and self.transformed_rows.
        initialize_first: If True, transformed_schema and _rows will be erased
            before processing begins.
  
      Raises:
        ValueError: Raised if a column value matches the row name.
        DuplicateValueError: Raised if a value already exists for a provided
            row/column.
      """
      if initialize_first:
        self.Initialize()
  
      rows_field = self.GetColumn(self.reply['schema'], self.rows_name)
      columns_field = self.GetColumn(self.reply['schema'], self.columns_name)
      values_field = self.GetColumn(self.reply['schema'], self.values_name)
  
      row_headers = {}
      row_count = 0
      column_headers = {}
      column_count = 0
  
      source_rows_field_index = self.reply['schema']['fields'].index(
          rows_field)
      source_columns_field_index = self.reply['schema']['fields'].index(
          columns_field)
      source_values_field_index = self.reply['schema']['fields'].index(
          values_field)
  
      self.AddPivotField(
          field_name=rows_field['name'],
          field_type=rows_field['type'],
          field_mode=rows_field['mode'])
  
      for data_row in self.reply['rows']:
        row_name = data_row['f'][source_rows_field_index]['v']
        column_name = data_row['f'][source_columns_field_index]['v']
        value = data_row['f'][source_values_field_index]['v']
  
        # Find or create the row entry matching the current value.
        if row_name in row_headers:
          row_index = row_headers[row_name]
          row = self.transformed_rows[row_index]
        else:
          row = {'f': [{'v': row_name}]}
          self.transformed_rows.append(row)
          row_index = row_count
          row_headers[row_name] = row_count
          row_count += 1
  
        # Find or create the column entry matching the current value.
        if column_name in column_headers:
          column_index = column_headers[column_name]
        else:
          self.AddPivotField(
              field_name=column_name,
              field_type=values_field['type'],
              field_mode='NULLABLE')
          column_index = column_count + 1
          column_headers[column_name] = column_index
          column_count += 1
  
        # If a 'column' value is the same as the 'row' heading, raise an error.
        if column_index == 0:
          raise ValueError(
              'Column value \'%s\' is the same as the row heading name.'
              % column_name)
  
        # If there aren't enough values in the current row, add empty values.
        while column_index > (len(self.transformed_rows[row_index]['f']) - 1):
          self.transformed_rows[row_index]['f'].append({'v': None})
  
        # If the target cell already has a value, throw an error.
        if self.transformed_rows[row_index]['f'][column_index]['v']:
          msg = (
              'Pivot failed: value already exists at row "%s", col "%s". '
              'Pivots require data to be pre-aggregated; each row/col combination '
              'can only appear once.' % (row_name, column_name))
          logging.error(msg)
          raise DuplicateValueError(msg)
  
        # Set the value of the target cell.
        self.transformed_rows[row_index]['f'][column_index]['v'] = value
  
      if modify_reply:
        self.reply['schema'] = self.transformed_schema
        self.reply['rows'] = self.transformed_rows
function_tokens: def Transform ( self , modify_reply = True , initialize_first = True ) : if initialize_first : self . Initialize ( ) rows_field = self . GetColumn ( self . reply [ 'schema' ] , self . rows_name ) columns_field = self . GetColumn ( self . reply [ 'schema' ] , self . columns_name ) values_field = self . GetColumn ( self . reply [ 'schema' ] , self . values_name ) row_headers = { } row_count = 0 column_headers = { } column_count = 0 source_rows_field_index = self . reply [ 'schema' ] [ 'fields' ] . index ( rows_field ) source_columns_field_index = self . reply [ 'schema' ] [ 'fields' ] . index ( columns_field ) source_values_field_index = self . reply [ 'schema' ] [ 'fields' ] . index ( values_field ) self . AddPivotField ( field_name = rows_field [ 'name' ] , field_type = rows_field [ 'type' ] , field_mode = rows_field [ 'mode' ] ) for data_row in self . reply [ 'rows' ] : row_name = data_row [ 'f' ] [ source_rows_field_index ] [ 'v' ] column_name = data_row [ 'f' ] [ source_columns_field_index ] [ 'v' ] value = data_row [ 'f' ] [ source_values_field_index ] [ 'v' ] # Find or create the row entry matching the current value. if row_name in row_headers : row_index = row_headers [ row_name ] row = self . transformed_rows [ row_index ] else : row = { 'f' : [ { 'v' : row_name } ] } self . transformed_rows . append ( row ) row_index = row_count row_headers [ row_name ] = row_count row_count += 1 # Find or create the column entry matching the current value. if column_name in column_headers : column_index = column_headers [ column_name ] else : self . AddPivotField ( field_name = column_name , field_type = values_field [ 'type' ] , field_mode = 'NULLABLE' ) column_index = column_count + 1 column_headers [ column_name ] = column_index column_count += 1 # If a 'column' value is the same as the 'row' heading, raise an error. if column_index == 0 : raise ValueError ( 'Column value \'%s\' is the same as the row heading name.' % column_name ) # If there aren't enough values in the current row, add empty values. while column_index > ( len ( self . transformed_rows [ row_index ] [ 'f' ] ) - 1 ) : self . transformed_rows [ row_index ] [ 'f' ] . append ( { 'v' : None } ) # If the target cell already has a value, throw an error. if self . transformed_rows [ row_index ] [ 'f' ] [ column_index ] [ 'v' ] : msg = ( 'Pivot failed: value already exists at row "%s", col "%s". ' 'Pivots require data to be pre-aggregated; each row/col combination ' 'can only appear once.' % ( row_name , column_name ) ) logging . error ( msg ) raise DuplicateValueError ( msg ) # Set the value of the target cell. self . transformed_rows [ row_index ] [ 'f' ] [ column_index ] [ 'v' ] = value if modify_reply : self . reply [ 'schema' ] = self . transformed_schema self . reply [ 'rows' ] = self . transformed_rows