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 default style to metadata! and colmetadata! #3216

Merged
merged 6 commits into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
* Improve printing of grouping keys when displaying `GroupedDataFrame`
([#3213](https://github.com/JuliaData/DataFrames.jl/pull/3213))

## Integration changes

* Support updates of metadata API introduced in DataAPI.jl 1.13.0
([3216](https://github.com/JuliaData/DataFrames.jl/pull/3216))

# DataFrames.jl v1.4.2 Patch Release Notes

## Bug fixes
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
[compat]
CategoricalArrays = "0.10.0"
Compat = "4.2"
DataAPI = "1.12.0"
DataAPI = "1.13.0"
InlineStrings = "1.3.0"
InvertedIndices = "1"
IteratorInterfaceExtensions = "0.1.1, 1"
Expand Down
36 changes: 20 additions & 16 deletions src/other/metadata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@ function metadatakeys(x::Union{DataFrameRow, SubDataFrame})
end

"""
metadata!(df::AbstractDataFrame, key::AbstractString, value; style)
metadata!(dfr::DataFrameRow, key::AbstractString, value; style)
metadata!(dfc::DataFrameColumns, key::AbstractString, value; style)
metadata!(dfr::DataFrameRows, key::AbstractString, value; style)
metadata!(df::AbstractDataFrame, key::AbstractString, value; style::Symbol=:default)
metadata!(dfr::DataFrameRow, key::AbstractString, value; style::Symbol=:default)
metadata!(dfc::DataFrameColumns, key::AbstractString, value; style::Symbol=:default)
metadata!(dfr::DataFrameRows, key::AbstractString, value; style::Symbol=:default)

Set table-level metadata for object `df` for key `key` to have value `value`
and style `style` and return `df`.
and style `style` (`:default` by default) and return `df`.

For `SubDataFrame` and `DataFrameRow` only `:note`-style is allowed.
Trying to set a key-value pair for which the key already exists in the parent
Expand All @@ -188,7 +188,8 @@ See also: [`metadata`](@ref), [`metadatakeys`](@ref),
$TABLEMETA_EXAMPLE
```
"""
function metadata!(df::DataFrame, key::AbstractString, value::Any; style)
function metadata!(df::DataFrame, key::AbstractString, value::Any;
style::Symbol=:default)
premeta = getfield(df, :metadata)
if premeta === nothing
meta = Dict{String, Tuple{Any, Any}}()
Expand All @@ -204,13 +205,13 @@ function metadata!(df::DataFrame, key::AbstractString, value::Any; style)
end

function metadata!(x::Union{DataFrameRows, DataFrameColumns},
key::AbstractString, value::Any; style)
key::AbstractString, value::Any; style::Symbol=:default)
metadata!(parent(x), key, value, style=style)
return x
end

function metadata!(x::Union{DataFrameRow, SubDataFrame},
key::AbstractString, value::Any; style)
key::AbstractString, value::Any; style::Symbol=:default)
if style !== :note
throw(ArgumentError("only :note-style metadata is supported for " *
"DataFrameRow and SubDataFrame"))
Expand Down Expand Up @@ -464,13 +465,13 @@ function colmetadatakeys(x::Union{DataFrameRow, SubDataFrame})
end

"""
colmetadata!(df::AbstractDataFrame, col::ColumnIndex, key::AbstractString, value; style)
colmetadata!(dfr::DataFrameRow, col::ColumnIndex, key::AbstractString, value; style)
colmetadata!(dfc::DataFrameColumns, col::ColumnIndex, key::AbstractString, value; style)
colmetadata!(dfr::DataFrameRows, col::ColumnIndex, key::AbstractString, value; style)
colmetadata!(df::AbstractDataFrame, col::ColumnIndex, key::AbstractString, value; style::Symbol=:default)
colmetadata!(dfr::DataFrameRow, col::ColumnIndex, key::AbstractString, value; style::Symbol=:default)
colmetadata!(dfc::DataFrameColumns, col::ColumnIndex, key::AbstractString, value; style::Symbol=:default)
colmetadata!(dfr::DataFrameRows, col::ColumnIndex, key::AbstractString, value; style::Symbol=:default)

Set column-level metadata in `df` for column `col` and key `key` to have value `value`
and style `style` and return `df`.
and style `style` (`:default` by default) and return `df`.

For `SubDataFrame` and `DataFrameRow` only `:note` style is allowed.
Trying to set a key-value pair for which the key already exists in the parent
Expand All @@ -484,7 +485,8 @@ See also: [`metadata`](@ref), [`metadatakeys`](@ref),
$COLMETADATA_EXAMPLE
```
"""
function colmetadata!(df::DataFrame, col::ColumnIndex, key::AbstractString, value::Any; style)
function colmetadata!(df::DataFrame, col::ColumnIndex, key::AbstractString, value::Any;
style::Symbol=:default)
idx = index(df)[col] # check if column exists and get its integer index
pre_cols_meta = getfield(df, :colmetadata)
if pre_cols_meta === nothing
Expand All @@ -502,13 +504,15 @@ function colmetadata!(df::DataFrame, col::ColumnIndex, key::AbstractString, valu
end

function colmetadata!(x::Union{DataFrameRows, DataFrameColumns},
col::ColumnIndex, key::AbstractString, value::Any; style)
col::ColumnIndex, key::AbstractString, value::Any;
style::Symbol=:default)
colmetadata!(parent(x), col, key, value; style=style)
return x
end

function colmetadata!(x::Union{DataFrameRow, SubDataFrame},
col::ColumnIndex, key::AbstractString, value::Any; style)
col::ColumnIndex, key::AbstractString, value::Any;
style::Symbol=:default)
col_name = _names(x)[index(x)[col]]
if style !== :note
throw(ArgumentError("only :note-style metadata is supported for " *
Expand Down
53 changes: 53 additions & 0 deletions test/metadata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,59 @@ end
@test check_allnotemetadata(df)
end

@testset "metadata default" begin
df = DataFrame(a=1)
metadata!(df, "x", "y")
colmetadata!(df, :a, "p", "q")
@test metadata(df, "x", style=true) == ("y", :default)
@test colmetadata(df, :a, "p", style=true) == ("q", :default)
df = eachcol(DataFrame(a=1))
metadata!(df, "x", "y")
colmetadata!(df, :a, "p", "q")
@test metadata(df, "x", style=true) == ("y", :default)
@test colmetadata(df, :a, "p", style=true) == ("q", :default)
df = view(DataFrame(a=1), :, :)
@test_throws ArgumentError metadata!(df, "x", "y")
@test_throws ArgumentError colmetadata!(df, :a, "p", "q")
end

@testset "fallback definitions of metadata and colmetadata" begin
nalimilan marked this conversation as resolved.
Show resolved Hide resolved
df = DataFrame()
@test metadata(df) == Dict()
@test metadata(df, style=true) == Dict()
@test colmetadata(df) == Dict()
@test colmetadata(df, style=true) == Dict()
df.a = 1:2
df.b = 2:3
df.c = 3:4
@test metadata(df) == Dict()
@test metadata(df, style=true) == Dict()
@test colmetadata(df) == Dict()
@test colmetadata(df, style=true) == Dict()
metadata!(df, "a1", "b1", style=:default)
metadata!(df, "a2", "b2", style=:note)
colmetadata!(df, :a, "x1", "y1", style=:default)
colmetadata!(df, :a, "x2", "y2", style=:note)
colmetadata!(df, :c, "x3", "y3", style=:note)
@test metadata(df) == Dict("a1" => "b1", "a2" => "b2")
@test metadata(df, style=true) == Dict("a1" => ("b1", :default),
"a2" => ("b2", :note))
@test colmetadata(df) == Dict(:a => Dict("x1" => "y1",
"x2" => "y2"),
:c => Dict("x3" => "y3"))
@test colmetadata(df, style=true) == Dict(:a => Dict("x1" => ("y1", :default),
"x2" => ("y2", :note)),
:c => Dict("x3" => ("y3", :note)))
@test colmetadata(df, :a) == Dict("x1" => "y1",
"x2" => "y2")
@test colmetadata(df, :a, style=true) == Dict("x1" => ("y1", :default),
"x2" => ("y2", :note))
@test colmetadata(df, :b) == Dict()
@test colmetadata(df, :b, style=true) == Dict()
@test colmetadata(df, :c) == Dict("x3" => "y3")
@test colmetadata(df, :c, style=true) == Dict("x3" => ("y3", :note))
end

@testset "rename & rename!" begin
df = DataFrame()
df2 = rename(df)
Expand Down