-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathdataframes_api.jl
125 lines (107 loc) · 4.31 KB
/
dataframes_api.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import DataFrames.AbstractDataFrame
using DataFrames
@require Revise begin
Revise.track(PlotlyJS, @__FILE__)
end
# utilities
_has_group(df::AbstractDataFrame, group::Any) = false
_has_group(df::AbstractDataFrame, group::Symbol) = haskey(df, group)
function _has_group(df::AbstractDataFrame, group::Vector{Symbol})
all(x -> haskey(df, x), group)
end
_group_name(df::AbstractDataFrame, group::Symbol) = df[1, group]
function _group_name(df::AbstractDataFrame, groups::Vector{Symbol})
string("(", join([df[1, g] for g in groups], ", "), ")")
end
"""
$(SIGNATURES)
Build a trace of kind `kind`, using the columns of `df` where possible. In
particular for all keyword arguments, if the value of the keyword argument is a
Symbol and matches one of the column names of `df`, replace the value of the
keyword argument with the column of `df`
If `group` is passed and is a Symbol that is one of the column names of `df`,
then call `by(df, group)` and construct one trace per SubDataFrame, passing
all other keyword arguments. This means all keyword arguments are passed
applied to all traces
Also, when using this routine you can pass a function as a value for any
keyword argument. This function will be replaced by calling the function on the
DataFrame. For example, if I were to pass `name=(df) -> "Wage (average =
$(mean(df[:X1])))"` then the `name` attribute on the trace would be replaced by
the `Wage (average = XX)`, where `XX` is the average of the `X1` column in the
DataFrame.
The ability to pass functions as values for keyword arguments is particularly
useful when using the `group` keyword arugment, as the function will be applied
to each SubDataFrame. In the example above, the name attribute would set a
different mean for each group.
"""
function GenericTrace(df::AbstractDataFrame; group=nothing, kind="scatter", kwargs...)
d = Dict{Symbol,Any}(kwargs)
if _has_group(df, group)
_traces = by(df, group) do dfg
GenericTrace(dfg; kind=kind, name=_group_name(dfg, group), kwargs...)
end
return GenericTrace[t for t in _traces[:x1]]
else
!isa(group, Void) && warn("Unknown group $(group), skipping")
end
for (k, v) in d
if isa(v, Symbol) && haskey(df, v)
d[k] = df[v]
elseif isa(v, Function)
d[k] = v(df)
end
end
GenericTrace(kind; d...)
end
"""
$(SIGNATURES)
Pass the provided values of `x` and `y` as keyword arguments for constructing
the trace from `df`. See other method for more information
"""
function GenericTrace(df::AbstractDataFrame, x::Symbol, y::Symbol; kwargs...)
GenericTrace(df; x=x, y=y, kwargs...)
end
"""
$(SIGNATURES)
Pass the provided value `y` as keyword argument for constructing the trace from
`df`. See other method for more information
"""
function GenericTrace(df::AbstractDataFrame, y::Symbol; kwargs...)
GenericTrace(df; y=y, kwargs...)
end
"""
$(SIGNATURES)
Construct a plot using the columns of `df` if possible. For each keyword
argument, if the value of the argument is a Symbol and the `df` has a column
whose name matches the value, replace the value with the column of the `df`.
If `group` is passed and is a Symbol that is one of the column names of `df`,
then call `by(df, group)` and construct one trace per SubDataFrame, passing
all other keyword arguments. This means all keyword arguments are passed
applied to all traces
"""
function Plot(df::AbstractDataFrame, l::Layout=Layout();
style::Style=CURRENT_STYLE[], kwargs...)
Plot(GenericTrace(df; kwargs...), l, style=style)
end
"""
$(SIGNATURES)
Construct a plot from `df`, passing the provided values of x and y as keyword
arguments. See docstring for other method for more information.
"""
function Plot(d::AbstractDataFrame, x::Symbol, y::Symbol, l::Layout=Layout();
style::Style=CURRENT_STYLE[], kwargs...)
Plot(d, l; x=x, y=y, style=style, kwargs...)
end
"""
$(SIGNATURES)
Construct a plot from `df`, passing the provided value y as a keyword argument.
See docstring for other method for more information.
"""
function Plot(d::AbstractDataFrame, y::Symbol, l::Layout=Layout();
style::Style=CURRENT_STYLE[], kwargs...)
Plot(d, l; y=y, style=style, kwargs...)
end
for t in _TRACE_TYPES
str_t = string(t)
@eval $t(df::AbstractDataFrame; kwargs...) = GenericTrace(df; kind=$(str_t), kwargs...)
end