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 get function for AbstractDataFrame #3281

Closed
dgegen opened this issue Feb 3, 2023 · 5 comments · Fixed by #3282
Closed

Add get function for AbstractDataFrame #3281

dgegen opened this issue Feb 3, 2023 · 5 comments · Fixed by #3282
Labels
Milestone

Comments

@dgegen
Copy link

dgegen commented Feb 3, 2023

Analogous to the syntax of the df.get function in Pandas, I propose to add the following function in src/abstractdataframe/iteration.jl, which I syntactically consider a conventional and convenient way to deal with cases where the return of default values is desired.

"""
    get(df::AbstractDataFrame, col_ind, default=nothing)
Return the column with the index `col_ind`, or the given default value `default`
if the column of the given index is not present in the DataFrame `df`.
"""
function Base.get(df::AbstractDataFrame, col_ind::Union{Symbol,AbstractString},
                  default=nothing) 
    hasproperty(df, col_ind) ? df[:, col_ind] : default
end

Does anyone have additional suggestions, thoughts or opposing arguments?
Cheers!

@bkamins
Copy link
Member

bkamins commented Feb 3, 2023

we could add:

get(df::DataFrameColumns, col_ind::ColumnIndex, default)

and

haskey(df::DataFrameColumns, col_ind::ColumnIndex)

so @dgegen could get the expected behavior, but with eachcol(df).

@nalimilan - what do you think about it?

@bkamins bkamins added the feature label Feb 3, 2023
@bkamins bkamins added this to the 1.5 milestone Feb 3, 2023
@nalimilan
Copy link
Member

IIRC we had this a long time ago, but we dropped it because it goes against the idea that data frames are collections of rows. It would have to be called getcols if we wanted to add it.

@bkamins
Copy link
Member

bkamins commented Feb 3, 2023

@nalimilan - that is why I do not want to add it to DataFrame but to DataFrameColumns. DataFrameColumns is a collection of columns so it could support get and haskey.

Note that we now support both get and haskey for DataFrameRow for the same reason (as it is a collection of columns).

@nalimilan
Copy link
Member

Woops, sorry. Why not then. :-)

@bkamins
Copy link
Member

bkamins commented Feb 3, 2023

Added in #3282

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

Successfully merging a pull request may close this issue.

3 participants