/
wheredataframe.jl
156 lines (132 loc) · 5.56 KB
/
wheredataframe.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
WhereDataFrame{<:AbstractDataFrame,<:AbstractIndex}
The result of a [`where`](@ref) operation on an `AbstractDataFrame`; a
subset of a `AbstractDataFrame`
Not meant to be constructed directly, see `where`.
"""
struct WhereDataFrame{D<:AbstractDataFrame, T<:AbstractVector{Int}}
parent::D
rows::T
end
Base.@propagate_inbounds function WhereDataFrame(parent::AbstractDataFrame, rows::AbstractVector{Bool})
if length(rows) != nrow(parent)
throw(ArgumentError("invalid length of `AbstractVector{Bool}` row index" *
" (got $(length(rows)), expected $(nrow(parent)))"))
end
return WhereDataFrame(parent, findall(rows))
end
rows(wdf::WhereDataFrame) = getfield(wdf, :rows)
Base.parent(wdf::WhereDataFrame) = getfield(wdf, :parent)
"""
where(d::AbstractDataFrame, args...)
`where` introduces a `where` clause, which will be applied in the next function.
`args...` obey the same syntax as `select(d, args...)`
Rows that return missing are understood as false
Examples:
- `filter`/`filter!` (resp `delete!`) returns an AbstractDataFrame after filtering (resp. deleting) specified rows
- `select`/`select!` and `transform/transform` returns an AbstractDataFrame with as many rows as the original `AbstractDataFrame` after applying the transformation on specified rows
- `view`, `combine`, `describe` return the same thing as the function applied to a view of the `AbstractDataFrame`
# Examples
```julia
julia> df = DataFrame(a = repeat([1, 2, 3, missing], outer=[2]),
b = repeat([2, 1], outer=[4]),
c = randn(8))
julia> wdf = where(df, :a => x -> x .> 1)
julia> wdf = where(df, :a => x -> x .> 1, :b => x -> x .< 2)
julia> filter(wdf)
julia> delete!(wdf)
julia> view(wdf)
julia> describe(wdf)
julia> combine(wdf, :a => sum)
```
"""
function where(df::AbstractDataFrame, args...)
dfr = select(df, args...)
if any(x -> !(eltype(x) <: Union{Bool, Missing}), eachcol(dfr))
throw("Conditions do not evaluate to bool or missing")
end
if size(dfr, 2) == 1
WhereDataFrame(df, coalesce.(dfr[!, 1], false))
else
WhereDataFrame(df, coalesce.(.&(eachcol(dfr)...), false))
end
end
##############################################################################
##
## Show: show rows of parent that satisfies the `where` condition
## with original row number
##
##############################################################################
function Base.summary(io::IO, wdf::WhereDataFrame)
print(io, "Where DataFrame")
end
function Base.show(io::IO,
wdf::WhereDataFrame;
allrows::Bool = !get(io, :limit, false),
allcols::Bool = !get(io, :limit, false),
splitcols = get(io, :limit, false),
rowlabel::Symbol = :Row,
eltypes::Bool = true,
truncate::Int = 32)
summary(io, wdf)
_show(io, wdf, allrows=allrows, allcols=allcols, splitcols=splitcols,
rowlabel=rowlabel, eltypes=eltypes, truncstring=truncate)
end
function _show(io::IO,
wdf::WhereDataFrame;
allrows::Bool = !get(io, :limit, false),
allcols::Bool = !get(io, :limit, false),
splitcols = get(io, :limit, false),
rowlabel::Symbol = :Row,
eltypes::Bool = true,
truncstring::Int)
df = parent(wdf)
_check_consistency(df)
# we will pass around this buffer to avoid its reallocation in ourstrwidth
buffer = IOBuffer(Vector{UInt8}(undef, 80), read=true, write=true)
nrows = length(rows(wdf))
dsize = displaysize(io)
availableheight = dsize[1] - 7
nrowssubset = fld(availableheight, 2)
bound = min(nrowssubset - 1, nrows)
if allrows || nrows <= availableheight
rowindices1 = rows(wdf)[1:nrows]
rowindices2 = 1:0
else
rowindices1 = rows(wdf)[1:bound]
rowindices2 = rows[max(bound + 1, nrows - nrowssubset + 1):nrows]
end
maxwidths = getmaxwidths(df, io, rowindices1, rowindices2, rowlabel, nothing,
eltypes, buffer, truncstring)
width = getprintedwidth(maxwidths)
showrows(io, df, rowindices1, rowindices2, maxwidths, splitcols, allcols,
rowlabel, false, eltypes, nothing, buffer, truncstring)
return
end
##############################################################################
##
## Some operations have special meanings
##
##############################################################################
Base.filter(wdf::WhereDataFrame) = parent(wdf)[rows(wdf), :]
Base.filter!(wdf::WhereDataFrame) = delete!(parent(wdf), setdiff(1:nrow(parent(wdf)), rows(wdf)))
Base.delete!(wdf::WhereDataFrame) = delete!(parent(wdf), rows(wdf))
Base.view(wdf::WhereDataFrame) = view(parent(wdf), rows(wdf), :)
function nonunique(wdf::WhereDataFrame, args...)
x = falses(size(parent(wdf), 1))
idx = rows(wdf)[findall(nonunique(view(wdf), args...))]
x[idx] .= true
return x
end
Base.unique!(wdf::WhereDataFrame, args...) = delete!(parent(wdf), findall(nonunique(wdf, args...)))
Base.unique(wdf::WhereDataFrame, args...) = parent(wdf)[(!).(nonunique(wdf, args...)), :]
# TODO:
# Base.select
# Base.select!
# Base.transform
# Base.transform!
DataFrame(wdf::WhereDataFrame; copycols::Bool=true) = DataFrame(view(wdf); copycols = copycols)
Base.first(df::WhereDataFrame, args...) = first(view(df), args...)
Base.last(df::WhereDataFrame, args...) = last(view(df), args...)
DataAPI.describe(wdf::WhereDataFrame, args...; kwargs...) = describe(view(wdf), args...; kwargs...)
combine(wdf::WhereDataFrame, args...; kwargs...) = combine(view(wdf), args...; kwargs...)