Skip to content

Latest commit

 

History

History
40 lines (30 loc) · 1.16 KB

README.md

File metadata and controls

40 lines (30 loc) · 1.16 KB

Typed DataFrames

This package exports a macro @withcols that allows one to add required columns to the DataFrame type within a function annotation. This is just a wrapper around the julia DataFrames package

For example

using DataFrames
using TypedDataFrames

# This function requires a dataframe with the columns :a and :b
@withcols function sumfirst(df::AbstractDataFrame[:a, :b])
    return df[1, :a] + df[1, :b]
end

sumfirst(DataFrame(a=[1,2], b=[3,4])) # 4

# Can have extra columns
sumfirst(DataFrame(a=[1,2], b=[3,4], c=[5,6])) # 4

# When a column is missing, throws an AssertionError
sumfirst(DataFrame(a=[1,2], c=[5,6]))
# ERROR: AssertionError: Missing columns [b]

# We can also specify types for the columns
@withcols function firstnameandage(df::AbstractDataFrame[:name=>String, :age=>Int64])
    return df[1, :name] * ':' * string(df[1, :age])
end

# When the types are right
firstnameandage(DataFrame(name=["John"], age=[37]))

# And when we make a mistake with the type
firstnameandage(DataFrame(name=[3], age=[37]))
# ERROR: AssertionError: Invalid Column Types [
#  name:- expected String != got Int64
# ]