Creates a data frame from CSV data.
- Parameters:
- data: The contents of a CSV file in a
<doc://com.apple.documentation/documentation/Foundation/Data> instance.
- columns: An array of column names; Set to `nil` to use every column in the CSV file.
- rows: A range of indices; Set to `nil` to use every row in the CSV file.
- types: A dictionary of column names and their CSV types.
The data frame infers the types for column names that aren't in the dictionary.
- options: The options that tell the data frame how to read the CSV data.
- Throws: A `CSVReadingError` instance.
Creates a data frame by converting JSON data.
The JSON file should contain a sequence of objects where each object contains a value for every column name.
Here's an example with two columns "id" and "name":
[
{"id": 1, "name": "foo"},
{"id": 2, "name": "bar"},
]
- Parameters:
- data: The contents of a JSON file as data.
- columns: An array of column names; Set to `nil` to use every column in the JSON file.
- types: A dictionary of column names and their JSON types.
The data frame infers the types for column names that aren't in the dictionary.
- options: The options that instruct how the data frame reads the JSON file.
- Throws: A `JSONReadingError` instance.
Returns the column names for an alias.
Use this method to discover whether an alias refers to more than one column.
For example, a data frame may have multiple columns with the same name
after you call its ``joined(_:on:kind:)-6moa8`` method.
get · iOS 15.0+
description
TypeScript
get description(): string
Swift declaration
var description: String { get }
A text representation of the data frame.
method · iOS 15.0+
removeAlias
TypeScript
removeAlias(alias: string): void
Swift declaration
mutating func removeAlias(_ alias: String)
Removes an alternative name for a column.
- Parameter alias: An additional name for the column.
Removes a column you select by its name from the data frame.
- Parameter name: The name of a column in the data frame.
- Returns: The column the method removes from the data frame.
Removes a column you select by its column identifier from the data frame.
- Parameter id: The identifier of a column in the data frame.
- Returns: The column the method removes from the data frame.
method · iOS 15.0+
renameColumn
TypeScript
renameColumn(name: string, newName: string): void
Swift declaration
mutating func renameColumn(_ name: String, to newName: String)
Renames a column in the data frame.
- Parameters:
- name: The name of a column in the data frame.
- newName: The new name for the column. The new name must not be the
same as another column in the data frame.
Returns the index of a column.
- Parameter columnName: The name or an alias of the column.
- Returns: An integer if the column name or alias exists in the data frame;
otherwise, `nil`.
This method's complexity is O(*1*).
mutating func replaceColumn<T>(_ id: ColumnID<T>, with newColumn: AnyColumn)
Replaces a column in the data frame, by column identifier, with a type-erased column.
- Parameters:
- id: The identifier of a column in the data frame.
- newColumn: Another column that replaces the column.
mutating func replaceColumn<T, U>(_ id: ColumnID<T>, with newColumn: Column<U>)
Replaces a column in the data frame, by column identifier, with a typed column.
- Parameters:
- id: The identifier of a column in the data frame.
- newColumn: Another column that replaces the column.
mutating func replaceColumn(_ name: String, with newColumn: AnyColumn)
Replaces a column in the data frame, by name, with a type-erased column.
- Parameters:
- name: The name of a column in the data frame.
- newColumn: Another column that replaces the column.
mutating func replaceColumn<T>(_ name: String, with newColumn: Column<T>)
Replaces a column in the data frame, by name, with a typed column.
- Parameters:
- name: The name of a column in the data frame.
- newColumn: Another column that replaces the column.
method · iOS 15.0+
appendEmptyRow
TypeScript
appendEmptyRow(): void
Swift declaration
mutating func appendEmptyRow()
Adds an empty row to the data frame.
Each value in an empty row is `nil`.
Merges two columns that you select by column identifier into a new column.
- Parameters:
- columnID1: The identifier of a column.
- columnID2: The identifier of another column.
- newColumnName: The name of the new column that replaces the two columns.
- transform: A closure that combines the corresponding elements of the two columns into one element.
The merged column replaces the original column.
Merges two columns that you select by name into a new column.
- Parameters:
- columnName1: The name of a column.
- columnName2: The name of another column.
- newColumnName: The name of the new column that replaces the two columns.
- transform: A closure that combines the corresponding elements of the two columns into one element.
The merged column replaces the original column.
Merges three columns that you select by column identifier into a new column.
- Parameters:
- columnID1: The identifier of a column.
- columnID2: The identifier of a second column.
- columnID3: The identifier of a third column.
- newColumnName: The name of the new column that replaces the three columns.
- transform: A closure that combines the corresponding elements of the three columns into one element.
The merged column replaces the original column.
Merges three columns that you select by name into a new column.
- Parameters:
- columnName1: The name of a column.
- columnName2: The name of a second column.
- columnName3: The name of a third column.
- newColumnName: The name of the new column that replaces the three columns.
- transform: A closure that combines the corresponding elements of the three columns into one element.
The merged column replaces the original column.
method · iOS 15.4+
containsColumn
TypeScript
containsColumn(id: ColumnID<unknown>): boolean
Swift declaration
func containsColumn<T>(_ id: ColumnID<T>) -> Bool
Returns a Boolean value indicating whether the data frame contains a column matching a column ID.
- Parameter id: A column ID.
method · iOS 16.0+
containsColumn
TypeScript
containsColumn17205(name: string): boolean
Swift declaration
func containsColumn(_ name: String) -> Bool
Returns a Boolean value indicating whether the data frame contains a column.
- Parameters:
- name: A column name.
Applies a transform closure that modifies the nonempty elements of a column
you select by column identifier.
- Parameters:
- id: The identifier of a column in the data frame.
- transform: A closure that transforms each non-`nil` element in the column.
Applies a transform closure that modifies the elements of a column you select by column identifier.
- Parameters:
- id: The identifier of a column in the data frame.
- transform: A closure that transforms each element in the column.
Applies a transform closure that modifies the nonempty elements of a column you select by name.
- Parameters:
- name: The name of a column in the data frame.
- transform: A closure that transforms each element in the column.
Applies a transform closure that modifies the elements of a column you select by name.
- Parameters:
- name: The name of a column in the data frame.
- transform: A closure that transforms each element in the column.
get · iOS 15.0+
debugDescription
TypeScript
get debugDescription(): string
Swift declaration
var debugDescription: String { get }
A text representation of the data frame suitable for debugging.
Creates a data frame from a CSV file.
- Parameters:
- url: A URL for a CSV file.
- columns: An array of column names; Set to `nil` to use every column in the CSV file.
- rows: A range of indices; Set to `nil` to use every row in the CSV file.
- types: A dictionary of column names and their CSV types.
The data frame infers the types for column names that aren't in the dictionary.
- options: The options that tell the data frame how to read the CSV file.
- Throws: A `CSVReadingError` instance.
Creates a data frame by reading a JSON file.
The JSON file should contain a sequence of objects where each object contains a value for every column name.
Here's an example with two columns "id" and "name":
[
{"id": 1, "name": "foo"},
{"id": 2, "name": "bar"},
]
- Parameters:
- url: A URL to a JSON file.
- columns: An array of column names; Set to `nil` to use every column in the JSON file.
- types: A dictionary of column names and their JSON types.
The data frame infers the types for column names that aren't in the dictionary.
- options: The options that instruct how the data frame reads the JSON file.
- Throws: A `JSONReadingError` instance.
Creates a data frame from a Turi Create scalable data frame.
- Parameters:
- url: A URL to an `SFrame` directory.
- columns: An array of column names; Set to `nil` to use every column in the `SFrame`.
- rows: A range of indices; Set to `nil` to use every row in the `SFrame`.
- Throws: An `SFrameReadingError` instance.
subscript · iOS 15.0+
subscript
TypeScript
subscriptRow(index: number): DataFrame_Row
Swift declaration
subscript(row index: Int) -> DataFrame.Row { get set }
Accesses a row by its index.
- Parameter index: The index of a row.
get · iOS 15.0+
base
TypeScript
get base(): DataFrame
Swift declaration
var base: DataFrame { get }
The underlying data frame.
For a ``DataFrame`` instance, this property is equivalent to `self`.
Arranges the rows of a data frame according to a column that you select by its column identifier,
with a predicate.
- Parameters:
- columnID: The identifier of a column.
- areInIncreasingOrder: A closure that returns a Boolean that indicates
whether the two elements are in increasing order.
> Note: Elements with a value of `nil` are less than all non-`nil` values.
mutating func sort(on columnName: String, order: Order = .ascending)
Arranges the rows of a data frame according to a column that you select by its name.
- Parameters:
- columnName: The name of a column.
- order: A sorting order.
This is a convenience method that only works for columns of the following types:
- <doc://com.apple.documentation/documentation/Swift/Bool>
- <doc://com.apple.documentation/documentation/Swift/Int>
- <doc://com.apple.documentation/documentation/Swift/Float>
- <doc://com.apple.documentation/documentation/Swift/Double>
- <doc://com.apple.documentation/documentation/Foundation/Date>
> Note: Elements with a value of `nil` are less than all non-`nil` values.
get · iOS 15.0+
shape
TypeScript
get shape(): { "rows": number; "columns": number }
Swift declaration
var shape: (rows: Int, columns: Int) { get }
The number of rows and columns in the data frame.
- Parameters:
- rows: The number of rows in the data frame.
- columns: The number of columns in the data frame.
Adds a dictionary's values as a row to the data frame.
- Parameter dictionary: A dictionary of values whose key is a column's name.
Each key in the dictionary must be the name or alias of a column in the data frame.
Each value in the dictionary must be of the same types as the corresponding column.
method · iOS 15.0+
append
TypeScript
appendRow(row: DataFrame_Row): void
Swift declaration
mutating func append(row: DataFrame.Row)
Adds a row of values to the data frame.
- Parameter row: A row from a data frame.
method · iOS 15.0+
append
TypeScript
appendColumn(column: Column<unknown>): void
Swift declaration
mutating func append<T>(column: Column<T>)
Adds a typed column to the end of the data frame.
- Parameter column: A typed column.
The column must have the same number of rows as the data frame,
and must not have the same name as another column in the data frame.
The column you append becomes the last column and has the highest index
in the data frame.
method · iOS 15.0+
append
TypeScript
appendColumn3352(column: AnyColumn): void
Swift declaration
mutating func append(column: AnyColumn)
Adds a type-erased column to the end of the data frame.
- Parameter column: A type-erased column.
The column must have the same number of rows as the data frame,
and must not have the same name as another column in the data frame.
method · iOS 15.0+
append
TypeScript
appendRowsOf(other: DataFrame): void
Swift declaration
mutating func append(rowsOf other: DataFrame)
Adds the rows of another data frame that has the same column names and types.
This method raises a fatal error if the data frame columns don't match.
The following code shows how to check that the columns match.
```
let sameColumns = left.columns.count == right.columns.count && zip(left.columns, right.columns).allSatisfy {
$0.name == $1.name && $0.wrappedElementType == $1.wrappedElementType
}
```
- Parameter other: Another data frame that has the same number of columns.
The columns in `other` must have the same names and types as the columns in the data frame.
method · iOS 15.0+
append
TypeScript
append(other: DataFrame_Slice): void
Swift declaration
mutating func append(_ other: DataFrame.Slice)
Adds the rows of a slice from a data frame.
- Parameter other: A slice of a data frame. The columns in `other` that have
the same name as columns in the data frame must also have the same type.
The method ignores columns in `other` that don't exist in the data frame.
The method fills the values for columns in the data frame that don't exist in `other` to `nil`.
method · iOS 15.0+
append
TypeScript
append64411(other: DataFrame): void
Swift declaration
mutating func append(_ other: DataFrame)
Adds the rows of another data frame.
The method ignores columns in `other` that don't exist in the data frame.
It fills the values for columns in the data frame that don't exist in `other` to `nil`.
It raises a fatal error if columns with the same name have different types.
The following code shows how to check that the columns match.
```
func columnTypesAreEqual(_ left: DataFrame, _ right: DataFrame) -> Bool {
for rightColumn in right.columns {
guard let index = left.indexOfColumn(rightColumn.name) else {
continue
}
let leftColumn = left.columns[index]
if leftColumn.wrappedElementType != rightColumn.wrappedElementType {
return false
}
}
return true
}
```
- Parameter other: Another data frame. The columns in `other` that have the same name as columns in the data
frame must also have the same type.
subscript · iOS 15.0+
subscript
TypeScript
subscriptColumn(index: number): AnyColumn
Swift declaration
subscript(column index: Int) -> AnyColumn { get set }
Accesses a column by its index.
- Parameter index: The index of a column.
Returns a selection of rows that satisfy a predicate in the columns you select by column identifier.
- Parameters:
- columnID: The identifier of a column in the data frame.
- isIncluded: A predicate closure that receives an element of the column as its argument
and returns a Boolean that indicates whether the slice includes the element's row.
- Returns: A data frame slice that contains the rows that satisfy the predicate.
Returns a selection of rows that satisfy a predicate.
- Parameter isIncluded: A predicate closure that receives an row and
returns a Boolean that indicates whether the slice includes that row.
- Returns: A data frame slice that contains the rows that satisfy the predicate.
mutating func insert(row: DataFrame.Row, at index: Int)
Adds a row of values at a position in the data frame.
- Parameters:
- row: A row from a data frame.
- index: A row position in the data frame.
The method inserts the new row before the row currently at `index`.
If you pass the array's `shape.rows` property as the `index` parameter,
the method appends the new row to the data frame.
mutating func insert<T>(column: Column<T>, at index: Int)
Adds a typed column at a position in the data frame.
- Parameters:
- column: A typed column.
The column must have the same number of rows as the data frame,
and must not have the same name as another column in the data frame.
- index: A column position in the data frame.
The method inserts the new column before the column currently at `index`.
If you pass the array's `shape.columns` property as the `index` parameter,
the method appends the new column to the data frame.
mutating func insert(column: AnyColumn, at index: Int)
Adds a type-erased column at a position in the data frame.
- Parameters:
- column: A type-erased column.
The column must have the same number of rows as the data frame,
and must not have the same name as another column in the data frame.
- index: A column position in the data frame.
The method inserts the new column before the column currently at `index`.
If you pass the array's `shape.column` property as the `index` parameter,
the method appends the new column to the data frame.
method · iOS 15.0+
prefix
TypeScript
prefix(maxLength: number): DataFrame_Slice
Swift declaration
func prefix(_ maxLength: Int) -> DataFrame.Slice
Returns a slice that contains the initial rows up to a maximum length.
- Parameter maxLength: The maximum number of rows.
method · iOS 15.0+
suffix
TypeScript
suffix(maxLength: number): DataFrame_Slice
Swift declaration
func suffix(_ maxLength: Int) -> DataFrame.Slice
Returns a slice that contains the final rows up to a maximum length.
- Parameter maxLength: The maximum number of rows.
Adds an alternative name for a column.
- Parameters:
- alias: An additional name for the column.
- columnName: The name of a column.
method · iOS 15.0+
removeRow
TypeScript
removeRow(index: number): void
Swift declaration
mutating func removeRow(at index: Int)
Removes a row from the data frame.
- Parameter index: A row position in the data frame.
method · iOS 15.0+
selecting
TypeScript
selecting(columnNames: Array<string>): DataFrame
Swift declaration
func selecting<S>(columnNames: S) -> DataFrame where S : Sequence, S.Element == String
Generates a data frame that includes the columns you select with a sequence of names.
- Parameter columnNames: A sequence of column names.
- Returns: A new data frame.
init · iOS 15.0+
init
TypeScript
static init(): DataFrame
Swift declaration
init()
Creates an empty data frame with no rows or columns.
subscript<C>(mask: C) -> DataFrame.Slice where C : Collection, C.Element == Bool { get }
Returns a slice of the rows by masking its elements with a Boolean column.
- Parameter mask: A Boolean column that indicates whether the method includes a row in the slice.
subscript<S>(columnNames: S) -> DataFrame where S : Sequence, S.Element == String { get }
Generates a data frame that includes the columns in a sequence of column names.
- Parameter columnNames: A sequence of column names.
- Returns: A new data frame.