PTable

public struct PTable
extension PTable: CustomStringConvertible
extension PTable: Equatable

A collection of named PColumns.

A PTable, also known as a data frame, represents a tabular collection of data.

Invariants:

  • Each column must have the same number of elements.
  • Column names are unique.
  • Initializes a PTable from a sequence of String, PColumn pairs.

    Throws

    PError.colCountMisMatch if the column lengths are not equal.

    Throws

    PError.duplicateColumnName if the name of a column is duplicated.

    Declaration

    Swift

    public init(_ columns: [(String, PColumn)]) throws
  • Initializes a PTable from a dictionary mapping from Strings to PColumns.

    Throws

    PError.colCountMisMatch if the column lengths are not equal.

    Declaration

    Swift

    public init(_ columns: [String : PColumn]) throws
  • Accesses the PColumn with a given name.

    Declaration

    Swift

    public subscript(columnName: String) -> PColumn? { get set }

    Parameters

    columnName

    The name of the column to access.

  • Selects a subset of columns to form a new PTable.

    Declaration

    Swift

    public subscript(columnNames: [String]) -> PTable { get }

    Parameters

    columnNames

    The list of column names to include in the new PTable. Each element must be unique, and must refer to a valid column in this PTable.

  • Builds a new PTable selecting only rows set in the index set.

    Declaration

    Swift

    public subscript(indexSet: PIndexSet) -> PTable { get }

    Parameters

    indexSet

    The set of rows to include in the new table. The PIndexSet must have the same number of rows (count) as the `PTable.

  • Access an element at a given row and column.

    Note: this subscript operation is generic over the return type; as a result you need to tell Swift what type you expect to come out based on your knowledge of the storage type of the underlying column. See the following example:

    var myValue: Double = myTable["myColumnOfDoubles", 23]
    myValue += 103
    myTable["myColumnOfDoubles", 23] = myValue
    

    Note: although this is an O(1) operation, it is relatively inefficient. If you need to compute a result over a large number of rows, look at the writing your operation against a PTypedColumn type instead.

    Declaration

    Swift

    public subscript<T>(columnName: String, index: Int) -> T? where T : PCSVParsible, T : PDefaultInit, T : PStringParsible, T : Comparable, T : Hashable { get set }

    Parameters

    columnName

    The name of the column to access.

    index

    The offset into the column to access.

  • The names of the columns contained in this PTable.

    Declaration

    Swift

    public var columnNames: [String] { get set }
  • Rename a column.

    Declaration

    Swift

    public mutating func rename(_ col: String, to newName: String) throws

    Parameters

    col

    The name of the column currently.

    newName

    The new name of the column.

  • Drops columns.

    This is the safe variation of drop(_:), which will throw an error if there is a problem with a provided column name.

    Note: this function is implemented such that it either fully succeeds or throws an error, and will never leave the Table in an inconsistent state.

    Declaration

    Swift

    public mutating func drop(columns: String...) throws
  • Drops columns.

    If a column name does not exist in the PTable, it is silently ignored.

    Declaration

    Swift

    public mutating func drop(_ columns: String...)
  • Drops rows that contain nils.

    Declaration

    Swift

    public mutating func dropNils(columns: [String]? = nil)

    Parameters

    columns

    if nil (default), filtering occurs over all columns, otherwise only rows containing nils in the specified subset of columns are dropped.

  • Returns a new PTable where rows containing nils have been dropped.

    Declaration

    Swift

    public func droppedNils(columns: [String]? = nil) -> PTable

    Parameters

    columns

    if nil (default), filtering occurs over all columns, otherwise only rows containing nils in the specified subset of columns are dropped.

  • Sorts the PTable (in place) based on elements in the named column.

    This sort is guaranteed to be stable, such that if the elements of column columnName are equal, than they will appear in the same order after sorting as before.

    Declaration

    Swift

    public mutating func sort(by columnName: String, ascending order: Bool = true)

    Parameters

    columnName

    The name of the column to use to sort.

    order

    true (default) for ascending, false for descending.

  • Sorts the PTable (in place) based on elements in the named columns.

    This sort is guaranteed to be stable.

    Declaration

    Swift

    public mutating func sort(
      by columnName1: String, ascending c1Order: Bool = true, _ columnName2: String,
      ascending c2Order: Bool = true
    )

    Parameters

    columnName1

    The name of the first column to use to sort.

    c1Order

    true for ascending ordering of columnName1, false otherwise.

    columnName2

    The name of the second column to use to sort.

    c2Order

    true for ascending ordering of columnName2, false otherwise.

  • Undocumented

    Declaration

    Swift

    public func sorted(by columnName: String, ascending: Bool = true) -> PTable
  • Undocumented

    Declaration

    Swift

    public func sorted(
      by c1: String, ascending c1Order: Bool = true, _ c2: String, ascending c2Order: Bool = true
    ) -> PTable
  • Perform a “group-by” operation, reducing the groups with aggregations.

    Declaration

    Swift

    public func group(
      by column: String,
      applying aggregations: Aggregation...
    ) throws -> PTable

    Parameters

    column

    Group rows in PTable based on elements in this column.

    aggregations

    The set of aggregations to apply.

  • Perform a “group-by” operation, reducing the groups with aggregations.

    Declaration

    Swift

    public func group(
      by columnNames: [String],
      applying aggregations: Aggregation...
    ) throws -> PTable

    Parameters

    columnNames

    Group rows in PTable based on elements in these columns.

    aggregations

    The set of aggregations to apply.

  • Perform a “group-by” operation, reducing the groups with aggregations.

    Declaration

    Swift

    public func group(
      by columnNames: [String],
      applying aggregations: [Aggregation]
    ) throws -> PTable

    Parameters

    columnNames

    Group rows in PTable based on elements in these columns.

    aggregations

    The set of aggregations to apply.

  • Undocumented

    Declaration

    Swift

    public mutating func joined(with other: PTable, onColumn joinColumnName: String) throws
  • Undocumented

    Declaration

    Swift

    public func join(with other: PTable, onColumn joinColumnName: String) throws -> PTable
  • The number of rows contained within the PTable.

    If there are no PColumns in the table, count returns nil.

    Declaration

    Swift

    public var count: Int? { get }
  • Computes summaries for each column contained within this PTable.

    Declaration

    Swift

    public func summarize() -> [(String, PColumnSummary)]
  • A string representation of a (subset) of the table.

    Declaration

    Swift

    public var description: String { get }
  • Returns true iff lhs and rhs contain identical data, false otherwise.

    Declaration

    Swift

    public static func == (lhs: PTable, rhs: PTable) -> Bool
  • Undocumented

    Declaration

    Swift

    public init(csv file: String) throws
  • Undocumented

    Declaration

    Swift

    public func tmap<
      O: ElementRequirements,
      T1: ElementRequirements
    >(
      _ c1: String,
      fn: (T1) throws -> O
    ) rethrows -> PTypedColumn<O>
  • Undocumented

    Declaration

    Swift

    public func tmap<
      O: ElementRequirements,
      T1: ElementRequirements,
      T2: ElementRequirements
    >(
      _ c1: String,
      _ c2: String,
      fn: (T1, T2) throws -> O
    ) rethrows -> PTypedColumn<O>
  • Undocumented

    Declaration

    Swift

    public func tmap<
      O: ElementRequirements,
      T1: ElementRequirements,
      T2: ElementRequirements,
      T3: ElementRequirements
    >(
      _ c1: String,
      _ c2: String,
      _ c3: String,
      fn: (T1, T2, T3) throws -> O
    ) rethrows -> PTypedColumn<O>
  • Undocumented

    Declaration

    Swift

    public func tmap<
      O: ElementRequirements,
      T1: ElementRequirements,
      T2: ElementRequirements,
      T3: ElementRequirements,
      T4: ElementRequirements
    >(
      _ c1: String,
      _ c2: String,
      _ c3: String,
      _ c4: String,
      fn: (T1, T2, T3, T4) throws -> O
    ) rethrows -> PTypedColumn<O>
  • Undocumented

    Declaration

    Swift

    public func tmap<
      O: ElementRequirements,
      T1: ElementRequirements,
      T2: ElementRequirements,
      T3: ElementRequirements,
      T4: ElementRequirements,
      T5: ElementRequirements
    >(
      _ c1: String,
      _ c2: String,
      _ c3: String,
      _ c4: String,
      _ c5: String,
      fn: (T1, T2, T3, T4, T5) throws -> O
    ) rethrows -> PTypedColumn<O>
  • Undocumented

    Declaration

    Swift

    public func tmap<
      O: ElementRequirements,
      T1: ElementRequirements,
      T2: ElementRequirements,
      T3: ElementRequirements,
      T4: ElementRequirements,
      T5: ElementRequirements,
      T6: ElementRequirements
    >(
      _ c1: String,
      _ c2: String,
      _ c3: String,
      _ c4: String,
      _ c5: String,
      _ c6: String,
      fn: (T1, T2, T3, T4, T5, T6) throws -> O
    ) rethrows -> PTypedColumn<O>