Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a function for skippmissing and then "spread"ing the result #121

Open
pdeffebach opened this issue Oct 9, 2020 · 2 comments
Open

Add a function for skippmissing and then "spread"ing the result #121

pdeffebach opened this issue Oct 9, 2020 · 2 comments

Comments

@pdeffebach
Copy link
Contributor

This is something I have discussed elsewhere as being useful, and came up today on slack.

Problem

function standardize(x)
    mu = mean(x)
    sigma = std(x)
    return (x .- mu) ./ sigma
end
standardize([1,2,3, missing]

We can't use passmissing because this isn't an element-wise operation. We need an operation that applies skipmissing to the input, then applies the function, then "spreads" the result of the function to a vector of the same length as the output.

function skipmissing_then_collect(fun, args...)
    smargs = skipmissings(args...)
    res = fun(smargs...)
    out = Union{eltype(res), Missing}[missing for i in 1:length(first(args))]# assume all args vectors also
    res_counter = 1
    for i in eachindex(first(smargs))
        out[i] = res[res_counter] # can probably do fancy iteration stuff here
        res_counter += 1
    end
    out
end

This might solve a lot of problems in DataFrames as well.

@bkamins
Copy link
Member

bkamins commented Oct 10, 2020

Great - let us discuss it here. This would be much better to have a generic solution (if we can come up with a good recipe) than having a custom patch in DataFrames.jl.

@nalimilan
Copy link
Member

As noted when discussing this in DataFrames (JuliaData/DataFrames.jl#2258 (comment)), it could be better (when inputs are vectors) to first find the indices of complete observations, pass a SubArray view of the complete observations to the user-provided function, and use these indices to fill the returned vector. That would avoid going over the data twice to identify missing values, and that would be simpler for users since the function would be passed AbstractVectors rather than SkipMissings iterators.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants