DataFrame Class

A DataFrame object, used to communicate data to and from the AMPL entities.

An object of this class can be used to do the following tasks:

Assign values to AMPL entities (once the DataFrame is populated, use ampl.AMPL.SetData to assign its values to the modelling entities in its columns)

Get values from AMPL, decoupling the values from the AMPL entities they originate via ampl.Entities.Entity.GetValues.

A DataFrame object can be created in various ways via the various constructors: create a skeleton manually specifiying the number of indexing columns and the column headers; get column names (and number of indexing columns) from entities of the API passing them directly to the appropriate constructor, get values from AMPL, decoupling the values from the AMPL entities they originate from (via ampl.Entities.Entity.GetValues()).

Populating a DataFrame object can be done adding row by row to a pre-existing skeleton via ampl.DataFrame.AddRow, setting whole columns of a pre-existing skeleton via ampl.DataFrame.SetColumn or adding columns (including indexing columns) via ampl.DataFrame.AddColumn. Modifying a DataFrame object can be done via ampl.DataFrame.SetColumn or via ampl.DataFrame.SetArray or ampl.DataFrame.SetMatrix Accessing data in a DataFrame can be done row by row using ampl.DataFrame.GetRow or by column via ampl.DataFrame.GetColumn.

Namespace
ampl
Assemblies
  • AMPL

Syntax

public class DataFrame : IEnumerable<DataFrame.Row>, IEnumerable
class ampl.DataFrame

Constructors

DataFrame(System.Int32)

Create a new DataFrame with the specified number of indices

public DataFrame(int numberOfIndexColumns)
DataFrame(System.Int32, System.String[])

Create a new DataFrame with the specified number of indices and the specified headers

Arguments:
  • numberOfIndexColumns (System.Int32) – Number of columns to be considered as index for this table (&gt;=0)
  • headers (System.String<System.String>[]) – Headers (column names)
public DataFrame(int numberOfIndexColumns, params string[] headers)
DataFrame(ampl.Entities.Entity[])

Create a new DataFrame with the specified entities as column headers.

Arguments:
  • headers (ampl.Entities.Entity<ampl.Entities.Entity>[]) – The columns headers to be used; since they are AMPL entities, sets are automatically considered indices and have to be placed first in the constructor.
public DataFrame(params Entity[] headers)

Methods

AddColumn(System.String)

Add a new empty column with the corresponding header

Arguments:
  • header (System.String) – The header
public void AddColumn(string header)
AddColumn(System.String, System.Double[])

Add a new column with the corresponding header and values to the dataframe

Arguments:
  • header (System.String) – The name of the new column
  • values (System.Double<System.Double>[]) – An array of size NumRows with all the values of the new column
public void AddColumn(string header, double[] values)
AddColumn(System.String, System.String[])

Add a new column with the corresponding header and values to the dataframe

Arguments:
  • header (System.String) – The name of the new column
  • values (System.String<System.String>[]) – An array of size NumRows with all the values of the new column
public void AddColumn(string header, string[] values)
AddRow(System.Object[])

Add a row to the DataFrame. The size of the tuple must be equal to the total number of columns in the dataframe.

Arguments:
  • values (System.Object<System.Object>[]) – A collectiong containing all the values for the row to be added
public void AddRow(params object[] values)
AddRow(ampl.Tuple)

Add a row to the DataFrame. The size of the tuple must be equal to the total number of columns in the dataframe.

Arguments:
  • value (ampl.Tuple) – A tuple containing all the values for the row to be added
public void AddRow(Tuple value)
AddRow(ampl.VariantRef)

Add a value to a DataFrame composed of only one column

Arguments:
  • a1 (ampl.VariantRef) – The value to add
public void AddRow(VariantRef a1)
AddRow(ampl.VariantRef, ampl.VariantRef)

Add a value to a DataFrame composed of two columns.

Arguments:
  • a1 (ampl.VariantRef) – Value for the first column
  • a2 (ampl.VariantRef) – Value for the second column
public void AddRow(VariantRef a1, VariantRef a2)
AddRow(ampl.VariantRef, ampl.VariantRef, ampl.VariantRef)

Add a value to a DataFrame composed of two columns.

Arguments:
  • a1 (ampl.VariantRef) – Value for the first column
  • a2 (ampl.VariantRef) – Value for the second column
  • a3 (ampl.VariantRef) – Value for the third column
public void AddRow(VariantRef a1, VariantRef a2, VariantRef a3)
AddRow(ampl.VariantRef, ampl.VariantRef, ampl.VariantRef, ampl.VariantRef)

Add a value to a DataFrame composed of two columns.

Arguments:
  • a1 (ampl.VariantRef) – Value for the first column
  • a2 (ampl.VariantRef) – Value for the second column
  • a3 (ampl.VariantRef) – Value for the third column
  • a4 (ampl.VariantRef) – Value for the fourth column
public void AddRow(VariantRef a1, VariantRef a2, VariantRef a3, VariantRef a4)
GetColumn(System.String)

Get the specified column as a view object

Arguments:
  • header (System.String) – The column name
Return type:

ampl.DataFrame.Column

public DataFrame.Column GetColumn(string header)
GetEnumerator()
Return type:System.Collections.Generic.IEnumerator<System.Collections.Generic.IEnumerator`1>{ampl.DataFrame.Row<ampl.DataFrame.Row>}
public IEnumerator<DataFrame.Row> GetEnumerator()
GetHeaders()

Get headers of all columns

Return type:System.String<System.String>[]
public string[] GetHeaders()
GetRow(System.Object[])

Get a row by value of the indexing columns. If the index is not specified, gets the only row of a dataframe with no indexing columns.

Arguments:
  • index (System.Object<System.Object>[]) – Collection of objects representing the index of the desired row
Return type:

ampl.DataFrame.Row

public DataFrame.Row GetRow(params object[] index)
GetRow(ampl.Tuple)

Get a row by value of the indexing columns. If the index is not specified, gets the only row of a dataframe with no indexing columns.

Arguments:
  • index (ampl.Tuple) – Tuple representing the index of the desired row
Return type:

ampl.DataFrame.Row

public DataFrame.Row GetRow(Tuple index)
GetRowByIndex(System.Int32)

Get row by numeric index

Arguments:
  • index (System.Int32) – Zero-based index of the row to get
Return type:

ampl.DataFrame.Row

Returns:

Corresponding row

public DataFrame.Row GetRowByIndex(int index)
Reserve(System.Int32)

Reserve space for the given number of rows. NOTE that the rows cannot be accessed, they still have to be added via DataFrame.AddRow.

Arguments:
  • numRows (System.Int32) – Number of rows to be resetved
public void Reserve(int numRows)
SetArray(System.Double[], System.Double[])

Set the values of a DataFrame from an array of doubles. The DataFrame must have one index and one data column.

Arguments:
  • indices (System.Double<System.Double>[]) – The indices of the values to set
  • values (System.Double<System.Double>[]) – An array containing the values to be set
public void SetArray(double[] indices, double[] values)
SetArray(System.Double[], System.String[])

Set the values of a DataFrame from an array of strings. The DataFrame must have one index and one data column.

Arguments:
  • indices (System.Double<System.Double>[]) – The indices of the values to set
  • values (System.String<System.String>[]) – An array containing the values to be set
public void SetArray(double[] indices, string[] values)
SetArray(System.String[], System.Double[])

Set the values of a DataFrame from an array of doubles. The DataFrame must have one index and one data column.

Arguments:
  • indices (System.String<System.String>[]) – The indices of the values to set
  • values (System.Double<System.Double>[]) – An array containing the values to be set
public void SetArray(string[] indices, double[] values)
SetArray(System.String[], System.String[])

Set the values of a DataFrame from an array of strings. The DataFrame must have one index and one data column.

Arguments:
  • indices (System.String<System.String>[]) – The indices of the values to set
  • values (System.String<System.String>[]) – An array containing the values to be set
public void SetArray(string[] indices, string[] values)
SetColumn(System.String, System.Double[])

Set the values of a column

Arguments:
  • header (System.String) – The header of the column to be set
  • values (System.Double<System.Double>[]) – The values to be set
public void SetColumn(string header, params double[] values)
SetColumn(System.String, System.String[])

Set the values of a column

Arguments:
  • header (System.String) – The header of the column to be set
  • values (System.String<System.String>[]) – The values to be set
public void SetColumn(string header, params string[] values)
SetMatrix(System.Double[], System.Double[], System.Double[])

Set a matrix of doubles to an empty DataFrame. The DataFrame must have two indices and one data column.

Arguments:
  • indices0 (System.Double<System.Double>[]) – The values of the first indexing column
  • indices1 (System.Double<System.Double>[]) – The values of the second indexing column
  • values (System.Double<System.Double>[,]) – A 2d-array of doubles
public void SetMatrix(double[] indices0, double[] indices1, double[, ] values)
SetMatrix(System.Double[], System.Double[], System.String[])

Set a matrix of strings to an empty DataFrame. The DataFrame must have two indices and one data column.

Arguments:
  • indices0 (System.Double<System.Double>[]) – The values of the first indexing column
  • indices1 (System.Double<System.Double>[]) – The values of the second indexing column
  • values (System.String<System.String>[,]) – A 2d-array of strings
public void SetMatrix(double[] indices0, double[] indices1, string[, ] values)
SetMatrix(System.Double[], System.String[], System.Double[])

Set a matrix of doubles to an empty DataFrame. The DataFrame must have two indices and one data column.

Arguments:
  • indices0 (System.Double<System.Double>[]) – The values of the first indexing column
  • indices1 (System.String<System.String>[]) – The values of the second indexing column
  • values (System.Double<System.Double>[,]) – A 2d-array of doubles
public void SetMatrix(double[] indices0, string[] indices1, double[, ] values)
SetMatrix(System.Double[], System.String[], System.String[])

Set a matrix of strings to an empty DataFrame. The DataFrame must have two indices and one data column.

Arguments:
  • indices0 (System.Double<System.Double>[]) – The values of the first indexing column
  • indices1 (System.String<System.String>[]) – The values of the second indexing column
  • values (System.String<System.String>[,]) – A 2d-array of strings
public void SetMatrix(double[] indices0, string[] indices1, string[, ] values)
SetMatrix(System.String[], System.Double[], System.Double[])

Set a matrix of doubles to an empty DataFrame. The DataFrame must have two indices and one data column.

Arguments:
  • indices0 (System.String<System.String>[]) – The values of the first indexing column
  • indices1 (System.Double<System.Double>[]) – The values of the second indexing column
  • values (System.Double<System.Double>[,]) – A 2d-array of doubles
public void SetMatrix(string[] indices0, double[] indices1, double[, ] values)
SetMatrix(System.String[], System.Double[], System.String[])

Set a matrix of strings to an empty DataFrame. The DataFrame must have two indices and one data column.

Arguments:
  • indices0 (System.String<System.String>[]) – The values of the first indexing column
  • indices1 (System.Double<System.Double>[]) – The values of the second indexing column
  • values (System.String<System.String>[,]) – A 2d-array of strings
public void SetMatrix(string[] indices0, double[] indices1, string[, ] values)
SetMatrix(System.String[], System.String[], System.Double[])

Set a matrix of doubles to an empty DataFrame. The DataFrame must have two indices and one data column.

Arguments:
  • indices0 (System.String<System.String>[]) – The values of the first indexing column
  • indices1 (System.String<System.String>[]) – The values of the second indexing column
  • values (System.Double<System.Double>[,]) – A 2d-array of doubles
public void SetMatrix(string[] indices0, string[] indices1, double[, ] values)
SetMatrix(System.String[], System.String[], System.String[])

Set a matrix of strings to an empty DataFrame. The DataFrame must have two indices and one data column.

Arguments:
  • indices0 (System.String<System.String>[]) – The values of the first indexing column
  • indices1 (System.String<System.String>[]) – The values of the second indexing column
  • values (System.String<System.String>[,]) – A 2d-array of strings
public void SetMatrix(string[] indices0, string[] indices1, string[, ] values)
GetEnumerator()
Return type:System.Collections.IEnumerator
IEnumerator IEnumerable.GetEnumerator()
ToString()

Return a tabular string representation of the dataframe object.

Return type:System.String
public override string ToString()

Properties

ampl.DataFrame.Item[System.Object[]]

Get the row corresponding to the specified index

Return type:ampl.DataFrame.Row
public DataFrame.Row this[params object[] index] { get; }
NumCols()

Get the total number of columns in this dataframe (indexarity + number of values)

Return type:System.Int32
public int NumCols { get; }
NumIndices()

Get the number of indices (indexarity) of this dataframe

Return type:System.Int32
public int NumIndices { get; }
NumRows()

Get the number of data rows in this dataframe

Return type:System.Int32
public int NumRows { get; }