Home > database >  How to use instance subscript in Swift Tabular Data to access DataFrame Column as an Array
How to use instance subscript in Swift Tabular Data to access DataFrame Column as an Array

Time:10-13

I am attempting to turn a Swift TabularData DataFrame column into an array. The Apple Developer Documentation here describes the following Instance Subscript that does exactly that:

subscript<T>(columnName: String, type: T.Type = T.self) -> [T?] { get set }

However, when I attempt to the following code on a DataFrame (named elevationsDf) with a column (named "PLoss"), it returns a Column<Double> instead of a [Double].

print(type(of: elevationsDf["PLoss", Double.self]))

What should I change to address this issue?

CodePudding user response:

Well, DataFrame also has this subscript:

subscript<T>(columnName: String, type: T.Type) -> Column<T> { get set }

And that is what your code resolves to.

To call the subscript that you want, you can specify the type of the result explicitly:

let c: [Int?] = df["a", Int.self]

Or just:

let c: [Int?] = df["a"]
  • Related