forked from JuliaData/DataFrames.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.jl
85 lines (73 loc) · 2.06 KB
/
utils.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
"""
AsTable(cols)
A type used for selection operations to signal that the columns selected by the
wrapped selector should be passed as a `NamedTuple` to the function.
"""
struct AsTable
cols
end
Base.broadcastable(x::AsTable) = Ref(x)
function make_unique!(names::Vector{Symbol}, src::AbstractVector{Symbol};
makeunique::Bool=false)
if length(names) != length(src)
throw(DimensionMismatch("Length of src doesn't match length of names."))
end
seen = Set{Symbol}()
dups = Int[]
for i in 1:length(names)
name = src[i]
if in(name, seen)
push!(dups, i)
else
names[i] = src[i]
push!(seen, name)
end
end
if length(dups) > 0
if !makeunique
dupstr = join(string.(':', unique(src[dups])), ", ", " and ")
msg = "Duplicate variable names: $dupstr. Pass makeunique=true " *
"to make them unique using a suffix automatically."
throw(ArgumentError(msg))
end
end
for i in dups
nm = src[i]
k = 1
while true
newnm = Symbol("$(nm)_$k")
if !in(newnm, seen)
names[i] = newnm
push!(seen, newnm)
break
end
k += 1
end
end
return names
end
function make_unique(names::AbstractVector{Symbol}; makeunique::Bool=false)
make_unique!(similar(names), names, makeunique=makeunique)
end
"""
gennames(n::Integer)
Generate standardized names for columns of a DataFrame.
The first name will be `:x1`, the second `:x2`, etc.
"""
function gennames(n::Integer)
res = Vector{Symbol}(undef, n)
for i in 1:n
res[i] = Symbol(@sprintf "x%d" i)
end
return res
end
function funname(f)
n = nameof(f)
String(n)[1] == '#' ? :function : n
end
if isdefined(Base, :ComposedFunction) # Julia >= 1.6.0-DEV.85
using Base: ComposedFunction
else
using Compat: ComposedFunction
end
funname(c::ComposedFunction) = Symbol(funname(c.outer), :_, funname(c.inner))