Skip to content

Commit

Permalink
Add a kwarg constructor for DataFrames (JuliaData#228)
Browse files Browse the repository at this point in the history
Example: DataFrame(x = [1:4], y = rand(4))
  • Loading branch information
powerdistribution committed Apr 4, 2013
1 parent 3323b3a commit df8cee9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/dataframe.jl
Expand Up @@ -42,8 +42,15 @@ end
##
##############################################################################

# The empty DataFrame
DataFrame() = DataFrame({}, Index())
# A DataFrame from keyword arguments
# This also covers the empty DataFrame.
function DataFrame(;kwargs...)
result = DataFrame({}, Index())
for (k,v) in kwargs
result[string(k)] = v
end
return result
end

# No-op given a DataFrame
DataFrame(df::DataFrame) = df
Expand Down
4 changes: 4 additions & 0 deletions test/data.jl
Expand Up @@ -182,12 +182,16 @@ df3 = DataFrame({dvint})
df4 = DataFrame([1:4 1:4])
df5 = DataFrame({DataVector[1,2,3,4], dvstr})
df6 = DataFrame({dvint, dvint, dvstr}, ["A", "B", "C"])
df7 = DataFrame(x = dvint, y = dvstr)
@assert size(df7) == (4, 2)
@assert isequal(df7["x"], dvint)

test_group("description functions")
@assert nrow(df6) == 4
@assert ncol(df6) == 3
@assert all(colnames(df6) .== ["A", "B", "C"])
@assert all(colnames(df2) .== ["x1", "x2"])
@assert all(colnames(df7) .== ["x", "y"])

test_group("ref")
@assert df6[2, 3] == "two"
Expand Down

0 comments on commit df8cee9

Please sign in to comment.