DataFramesMeta.jl
macros behave similar to DataFrames.jl
functions:
- they both take a
DataFrame
as a first positional argument - they have in-place mutating functions (as discussed in Section 4.10.1: the bang
!
functions)
Nevertheless, there are some differences between using DataFrames.jl
functions versus DataFramesMeta.jl
macros.
First, using parentheses in the macro commands are optional, and it can be replaced by spaces instead. For example:
@select df :col
We will be using the space syntax in this chapter.
Second, macros parse static commands by default. If you want dynamic parsing you’ll need to add $
to your syntax. This happens because macros treat the input as a static string. For example:
will always work because our intended selected column command with the argument :col
won’t change in runtime (the time that Julia is executing code). It will always mean the same operation no matter the context.
Now suppose that you want to use one of the column selectors presented in Section . Here, the expression inside the macro needs to be parsed dynamically. In other words, it is not static and the operation will change with context. For example:
This tells DataFramesMeta.jl
to treat the Not(:col)
part of the macro as dynamic. It will parse this expression and replace it by all of the columns except :col
.
Third, most of DataFramesMeta.jl
macros have two different versions: a non-vectorized, and a vectorized form. The non-vectorized form is the default form and treats arguments as whole columns, i.e., they operate on arrays whereas the vectorized form has an r
prefix (as in rows) and vectorizes all operators and functions calls. This is the same behavior as adding the dot operator .
into the desired operation. Similar to the function from DataFrames.jl
that we saw in Section 4.3.2.
These 3 operations are equivalent:
# DataFrames.jl
transform(df, :col => ByRow(exp))
# DataFramesMeta.jl vectorized with r prefix
NOTE: For most of the
DataFramesMeta.jl
macros we have four variants:
@macro
: non-vectorized@rmacro
: vectorized@macro!
: non-vectorized in-place