@@ -2,25 +2,21 @@ import DataAPI: All, Between, BroadcastedSelector, Cols
2
2
import DataFrames: AsTable, ByRow, ColumnIndex, MultiColumnIndex, normalize_selection
3
3
import InvertedIndices: BroadcastedInvertedIndex
4
4
5
-
6
-
7
- make_pair_concrete (@nospecialize (x:: Pair )) =
8
- make_pair_concrete (x. first) => make_pair_concrete (x. second)
5
+ function make_pair_concrete (@nospecialize (x:: Pair ))
6
+ return make_pair_concrete (x. first) => make_pair_concrete (x. second)
7
+ end
9
8
make_pair_concrete (@nospecialize (x)) = x
10
9
11
10
broadcast_pair (df:: DTable , @nospecialize (p:: Any )) = p
12
11
13
12
# Copied as is from DataFrames.jl
14
13
function broadcast_pair (df:: DTable , @nospecialize (p:: Pair ))
15
14
src, second = p
16
- src_broadcast = src isa Union{BroadcastedInvertedIndex,
17
- BroadcastedSelector}
18
- second_broadcast = second isa Union{BroadcastedInvertedIndex,
19
- BroadcastedSelector}
15
+ src_broadcast = src isa Union{BroadcastedInvertedIndex,BroadcastedSelector}
16
+ second_broadcast = second isa Union{BroadcastedInvertedIndex,BroadcastedSelector}
20
17
if second isa Pair
21
18
fun, dst = second
22
- dst_broadcast = dst isa Union{BroadcastedInvertedIndex,
23
- BroadcastedSelector}
19
+ dst_broadcast = dst isa Union{BroadcastedInvertedIndex,BroadcastedSelector}
24
20
if src_broadcast || dst_broadcast
25
21
new_src = src_broadcast ? names (df, src. sel) : src
26
22
new_dst = dst_broadcast ? names (df, dst. sel) : dst
45
41
# as then broadcasting produces Matrix{Any} rather than Matrix{<:Pair}
46
42
broadcast_pair (df:: DTable , @nospecialize (p:: AbstractMatrix )) = isempty (p) ? [] : p
47
43
48
-
49
44
# Copied as is from DataFrames.jl
50
45
function broadcast_pair (df:: DTable , @nospecialize (p:: AbstractVecOrMat{<:Pair} ))
51
46
isempty (p) && return []
52
47
need_broadcast = false
53
48
54
49
src = first .(p)
55
50
first_src = first (src)
56
- if first_src isa Union{BroadcastedInvertedIndex,
57
- BroadcastedSelector}
51
+ if first_src isa Union{BroadcastedInvertedIndex,BroadcastedSelector}
58
52
if any (!= (first_src), src)
59
- throw (ArgumentError (" when broadcasting column selector it must " *
60
- " have a constant value" ))
53
+ throw (
54
+ ArgumentError (
55
+ " when broadcasting column selector it must " * " have a constant value"
56
+ ),
57
+ )
61
58
end
62
59
need_broadcast = true
63
60
new_names = names (df, first_src. sel)
64
61
if ! (length (new_names) == size (p, 1 ) || size (p, 1 ) == 1 )
65
- throw (ArgumentError (" broadcasted dimension does not match the " *
66
- " number of selected columns" ))
62
+ throw (
63
+ ArgumentError (
64
+ " broadcasted dimension does not match the " * " number of selected columns"
65
+ ),
66
+ )
67
67
end
68
68
new_src = new_names
69
69
else
@@ -72,33 +72,45 @@ function broadcast_pair(df::DTable, @nospecialize(p::AbstractVecOrMat{<:Pair}))
72
72
73
73
second = last .(p)
74
74
first_second = first (second)
75
- if first_second isa Union{BroadcastedInvertedIndex,
76
- BroadcastedSelector}
75
+ if first_second isa Union{BroadcastedInvertedIndex,BroadcastedSelector}
77
76
if any (!= (first_second), second)
78
- throw (ArgumentError (" when using broadcasted column selector it " *
79
- " must have a constant value" ))
77
+ throw (
78
+ ArgumentError (
79
+ " when using broadcasted column selector it " * " must have a constant value"
80
+ ),
81
+ )
80
82
end
81
83
need_broadcast = true
82
84
new_names = names (df, first_second. sel)
83
85
if ! (length (new_names) == size (p, 1 ) || size (p, 1 ) == 1 )
84
- throw (ArgumentError (" broadcasted dimension does not match the " *
85
- " number of selected columns" ))
86
+ throw (
87
+ ArgumentError (
88
+ " broadcasted dimension does not match the " * " number of selected columns"
89
+ ),
90
+ )
86
91
end
87
92
new_second = new_names
88
93
else
89
94
if first_second isa Pair
90
95
fun, dst = first_second
91
- if dst isa Union{BroadcastedInvertedIndex,
92
- BroadcastedSelector}
96
+ if dst isa Union{BroadcastedInvertedIndex,BroadcastedSelector}
93
97
if ! all (x -> x isa Pair && last (x) == dst, second)
94
- throw (ArgumentError (" when using broadcasted column selector " *
95
- " it must have a constant value" ))
98
+ throw (
99
+ ArgumentError (
100
+ " when using broadcasted column selector " *
101
+ " it must have a constant value" ,
102
+ ),
103
+ )
96
104
end
97
105
need_broadcast = true
98
106
new_names = names (df, dst. sel)
99
107
if ! (length (new_names) == size (p, 1 ) || size (p, 1 ) == 1 )
100
- throw (ArgumentError (" broadcasted dimension does not match the " *
101
- " number of selected columns" ))
108
+ throw (
109
+ ArgumentError (
110
+ " broadcasted dimension does not match the " *
111
+ " number of selected columns" ,
112
+ ),
113
+ )
102
114
end
103
115
new_dst = new_names
104
116
new_second = first .(second) .=> new_dst
@@ -119,7 +131,9 @@ function broadcast_pair(df::DTable, @nospecialize(p::AbstractVecOrMat{<:Pair}))
119
131
end
120
132
121
133
# Copied as is from DataFrames.jl
122
- function manipulate (df:: DTable , @nospecialize (cs... ); copycols:: Bool , keeprows:: Bool , renamecols:: Bool )
134
+ function manipulate (
135
+ df:: DTable , @nospecialize (cs... ); copycols:: Bool , keeprows:: Bool , renamecols:: Bool
136
+ )
123
137
cs_vec = []
124
138
for v in cs
125
139
if v isa AbstractVecOrMat{<: Pair }
@@ -128,8 +142,12 @@ function manipulate(df::DTable, @nospecialize(cs...); copycols::Bool, keeprows::
128
142
push! (cs_vec, v)
129
143
end
130
144
end
131
- return _manipulate (df, Any[normalize_selection (index (df), make_pair_concrete (c), renamecols) for c in cs_vec],
132
- copycols, keeprows)
145
+ return _manipulate (
146
+ df,
147
+ Any[normalize_selection (index (df), make_pair_concrete (c), renamecols) for c in cs_vec],
148
+ copycols,
149
+ keeprows,
150
+ )
133
151
end
134
152
135
153
# Not copied - full custom implementation
@@ -156,13 +174,12 @@ function _manipulate(df::DTable, normalized_cs::Vector{Any}, copycols::Bool, kee
156
174
# ########
157
175
158
176
colresults = Dict {Int,Any} (
159
- k => fetch (Dagger. spawn (length, v)) == 1 ? fetch (v) : v
160
- for (k, v) in colresults
177
+ k => fetch (Dagger. spawn (length, v)) == 1 ? fetch (v) : v for (k, v) in colresults
161
178
)
162
179
163
180
mapmask = [
164
- haskey (colresults, x) && colresults[x] isa Dagger. EagerThunk
165
- for (x, _) in enumerate (normalized_cs)
181
+ haskey (colresults, x) && colresults[x] isa Dagger. EagerThunk for
182
+ (x, _) in enumerate (normalized_cs)
166
183
]
167
184
168
185
mappable_part_of_normalized_cs = filter (x -> ! mapmask[x[1 ]], collect (enumerate (normalized_cs)))
@@ -204,28 +221,33 @@ function _manipulate(df::DTable, normalized_cs::Vector{Any}, copycols::Bool, kee
204
221
end
205
222
206
223
# Not copied - full custom implementation
207
- function manipulate (dt:: DTable , args:: AbstractVector{Int} ; copycols:: Bool , keeprows:: Bool , renamecols:: Bool )
224
+ function manipulate (
225
+ dt:: DTable , args:: AbstractVector{Int} ; copycols:: Bool , keeprows:: Bool , renamecols:: Bool
226
+ )
208
227
colidx = first (args)
209
228
colname = Tables. columnnames (Tables. columns (dt))[colidx]
210
- map (r -> (; colname => Tables. getcolumn (r, colidx)), dt)
229
+ return map (r -> (; colname => Tables. getcolumn (r, colidx)), dt)
211
230
end
212
231
213
232
# Copied as is from DataFrames.jl
214
- function manipulate (df:: DTable , c:: MultiColumnIndex ; copycols:: Bool , keeprows:: Bool ,
215
- renamecols:: Bool )
233
+ function manipulate (
234
+ df:: DTable , c:: MultiColumnIndex ; copycols:: Bool , keeprows:: Bool , renamecols:: Bool
235
+ )
216
236
if c isa AbstractVector{<: Pair }
217
- return manipulate (df, c... , copycols= copycols, keeprows= keeprows,
218
- renamecols= renamecols)
237
+ return manipulate (df, c... ; copycols= copycols, keeprows= keeprows, renamecols= renamecols)
219
238
else
220
- return manipulate (df, index (df)[c], copycols= copycols, keeprows= keeprows,
221
- renamecols= renamecols)
239
+ return manipulate (
240
+ df, index (df)[c]; copycols= copycols, keeprows= keeprows, renamecols= renamecols
241
+ )
222
242
end
223
243
end
224
244
225
245
# Copied as is from DataFrames.jl
226
- manipulate (df:: DTable , c:: ColumnIndex ; copycols:: Bool , keeprows:: Bool , renamecols:: Bool ) =
227
- manipulate (df, Int[index (df)[c]], copycols= copycols, keeprows= keeprows, renamecols= renamecols)
228
-
246
+ function manipulate (df:: DTable , c:: ColumnIndex ; copycols:: Bool , keeprows:: Bool , renamecols:: Bool )
247
+ return manipulate (
248
+ df, Int[index (df)[c]]; copycols= copycols, keeprows= keeprows, renamecols= renamecols
249
+ )
250
+ end
229
251
230
252
"""
231
253
select(df::DTable, args...; copycols::Bool=true, renamecols::Bool=true)
@@ -241,6 +263,12 @@ please file an issue with reproduction steps and data.
241
263
242
264
Please refer to DataFrames documentation for more details on usage.
243
265
"""
244
- select (df:: DTable , @nospecialize (args... ); copycols:: Bool = true , renamecols:: Bool = true ) =
245
- manipulate (df, map (x -> broadcast_pair (df, x), args)... ,
246
- copycols= copycols, keeprows= true , renamecols= renamecols)
266
+ function select (df:: DTable , @nospecialize (args... ); copycols:: Bool = true , renamecols:: Bool = true )
267
+ return manipulate (
268
+ df,
269
+ map (x -> broadcast_pair (df, x), args)... ;
270
+ copycols= copycols,
271
+ keeprows= true ,
272
+ renamecols= renamecols,
273
+ )
274
+ end
0 commit comments