diff --git a/Project.toml b/Project.toml index 9e040d7d24e..9bf1d67ef8b 100644 --- a/Project.toml +++ b/Project.toml @@ -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" @@ -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"] diff --git a/ext/JuMPDataFramesExt.jl b/ext/JuMPDataFramesExt.jl new file mode 100644 index 00000000000..5d9195221e2 --- /dev/null +++ b/ext/JuMPDataFramesExt.jl @@ -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 diff --git a/ext/test_DataFrames.jl b/ext/test_DataFrames.jl new file mode 100644 index 00000000000..fc5a55ee906 --- /dev/null +++ b/ext/test_DataFrames.jl @@ -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) + + return +end + +end