Reputation: 1550
I'm lazy, and got tired of selecting rows in dataframes with code like this (on the 0.4 RC):
using DataFrames
data = DataFrame(num=[1,2,3,4], let=["A", "B", "A", "B"])
subset = data[(data[:num].>1)&(data[:let].=="B"),:]
so I wrote the following macro
# Need to import SubstitutionString from Base
macro sel(dt, conditions)
dtstr=string(dt)
str = string(conditions)
out_str = replace(str, r":\w+",SubstitutionString(string(dtstr, "[", "\\g<0>", "]")))
out_str = string(dtstr, "[", out_str, ",:]")
eval(parse(out_str))
end
so that I could instead write
subset = @sel data (:num.>1)&(:let.=="B")
This works fine, except in the situation where I want to match columns to strings like "Turtles::Leonardo" or "12:25:00". Is there any way to avoid falsely identifying the contents of strings as column names here, or is this a limitation of this sloppy convert-to-string-and-work-on-that metaprogramming?
Upvotes: 2
Views: 68
Reputation: 1275
You could also take a look at DataFramesMeta
, where you find a similar macro, which should work in the problematic cases you mention. The macro works this way:
subset = @where(data, (:num.>1)&(:let.=="B"))
Upvotes: 1
Reputation: 5756
is it helps?
out_str = replace(str, r"(\()(:\w)",SubstitutionString(string(dtstr, "[", "\\g<2>", "]")))
Upvotes: 0