HarlanH/julia — pull request #3 by tshort (2012-06-28 02:28:38+00:00)
Title: First cut at adding an Index type for row and column indexing.

Hi again, Harlan.

This is the idea we sort-of touched on in the other pull-request thread. Basically, most indexing operations are passed to the function index() which tries to return a vector of integers.

The advantages are: (1) fewer combinations of row and column indexes, and (2) faster look-up of columns using a Dict (this should also help within and friends).

Here are results from examples included right in data.jl:

```jl
julia> srand(1)

julia> d1 = PooledDataVec(randi(3,20))
[3,1,1,3,2,3,3,1,3,3,2,2,1,2,2,1,1,1,1,1]

julia> d1[1] = NA
NA

julia> d2 = PooledDataVec(randi(2,20))
[1,1,1,1,1,1,2,2,1,2,2,1,2,1,2,1,2,2,1,1]

julia> d3 = DataVec(randn(20))
[-1.9281904783031796,-0.3440719649083631,1.0172501035542147,-0.48237977806678745,-0.45692987411395924,-0.9557701054607342,0.3107557448262618,-1.3141141434566452,-0.11821900294846033,0.3718388535412408,-0.6253944720950736,-0.8676474982941486,-0.549215456020029,-2.444501594406628,-0.542807145218249,-0.9458359171605532,-0.9521304724688996,-2.1565939874410254,-0.06925979990444593,-0.9161768852275985]

julia> d = DataFrame({d1,d2,d3}, ["d1","d2","d3"])
DataFrame({[NA,1,1,3,2,3,3,1,3,3,2,2,1,2,2,1,1,1,1,1], [1,1,1,1,1,1,2,2,1,2,2,1,2,1,2,1,2,2,1,1] ... },Index({"d1"=>1,"d2"=>2,"d3"=>3},["d1", "d2", "d3"]),SimpleIndex(20))

julia>

julia> d[1]
[NA,1,1,3,2,3,3,1,3,3,2,2,1,2,2,1,1,1,1,1]

julia> d["d3"]
[-1.9281904783031796,-0.3440719649083631,1.0172501035542147,-0.48237977806678745,-0.45692987411395924,-0.9557701054607342,0.3107557448262618,-1.3141141434566452,-0.11821900294846033,0.3718388535412408,-0.6253944720950736,-0.8676474982941486,-0.549215456020029,-2.444501594406628,-0.542807145218249,-0.9458359171605532,-0.9521304724688996,-2.1565939874410254,-0.06925979990444593,-0.9161768852275985]

julia> d[[1,2]]
DataFrame({[NA,1,1,3,2,3,3,1,3,3,2,2,1,2,2,1,1,1,1,1], [1,1,1,1,1,1,2,2,1,2,2,1,2,1,2,1,2,2,1,1]},Index({"d1"=>1,"d2"=>2},["d1", "d2"]),SimpleIndex(20))

julia> d[["d1","d3", "d2"]]
DataFrame({[NA,1,1,3,2,3,3,1,3,3,2,2,1,2,2,1,1,1,1,1] ... [1,1,1,1,1,1,2,2,1,2,2,1,2,1,2,1,2,2,1,1]},Index({"d1"=>1,"d2"=>3,"d3"=>2},["d1", "d3", "d2"]),SimpleIndex(20))

julia> d[:]
DataFrame({[NA,1,1,3,2,3,3,1,3,3,2,2,1,2,2,1,1,1,1,1], [1,1,1,1,1,1,2,2,1,2,2,1,2,1,2,1,2,2,1,1] ... },Index({"d1"=>1,"d2"=>2,"d3"=>3},["d1", "d2", "d3"]),SimpleIndex(20))

julia> d[1:2]
DataFrame({[NA,1,1,3,2,3,3,1,3,3,2,2,1,2,2,1,1,1,1,1], [1,1,1,1,1,1,2,2,1,2,2,1,2,1,2,1,2,2,1,1]},Index({"d1"=>1,"d2"=>2},["d1", "d2"]),SimpleIndex(20))

julia> d[2:end]
DataFrame({[1,1,1,1,1,1,2,2,1,2,2,1,2,1,2,1,2,2,1,1] ... },Index({"d2"=>1,"d3"=>2},["d2", "d3"]),SimpleIndex(20))

julia> d[[true,false,true]]
DataFrame({[NA,1,1,3,2,3,3,1,3,3,2,2,1,2,2,1,1,1,1,1] ... },Index({"d1"=>1,"d3"=>2},["d1", "d3"]),SimpleIndex(20))

julia> d[:,1]
[NA,1,1,3,2,3,3,1,3,3,2,2,1,2,2,1,1,1,1,1]

julia> d[1:3,1]
[NA,1,1]

julia> d[10:end,1]
[3,2,2,1,2,2,1,1,1,1,1]

julia> d[[1,2,3],1]
[NA,1,1]

julia> d[d["d1"] .== 1,1]
[1,1,1,1,1,1,1,1,1]

julia> d[DataVec([1:3]),1]
[NA,1,1]

julia> d[1:3,1]
[NA,1,1]

julia> d[10:end,1]
[3,2,2,1,2,2,1,1,1,1,1]

julia> d[1:3,1]
[NA,1,1]

julia> d[1:3,"d3"]
[-1.9281904783031796,-0.3440719649083631,1.0172501035542147]

julia> d[1:3,[1,2]]
DataFrame({[NA,1,1], [1,1,1]},Index({"d1"=>1,"d2"=>2},["d1", "d2"]),SimpleIndex(3))

julia> d[1:3,["d1","d3", "d2"]]
DataFrame({[NA,1,1], [-1.9281904783031796,-0.3440719649083631,1.0172501035542147], [1,1,1]},Index({"d1"=>1,"d2"=>3,"d3"=>2},["d1", "d3", "d2"]),SimpleIndex(3))

julia> d[1:3,:]
DataFrame({[NA,1,1], [1,1,1], [-1.9281904783031796,-0.3440719649083631,1.0172501035542147]},Index({"d1"=>1,"d2"=>2,"d3"=>3},["d1", "d2", "d3"]),SimpleIndex(3))

julia> d[1:3,1:2]
DataFrame({[NA,1,1], [1,1,1]},Index({"d1"=>1,"d2"=>2},["d1", "d2"]),SimpleIndex(3))

julia> d[1:3,2:end]
DataFrame({[1,1,1], [-1.9281904783031796,-0.3440719649083631,1.0172501035542147]},Index({"d2"=>1,"d3"=>2},["d2", "d3"]),SimpleIndex(3))

julia> d[1:3,[true,false,true]]
DataFrame({[NA,1,1], [-1.9281904783031796,-0.3440719649083631,1.0172501035542147]},Index({"d1"=>1,"d3"=>2},["d1", "d3"]),SimpleIndex(3))

julia> d[:,:]
DataFrame({[NA,1,1,3,2,3,3,1,3,3,2,2,1,2,2,1,1,1,1,1], [1,1,1,1,1,1,2,2,1,2,2,1,2,1,2,1,2,2,1,1] ... },Index({"d1"=>1,"d2"=>2,"d3"=>3},["d1", "d2", "d3"]),SimpleIndex(20))

```

Before I go any further, I'd like to have you look through this and see if it's agreeable.

I also see that there are a few other commits that must have occurred after you pulled in my stuff. One that is particularly important is the "fix summarise" commit.