Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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].

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

What should I change to address this issue?

>Solution :

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"]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading