-
-
Notifications
You must be signed in to change notification settings - Fork 398
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 DataFrames.jl extension #3821
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f75d808
Add DataFrames.jl extension
odow 08ab629
Update multi tutorial
odow 5065efc
Update docs/src/tutorials/linear/multi.jl
odow c980a0e
Fix formattig
odow 601c697
Update docs/src/tutorials/linear/multi.jl
odow 1a70972
Update
odow 890ede8
Update docs/src/extensions/DataFrames.md
odow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
# DataFrames.jl | ||
|
||
[DataFrames.jl](https://github.com/JuliaData/DataFrames.jl) provides tools for | ||
working with in-memory tabular data in Julia. | ||
|
||
!!! compat | ||
Using the DataFrames extension with JuMP requires Julia v1.9 or later. | ||
|
||
The DataFrames extension in JuMP lets you construct a `DataFrames.DataFrame` as | ||
a container in the JuMP macros. | ||
|
||
## License | ||
|
||
DataFrames.jl is licensed under the [MIT license](https://github.com/JuliaData/DataFrames.jl/blob/main/LICENSE.md). | ||
|
||
## Installation | ||
|
||
Install DataFrames using `Pkg.add`: | ||
|
||
```julia | ||
import Pkg | ||
Pkg.add("DataFrames") | ||
``` | ||
|
||
## Use with JuMP | ||
|
||
Activate the extension by loading both JuMP and DataFrames: | ||
|
||
```jldoctest ext_data_frames | ||
julia> using JuMP, DataFrames | ||
``` | ||
|
||
Then, pass `container = DataFrames.DataFrame` in the [`@variable`](@ref), | ||
[`@constraint`](@ref), or [`@expression`](@ref) macros: | ||
|
||
```jldoctest ext_data_frames | ||
julia> model = Model(); | ||
|
||
julia> @variable( | ||
model, | ||
x[i = 2:4, j = ["a", "b"]] >= i, | ||
container = DataFrames.DataFrame, | ||
) | ||
6×3 DataFrame | ||
Row │ i j value | ||
│ Int64 String GenericV… | ||
─────┼────────────────────────── | ||
1 │ 2 a x[2,a] | ||
2 │ 3 a x[3,a] | ||
3 │ 4 a x[4,a] | ||
4 │ 2 b x[2,b] | ||
5 │ 3 b x[3,b] | ||
6 │ 4 b x[4,b] | ||
``` | ||
|
||
Here `x` is a `DataFrames.DataFrame` array object, so operations use the | ||
DataFrames syntax: | ||
|
||
```jldoctest ext_data_frames | ||
julia> x[x.j .== "a", [:i, :value]] | ||
3×2 DataFrame | ||
Row │ i value | ||
│ Int64 GenericV… | ||
─────┼────────────────── | ||
1 │ 2 x[2,a] | ||
2 │ 3 x[3,a] | ||
3 │ 4 x[4,a] | ||
|
||
julia> DataFrames.unstack(x, :i, :j, :value) | ||
3×3 DataFrame | ||
Row │ i a b | ||
│ Int64 GenericV…? GenericV…? | ||
─────┼─────────────────────────────── | ||
1 │ 2 x[2,a] x[2,b] | ||
2 │ 3 x[3,a] x[3,b] | ||
3 │ 4 x[4,a] x[4,b] | ||
``` | ||
|
||
You can use `container = DataFrames.DataFrame` in the [`@expression`](@ref) | ||
macro: | ||
|
||
```jldoctest ext_data_frames | ||
julia> @expression( | ||
model, | ||
expr[j = ["a", "b"]], | ||
sum(x[x.j .== j, :value]), | ||
container = DataFrames.DataFrame, | ||
) | ||
2×2 DataFrame | ||
Row │ j value | ||
│ String AffExpr | ||
─────┼────────────────────────────────── | ||
1 │ a x[2,a] + x[3,a] + x[4,a] | ||
2 │ b x[2,b] + x[3,b] + x[4,b] | ||
``` | ||
|
||
and in [`@constraint`](@ref): | ||
|
||
```jldoctest ext_data_frames | ||
julia> @constraint( | ||
model, | ||
[j = ["a", "b"]], | ||
sum(x[x.j .== j, :value]) <= 1, | ||
container = DataFrames.DataFrame, | ||
) | ||
2×2 DataFrame | ||
Row │ j value | ||
│ String Constrai… | ||
─────┼────────────────────────────────────── | ||
1 │ a x[2,a] + x[3,a] + x[4,a] ≤ 1 | ||
2 │ b x[2,b] + x[3,b] + x[4,b] ≤ 1 | ||
``` | ||
|
||
### DataFrame-native syntax | ||
|
||
While you can use indexing in JuMP's `@expression` and `@constraint` macros, it | ||
may be more convenient to use DataFrames.jl split-apply-combine framework. For | ||
example, `expr` can be equivalently written as: | ||
|
||
```jldoctest ext_data_frames | ||
julia> expr2 = model[:expr2] = DataFrames.combine( | ||
DataFrames.groupby(x, :j), | ||
:value => sum => :value, | ||
) | ||
2×2 DataFrame | ||
Row │ j value | ||
│ String AffExpr | ||
─────┼────────────────────────────────── | ||
1 │ a x[2,a] + x[3,a] + x[4,a] | ||
2 │ b x[2,b] + x[3,b] + x[4,b] | ||
``` | ||
|
||
and the constraint could be written as | ||
|
||
```jldoctest ext_data_frames | ||
julia> df_constraint(v) = @constraint(model, sum(v) <= 1); | ||
|
||
julia> DataFrames.combine( | ||
DataFrames.groupby(x, :j), | ||
:value => df_constraint => :value, | ||
) | ||
2×2 DataFrame | ||
Row │ j value | ||
│ String Constrai… | ||
─────┼────────────────────────────────────── | ||
1 │ a x[2,a] + x[3,a] + x[4,a] ≤ 1 | ||
2 │ b x[2,b] + x[3,b] + x[4,b] ≤ 1 | ||
``` | ||
|
||
## Documentation | ||
|
||
See the [DataFrames.jl documentation](https://dataframes.juliadata.org/stable/) | ||
for more details on the syntax and features of `DataFrames.DataFrame`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
module JuMPDataFramesExt | ||
|
||
import DataFrames | ||
import JuMP | ||
|
||
function JuMP.Containers.container( | ||
f::Function, | ||
indices, | ||
::Type{DataFrames.DataFrame}, | ||
names::AbstractVector, | ||
) | ||
rows = vec(collect(indices)) | ||
df = DataFrames.DataFrame(NamedTuple{tuple(names...)}(arg) for arg in rows) | ||
df.value = [f(arg...) for arg in rows] | ||
return df | ||
end | ||
|
||
end #module |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This call the in-place operations?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand the question. It's not in-place. It creates a new DataFrame