# Slicing a SparseAxisArray Variable in JuMP

**URL:** <https://discourse.julialang.org/t/slicing-a-sparseaxisarray-variable-in-jump/88332>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [October 6, 2022, 8:39am UTC](https://discourse.julialang.org/t/slicing-a-sparseaxisarray-variable-in-jump/88332 "2022-10-06T08:39:37Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Janogo](https://avatars.discourse-cdn.com/v4/letter/j/779978/32.png) [@Janogo](https://discourse.julialang.org/u/Janogo)\
**Post date:** [October 6, 2022, 8:39am UTC](https://discourse.julialang.org/t/slicing-a-sparseaxisarray-variable-in-jump/88332/1 "2022-10-06T08:39:37Z")

</div>

Hi everyone,

I wish to slice a SparseAxisArray Container in JuMP. The documentation says this should work:

```julia
julia> x = Containers.@container([i = 1:3, j = [:A, :B]; i > 1], (i, j))
JuMP.Containers.SparseAxisArray{Tuple{Int64, Symbol}, 2, Tuple{Int64, Symbol}} with 4 entries:
  [2, A] = (2, :A)
  [2, B] = (2, :B)
  [3, A] = (3, :A)
  [3, B] = (3, :B)

```

```julia
julia> y = x[:, :B]
JuMP.Containers.SparseAxisArray{Tuple{Int64, Symbol}, 1, Tuple{Int64}} with 2 entries:
  [2] = (2, :B)
  [3] = (3, :B)

```

If I try the same code in my command line I get this Error:

```julia
julia> x = Containers.@container([i = 1:3, j = [:A, :B]; i > 1], (i, j))
JuMP.Containers.SparseAxisArray{Tuple{Int64, Symbol}, 2, Tuple{Int64, Symbol}} with 4 entries:
  [2, A] = (2, :A)
  [2, B] = (2, :B)
  [3, A] = (3, :A)
  [3, B] = (3, :B)

```

```julia
julia> x[:,:B]
ERROR: ArgumentError: Indexing with `:` is not supported by Containers.SparseAxisArray
Stacktrace:
 [1] _colon_error(#unused#::Colon, args::Symbol)
   @ JuMP.Containers C:\Users\jaelsner\.julia\packages\JuMP\Y4piv\src\Containers\SparseAxisArray.jl:94
 [2] getindex(::JuMP.Containers.SparseAxisArray{Tuple{Int64, Symbol}, 2, Tuple{Int64, Symbol}}, ::Function, ::Symbol)
   @ JuMP.Containers C:\Users\jaelsner\.julia\packages\JuMP\Y4piv\src\Containers\SparseAxisArray.jl:129
 [3] top-level scope
   @ REPL[28]:1

```

Does anyone have an idea what’s the problem here? Thanks!

---

<div class="post-metadata">

**Author:** ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)\
**Post date:** [October 6, 2022, 12:36pm UTC](https://discourse.julialang.org/t/slicing-a-sparseaxisarray-variable-in-jump/88332/2 "2022-10-06T12:36:53Z")

</div>

Welcome! I cannot reproduce this with `JuMP v1.3.1` and `Julia 1.6.7` on a Linux machine. For me it works as stated in the documentation. Which versions are you using?

---

<div class="post-metadata">

**Author:** ![Janogo](https://avatars.discourse-cdn.com/v4/letter/j/779978/32.png) [@Janogo](https://discourse.julialang.org/u/Janogo)\
**Post date:** [October 6, 2022, 1:32pm UTC](https://discourse.julialang.org/t/slicing-a-sparseaxisarray-variable-in-jump/88332/3 "2022-10-06T13:32:31Z")

</div>

Hi Mike, that was the trick, thanks! I was running JuMP v1.1.1 and there slicing was not supported for SparseAxisArray.

---

<div class="post-metadata">

**Author:** ![felipecordera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felipecordera/32/43701_2.png) [@felipecordera](https://discourse.julialang.org/u/felipecordera)\
**Post date:** [October 19, 2022, 7:43pm UTC](https://discourse.julialang.org/t/slicing-a-sparseaxisarray-variable-in-jump/88332/4 "2022-10-19T19:43:28Z")

</div>

Hi everyone,

I have exactly the same problem when trying to reproduce the above example taken from the documentation. I am running Julia 1.8.2 and JuMP 1.3.1 on a Windows machine.

Any idea why could this be happening? Thanks!

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [October 19, 2022, 8:32pm UTC](https://discourse.julialang.org/t/slicing-a-sparseaxisarray-variable-in-jump/88332/5 "2022-10-19T20:32:19Z")

</div>

> Any idea why could this be happening? Thanks!

You aren’t actually using JuMP v1.3.1. What is `import Pkg; Pkg.status()`? If you recently upgraded your packages, remember to restart Julia for the changes to take effect.

---

<div class="post-metadata">

**Author:** ![felipecordera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felipecordera/32/43701_2.png) [@felipecordera](https://discourse.julialang.org/u/felipecordera)\
**Post date:** [October 19, 2022, 9:59pm UTC](https://discourse.julialang.org/t/slicing-a-sparseaxisarray-variable-in-jump/88332/6 "2022-10-19T21:59:29Z")

</div>

> [@Janogo](#):
>
> `x = Containers.@container([i = 1:3, j = [:A, :B]; i > 1], (i, j))`

It shows “JuMP v1.3.1”.

Now I realized that I can reproduce the example in the Julia shell, but not in a notebook (IJulia)… does that make any sense?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [October 19, 2022, 10:06pm UTC](https://discourse.julialang.org/t/slicing-a-sparseaxisarray-variable-in-jump/88332/7 "2022-10-19T22:06:45Z")

</div>

> Now I realized that I can reproduce the example in the Julia shell, but not in a notebook (IJulia)… does that make any sense?

No. It means you are running a different version of JuMP in the notebook. When did you update your packages? Have you restarted the notebook since?

Can you show the full output of `Pkg.status()` from a cell in the notebook?

---

<div class="post-metadata">

**Author:** ![felipecordera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felipecordera/32/43701_2.png) [@felipecordera](https://discourse.julialang.org/u/felipecordera)\
**Post date:** [October 19, 2022, 10:14pm UTC](https://discourse.julialang.org/t/slicing-a-sparseaxisarray-variable-in-jump/88332/8 "2022-10-19T22:14:13Z")

</div>

I updated the packages yesterday. And by now I have restarted the notebook, Julia, and my laptop.

When I run (from a cell in the notebook):

> import Pkg; Pkg.status()

I get:

Status `C:\Users\xxx\.julia\environments\v1.8\Project.toml`  
[336ed68f] CSV v0.10.4  
⌅ [324d7699] CategoricalArrays v0.10.6  
⌅ [a93c6f00] DataFrames v1.3.4  
[1313f7d8] DataFramesMeta v0.12.0  
[7806a523] DecisionTree v0.11.2  
[b4f34e82] Distances v0.10.7  
⌅ [31c24e10] Distributions v0.25.68  
[38e38edf] GLM v1.8.0  
[2e9cd046] Gurobi v0.11.3  
[7073ff75] IJulia v1.23.3  
[4076af6c] JuMP v1.3.1  
[f0e99cf1] MLBase v0.9.0  
[cc2ba9b6] MLDataUtils v0.5.4  
⌅ [b8f27783] MathOptInterface v1.7.0  
⌅ [b8a86587] NearestNeighbors v0.4.11  
[42b8e9d4] ParallelKMeans v1.0.1  
[a03496cd] PlotlyBase v0.8.19  
⌃ [91a5bcdd] Plots v1.35.3  
[3646fa90] ScikitLearn v0.6.4  
[f3b207a7] StatsPlots v0.15.4  
⌅ [bd369af6] Tables v1.7.0  
[9a3f8284] Random  
[10745b16] Statistics  
Info Packages marked with ⌃ and ⌅ have new versions available, but those with ⌅ cannot be upgraded. To see why use `status --outdated`

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [October 19, 2022, 10:25pm UTC](https://discourse.julialang.org/t/slicing-a-sparseaxisarray-variable-in-jump/88332/9 "2022-10-19T22:25:03Z")

</div>

And what is the exact error if you run:

```julia
using JuMP
x = Containers.@container([i = 1:3, j = [:A, :B]; i > 1], (i, j))
x[:, :A]

```

---

<div class="post-metadata">

**Author:** ![felipecordera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felipecordera/32/43701_2.png) [@felipecordera](https://discourse.julialang.org/u/felipecordera)\
**Post date:** [October 19, 2022, 10:34pm UTC](https://discourse.julialang.org/t/slicing-a-sparseaxisarray-variable-in-jump/88332/10 "2022-10-19T22:34:29Z")

</div>

This is the error it shows:

ArgumentError: Indexing with `:` is not supported by Containers.SparseAxisArray

Stacktrace:  
[1] \_colon\_error(#unused#::Colon, args::Symbol)  
@ JuMP.Containers C:\Users\iai\builds\InterpretableAI\SystemImage\SysImgBuilder.julia\packages\JuMP\60Bnj\src\Containers\SparseAxisArray.jl:94  
[2] getindex(::JuMP.Containers.SparseAxisArray{Tuple{Int64, Symbol}, 2, Tuple{Int64, Symbol}}, ::Function, ::Symbol)  
@ JuMP.Containers C:\Users\iai\builds\InterpretableAI\SystemImage\SysImgBuilder.julia\packages\JuMP\60Bnj\src\Containers\SparseAxisArray.jl:129  
[3] top-level scope  
@ In[1]:3  
[4] eval  
@ .\boot.jl:368 [inlined]  
[5] include\_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)  
@ Base .\loading.jl:1428

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [October 19, 2022, 10:35pm UTC](https://discourse.julialang.org/t/slicing-a-sparseaxisarray-variable-in-jump/88332/11 "2022-10-19T22:35:16Z")

</div>

> [@felipecordera](#):
>
> C:\Users\iai\builds\InterpretableAI\SystemImage\SysImgBuilder.julia\packages\JuMP\60Bnj\src\Containers\SparseAxisArray.jl:94

This suggests you built a system image with an older version of JuMP? If you update your packages, you need to rebuild the system image.

---

<div class="post-metadata">

**Author:** ![felipecordera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felipecordera/32/43701_2.png) [@felipecordera](https://discourse.julialang.org/u/felipecordera)\
**Post date:** [October 19, 2022, 10:55pm UTC](https://discourse.julialang.org/t/slicing-a-sparseaxisarray-variable-in-jump/88332/12 "2022-10-19T22:55:06Z")

</div>

That fixes the problem. Thank you!
