-
Notifications
You must be signed in to change notification settings - Fork 32
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
Request - flags to allow additional arguments to be ignored and values supplied as "missing" to be overridden. #153
Comments
Thanks for the input! First off, DataFrames are a mystery to me, so you'll have to give examples without them. In principle, I can see that julia> @with_kw struct Mystruct
a
b
end
julia> Mystruct(Parameters.ignore_additional_kwargs, a=1, b=2, c=3) ? A bit uglier but maybe clearer? Not sure about the other one. Seems too specific? |
That's a nice syntax. Adding an ignore_missing parameter to your example:
gives I encourage you to look at DataFrames with Chain and DataFramesMeta. Maybe there's a better way to work ? but my usual process is:
While structs and functions are the best for calculations, Database tables are a standard for storage. The ignore_missing parameter is for when a DataBase column is missing or NULL for some rows and you want to supply a default (e.g. b=2a) in the struct. For me this is common. Maybe its just me:) |
lol, I don't know SQL or Pandas either ;-) But, yes, I probably should learn them... |
SQL yes cos its easy and so much data is stored in SQL databases. Don't bother with Pandas unless you have to. IMO Julia's DataFrames ecosystem is the best in any language. |
The first one can just be done with a utility function: using Parameters
@with_kw struct AA
a = 0
b = 0
c = 0
end
function construct_it(T, tup::D) where D
tf = fieldnames(D)
fn = fieldnames(T)
# remove all fields from tuple tup which are not in T
out = (;)
for f in fn
if f in tf && tup[f]!==missing
out = (out..., f=>tup[f])
end
end
T(;out...)
end
construct_it(AA, (a=3, b=7, u=8)) (ok, the function needs a better name). The I think that be the best approach. Edit: added missing-check/feature |
I like that :) |
will you put this in? |
The objective of this request is to make it very easy to use AsTable(:) to convert a DataFrame to an array of structures.
( AsTable(:) passes a DataFrame row as a named tuple. )
Example
allow_additional_args = true
, means column Z is ignored (rather than causing an error).overwrite_missing
means when column Y =missing
, the default of2X
is used, as it would be if field Y were not supplied e..gmystruct(X=1)
More generally
When the struct has 10 or 20 fields and the DataFrame has 50. (or when the struct is created from a larger struct with a super-set of fields), its nice to not have to re-state the fieldnames.
The text was updated successfully, but these errors were encountered: