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

WIP: Add DataFrames as a weak dependency #3441

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ SnoopPrecompile = "66db9d55-30c0-4569-8b51-7e840670fc0c"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[weakdeps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
DimensionalData = "0703355e-b756-11e9-17c0-8b28908087d0"

[extensions]
JuMPDataFramesExt = "DataFrames"
JuMPDimensionalDataExt = "DimensionalData"

[compat]
DataFrames = "1"
DimensionalData = "0.24"
MathOptInterface = "1.18"
MutableArithmetics = "1"
Expand All @@ -27,8 +30,9 @@ SnoopPrecompile = "1"
julia = "1.6"

[extras]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
DimensionalData = "0703355e-b756-11e9-17c0-8b28908087d0"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["DimensionalData", "Test"]
test = ["DataFrames", "DimensionalData", "Test"]
27 changes: 27 additions & 0 deletions ext/JuMPDataFramesExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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::F,
indices::Union{
JuMP.Containers.VectorizedProductIterator,
JuMP.Containers.NestedIterator,
},
::Type{DataFrames.DataFrame},
names::AbstractVector,
) where {F<:Function}
rows = vec(collect(indices))
data = [name => getindex.(rows, i) for (i, name) in enumerate(names)]
df = DataFrames.DataFrame(data)
df.value = map(i -> f(i...), rows)
return df
end

end #module
21 changes: 21 additions & 0 deletions ext/test_DataFrames.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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 TestContainersDataFrames

using Test

using DataFrames
using JuMP

function test_dimension_data_variable()
model = Model()
@variable(model, x[i = 2:4, j = 1:2], container = DataFrame)
@variable(model, y[i = 2:4, j = 1:2; isodd(i+j)], container = DataFrame)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The more I think about this, the less I like it. I prefer the explicit "add a new column to an existing dataframe" than the "construct a dataframe from these indices." The latter approach is just going to encourage the same nested loops as the GAMS example.


return
end

end