[DataFrames Question]: Filter function parameter order

Question:

This is not a technical question. Consider it esthetechnical. While all functions from the DataFrames module get a DataFrame as first parameter, why don’t “Filter” and “Map” get this treatment too ?
Why
filter(function, df::AbstractDataFrame)
but not
filter(df::AbstractDataFrame, function)
?

Dataset (if applicable): any

That’s just the way filter works in general - DataFrame strives for consistency with Julia base where possible:

help?> filter
search: filter filter! fieldtype fieldtypes

  filter(f, a)

  Return a copy of collection a, removing elements for which f is false. The function f is passed one argument.
1 Like

Note that in the development version of DataFrames (to be released as 1.0), a new subset function is provided which is more consistent with other DataFrames functions.

6 Likes

The filter(df, f) method is in GitHub - xiaodaigh/DataConvenience.jl: Convenience functions missing in Julia

Very happy to see this.

1 Like

The @where macro from from DataFramesMeta has the “right” order, I find that to be relatively convenient.

2 Likes

Since no one’s mentioned it, just to expand that the reason Base functions that take in a function argument put that function argument first (see also map, reduce, broadcast, etc…) is so that you can use those functions with do blocks, like

filter(1:10) do x
    4 < x < 7
end

since the do block creates an anonymous function and then by convention passes it as the first argument to the function.

5 Likes