/
unique.jl
381 lines (334 loc) · 11.1 KB
/
unique.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
"""
nonunique(df::AbstractDataFrame; keep::Symbol=:first)
nonunique(df::AbstractDataFrame, cols; keep::Symbol=:first)
Return a `Vector{Bool}` in which `true` entries indicate duplicate rows.
Duplicate rows are those for which at least another row contains equal values
(according to `isequal`) for all columns in `cols` (by default, all columns).
If `keep=:first` (the default), only the first occurrence of a set of duplicate
rows is indicated with a `false` entry.
If `keep=:last`, only the last occurrence of a set of duplicate rows is
indicated with a `false` entry.
If `keep=:noduplicates`, only rows without any duplicates are indicated with a
`false` entry.
# Arguments
- `df` : `AbstractDataFrame`
- `cols` : a selector specifying the column(s) or their transformations to
compare. Can be any column selector or transformation accepted by
[`select`](@ref) that returns at least one column if `df` has at least one
column.
See also [`unique`](@ref) and [`unique!`](@ref).
# Examples
```jldoctest
julia> df = DataFrame(i=1:4, x=[1, 2, 1, 2])
4×2 DataFrame
Row │ i x
│ Int64 Int64
─────┼──────────────
1 │ 1 1
2 │ 2 2
3 │ 3 1
4 │ 4 2
julia> df = vcat(df, df)
8×2 DataFrame
Row │ i x
│ Int64 Int64
─────┼──────────────
1 │ 1 1
2 │ 2 2
3 │ 3 1
4 │ 4 2
5 │ 1 1
6 │ 2 2
7 │ 3 1
8 │ 4 2
julia> nonunique(df)
8-element Vector{Bool}:
0
0
0
0
1
1
1
1
julia> nonunique(df, keep=:last)
8-element Vector{Bool}:
1
1
1
1
0
0
0
0
julia> nonunique(df, 2)
8-element Vector{Bool}:
0
0
1
1
1
1
1
1
```
"""
function nonunique(df::AbstractDataFrame; keep::Symbol=:first)
if !(keep in (:first, :last, :noduplicates))
throw(ArgumentError("`keep` must be :first, :last, or :noduplicates"))
end
nrow(df) == 0 && return Bool[]
res = fill(true, nrow(df))
cols = ntuple(i -> df[!, i], ncol(df))
if keep == :first
rpa = refpool_and_array.(cols)
refpools = first.(rpa)
refarrays = last.(rpa)
# if refarray cannot be used, we can avoid allocating a groups vector
if any(isnothing, refpools) || any(isnothing, refarrays)
_, _, gslots, _ = row_group_slots!(cols, Val(true), nothing,
false, nothing, false)
# unique rows are the first encountered group representatives,
# nonunique are everything else
@inbounds for g_row in gslots
g_row > 0 && (res[g_row] = false)
end
else # faster refarray method but allocates a groups vector
groups = Vector{Int}(undef, nrow(df))
ngroups = row_group_slots!(cols, refpools, refarrays,
Val(false), groups, false, false, false)[1]
seen = fill(false, ngroups)
for i in 1:nrow(df)
g = groups[i]
if !seen[g]
seen[g] = true
res[i] = false
end
end
end
else
# always allocate a group vector, use refarray automatically if possible
groups = Vector{Int}(undef, nrow(df))
ngroups = row_group_slots!(cols, Val(false), groups, false, nothing, false)[1]
if keep == :last
seen = fill(false, ngroups)
for i in nrow(df):-1:1
g = groups[i]
if !seen[g]
seen[g] = true
res[i] = false
end
end
else
@assert keep == :noduplicates
# -1 indicates that we have not seen the group yet
# positive value indicates the first position we have seen the group
# 0 indicates that we have seen the group at least twice
firstseen = fill(-1, ngroups)
for i in 1:nrow(df)
g = groups[i]
j = firstseen[g]
if j == -1
# this is possibly a non duplicate row
firstseen[g] = i
res[i] = false
elseif j > 0
# the row had a duplicate
res[j] = true
firstseen[g] = 0
end
end
end
end
return res
end
function nonunique(df::AbstractDataFrame, cols; keep::Symbol=:first)
udf = _try_select_no_copy(df, cols)
if ncol(df) > 0 && ncol(udf) == 0
throw(ArgumentError("finding duplicate rows in data frame when " *
"`cols` selects no columns is not allowed"))
end
return nonunique(udf, keep=keep)
end
"""
allunique(df::AbstractDataFrame, cols=:)
Return `true` if none of the rows of `df` are duplicated. Two rows are
duplicates if all their columns contain equal values (according to `isequal`)
for all columns in `cols` (by default, all columns).
# Arguments
- `df` : `AbstractDataFrame`
- `cols` : a selector specifying the column(s) or their transformations to
compare. Can be any column selector or transformation accepted by
[`select`](@ref).
See also [`unique`](@ref) and [`nonunique`](@ref).
# Examples
```jldoctest
julia> df = DataFrame(i=1:4, x=[1, 2, 1, 2])
4×2 DataFrame
Row │ i x
│ Int64 Int64
─────┼──────────────
1 │ 1 1
2 │ 2 2
3 │ 3 1
4 │ 4 2
julia> allunique(df)
true
julia> allunique(df, :x)
false
julia> allunique(df, :i => ByRow(isodd))
false
```
"""
function Base.allunique(df::AbstractDataFrame, cols=:)
udf = _try_select_no_copy(df, cols)
nrow(udf) == 0 && return true
return row_group_slots!(ntuple(i -> udf[!, i], ncol(udf)),
Val(false), nothing, false, nothing, true)[1] == nrow(df)
end
# avoid invoking Base.allunique(f, iterator) introduced in Julia 1.11
Base.allunique(df::AbstractDataFrame, cols::Tuple) =
invoke(Base.allunique, Tuple{AbstractDataFrame, Any}, df, cols)
"""
unique(df::AbstractDataFrame; view::Bool=false, keep::Symbol=:first)
unique(df::AbstractDataFrame, cols; view::Bool=false, keep::Symbol=:first)
Return a data frame containing only unique rows in `df`.
Non-unique (duplicate) rows are those for which at least another row contains
equal values (according to `isequal`) for all columns in `cols` (by default,
all columns).
If `keep=:first` (the default), only the first occurrence of a set of duplicate
rows is kept.
If `keep=:last`, only the last occurrence of a set of duplicate rows is kept.
If `keep=:noduplicates`, only rows without any duplicates are kept.
If `view=false` a freshly allocated `DataFrame` is returned, and if `view=true`
then a `SubDataFrame` view into `df` is returned.
# Arguments
- `df` : the AbstractDataFrame
- `cols` : a selector specifying the column(s) or their transformations to
compare. Can be any column selector or transformation accepted by
[`select`](@ref) that returns at least one column if `df` has at least one
column.
$METADATA_FIXED
See also: [`unique!`](@ref), [`nonunique`](@ref).
# Examples
```jldoctest
julia> df = DataFrame(i=1:4, x=[1, 2, 1, 2])
4×2 DataFrame
Row │ i x
│ Int64 Int64
─────┼──────────────
1 │ 1 1
2 │ 2 2
3 │ 3 1
4 │ 4 2
julia> df = vcat(df, df)
8×2 DataFrame
Row │ i x
│ Int64 Int64
─────┼──────────────
1 │ 1 1
2 │ 2 2
3 │ 3 1
4 │ 4 2
5 │ 1 1
6 │ 2 2
7 │ 3 1
8 │ 4 2
julia> unique(df) # doesn't modify df
4×2 DataFrame
Row │ i x
│ Int64 Int64
─────┼──────────────
1 │ 1 1
2 │ 2 2
3 │ 3 1
4 │ 4 2
julia> unique(df, 2)
2×2 DataFrame
Row │ i x
│ Int64 Int64
─────┼──────────────
1 │ 1 1
2 │ 2 2
julia> unique(df, keep=:noduplicates)
0×2 DataFrame
Row │ i x
│ Int64 Int64
─────┴──────────────
```
"""
@inline function Base.unique(df::AbstractDataFrame; view::Bool=false,
keep::Symbol=:first)
rowidxs = (!).(nonunique(df, keep=keep))
return view ? Base.view(df, rowidxs, :) : df[rowidxs, :]
end
@inline function Base.unique(df::AbstractDataFrame, cols; view::Bool=false,
keep::Symbol=:first)
rowidxs = (!).(nonunique(df, cols, keep=keep))
return view ? Base.view(df, rowidxs, :) : df[rowidxs, :]
end
"""
unique!(df::AbstractDataFrame; keep::Symbol=:first)
unique!(df::AbstractDataFrame, cols; keep::Symbol=:first)
Update `df` in-place to contain only unique rows.
Non-unique (duplicate) rows are those for which at least another row contains
equal values (according to `isequal`) for all columns in `cols` (by default,
all columns).
If `keep=:first` (the default), only the first occurrence of a set of duplicate
rows is kept.
If `keep=:last`, only the last occurrence of a set of duplicate rows is kept.
If `keep=:noduplicates`, only rows without any duplicates are kept.
# Arguments
- `df` : the AbstractDataFrame
- `cols` : column indicator (`Symbol`, `Int`, `Vector{Symbol}`, `Regex`, etc.)
specifying the column(s) to compare. Can be any column selector or
transformation accepted by [`select`](@ref) that returns at least one column
if `df` has at least one column.
$METADATA_FIXED
See also: [`unique!`](@ref), [`nonunique`](@ref).
# Examples
```jldoctest
julia> df = DataFrame(i=1:4, x=[1, 2, 1, 2])
4×2 DataFrame
Row │ i x
│ Int64 Int64
─────┼──────────────
1 │ 1 1
2 │ 2 2
3 │ 3 1
4 │ 4 2
julia> df = vcat(df, df)
8×2 DataFrame
Row │ i x
│ Int64 Int64
─────┼──────────────
1 │ 1 1
2 │ 2 2
3 │ 3 1
4 │ 4 2
5 │ 1 1
6 │ 2 2
7 │ 3 1
8 │ 4 2
julia> unique!(copy(df)) # modifies df
4×2 DataFrame
Row │ i x
│ Int64 Int64
─────┼──────────────
1 │ 1 1
2 │ 2 2
3 │ 3 1
4 │ 4 2
julia> unique(df, keep=:noduplicates)
0×2 DataFrame
Row │ i x
│ Int64 Int64
─────┴──────────────
```
"""
Base.unique!(df::AbstractDataFrame; keep::Symbol=:first) =
deleteat!(df, _findall(nonunique(df, keep=keep)))
Base.unique!(df::AbstractDataFrame, cols::AbstractVector; keep::Symbol=:first) =
deleteat!(df, _findall(nonunique(df, cols, keep=keep)))
Base.unique!(df::AbstractDataFrame, cols; keep::Symbol=:first) =
deleteat!(df, _findall(nonunique(df, cols, keep=keep)))