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

getindex should return a NamedArray (even when using n[:]) #57

Closed
s-celles opened this issue Dec 8, 2017 · 15 comments
Closed

getindex should return a NamedArray (even when using n[:]) #57

s-celles opened this issue Dec 8, 2017 · 15 comments

Comments

@s-celles
Copy link

s-celles commented Dec 8, 2017

Hello,

I noticed that

In[3]: n = NamedArray(rand(2,4,3))
Out[3]:
2×4×3 Named Array{Float64,3}

[:, :, C=1] =
A ╲ B │         1          2          3          4
──────┼───────────────────────────────────────────
10.746684   0.352576   0.859066  0.0463972
20.500173    0.32667   0.146571   0.796928

[:, :, C=2] =
A ╲ B │        1         2         3         4
──────┼───────────────────────────────────────
10.306856  0.242292  0.630703  0.548638
20.499724  0.184323  0.407832  0.684586

[:, :, C=3] =
A ╲ B │         1          2          3          4
──────┼───────────────────────────────────────────
10.841066  0.0542213   0.715639  0.0993138
20.312623   0.492007     0.5172   0.410809

In[4]: n[2,:,:]
Out[4]:
4×3 Named Array{Float64,2}
B ╲ C │        1         2         3
──────┼─────────────────────────────
10.500173  0.499724  0.312623
20.32667  0.184323  0.492007
30.146571  0.407832    0.5172
40.796928  0.684586  0.410809

but

In[5]: n[:]
Out[5]:
24-element Array{Float64,1}:
 0.746684 
 0.500173 
 0.352576 
 0.32667  
 0.859066 
 0.146571 
 0.0463972
 0.796928 
 0.306856 
 0.499724 
 0.242292 
 0.184323 
 0.630703 
 0.407832 
 0.548638 
 0.684586 
 0.841066 
 0.312623 
 0.0542213
 0.492007 
 0.715639 
 0.5172   
 0.0993138
 0.410809 

I was expecting a NamedArray to be returned

Kind regards

@s-celles
Copy link
Author

s-celles commented Dec 8, 2017

Flattening a NamedArray could help to convert it to other kind of datastructure (DataFrame...)
see queryverse/IterableTables.jl#65 (comment)

@nalimilan
Copy link
Contributor

That's consistent with Array:

julia> x = [1 2; 3 4]
2×2 Array{Int64,2}:
 1  2
 3  4

julia> x[:]
4-element Array{Int64,1}:
 1
 3
 2
 4

@davidavdav
Copy link
Owner

My question would be: what would the names be in such case?

@s-celles
Copy link
Author

s-celles commented Dec 8, 2017

The names (or value) of indexes.

@davidavdav
Copy link
Owner

...which are, for your example?

@s-celles
Copy link
Author

s-celles commented Dec 8, 2017

Something like

24-element Named Array{Float64,1}
A  │ B │ C │ 
───┼───┼───┼──────────
111...
...

@davidavdav
Copy link
Owner

You have to be more explicit. What is the result you would have wanted?

@s-celles
Copy link
Author

s-celles commented Dec 8, 2017

A, B, C columns should looks like

using IterTools
collect(product(1:2,1:4,1:3))
Out[4]:
24-element Array{Tuple{Int64,Int64,Int64},1}:
 (1, 1, 1)
 (2, 1, 1)
 (1, 2, 1)
 (2, 2, 1)
 (1, 3, 1)
 (2, 3, 1)
 (1, 4, 1)
 (2, 4, 1)
 (1, 1, 2)
 (2, 1, 2)
 (1, 2, 2)
 (2, 2, 2)
 (1, 3, 2)
 (2, 3, 2)
 (1, 4, 2)
 (2, 4, 2)
 (1, 1, 3)
 (2, 1, 3)
 (1, 2, 3)
 (2, 2, 3)
 (1, 3, 3)
 (2, 3, 3)
 (1, 4, 3)
 (2, 4, 3)

and forth column (because we had a 3 dimensions NamedArray) should contains values

@nalimilan
Copy link
Contributor

That kind of makes sense. But note that there's no type-stability issue here stricto sensu.

@s-celles s-celles changed the title getindex is not type stable getindex should return a NamedArray (even when using n[:]) Dec 8, 2017
@s-celles
Copy link
Author

s-celles commented Dec 8, 2017

ok so I'm changing title

@davidavdav
Copy link
Owner

This would also apply to any subset-of-dimensions-preserving reshaping then, I suppose.

@s-celles
Copy link
Author

s-celles commented Dec 8, 2017

yes but you should noticed that n[2, :] raises

WARNING: Partial linear indexing is deprecated. Use `reshape(A, Val{2})` to make the dimensionality of the array match the number of indices.

@s-celles
Copy link
Author

s-celles commented Dec 8, 2017

Thanks @davidavdav

@davidavdav
Copy link
Owner

Ah---I could have used product() from IterTools---I missed that, couldn't find product.
collect(product(names(n)...)).

@s-celles
Copy link
Author

s-celles commented Dec 8, 2017

Sorry I edited my post

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants