# Query.@orderby destroys DataFrame

**URL:** <https://discourse.julialang.org/t/query-orderby-destroys-dataframe/27114>\
**Category:** General Usage\
**Tags:** query, dataframes\
**Created:** [August 2, 2019, 2:05pm UTC](https://discourse.julialang.org/t/query-orderby-destroys-dataframe/27114 "2019-08-02T14:05:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![hmmueller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hmmueller/32/9617_2.png) [@hmmueller](https://discourse.julialang.org/u/hmmueller)\
**Post date:** [August 2, 2019, 2:05pm UTC](https://discourse.julialang.org/t/query-orderby-destroys-dataframe/27114/1 "2019-08-02T14:05:48Z")

</div>

This code does not work:

```julia
using DataFrames, Query
df = DataFrame(A=[1,2,3], B=[4,5,6]);
fromdata = @from i in df begin
           @where i.B > 3
           @select {i.A}
           @orderby i.A
           @collect DataFrame
        end 
println(typeof(fromdata))
println(nrow(fromdata))
println(typeof(fromdata.A))

```

However, when you put a comment # in front of @orderBy, it works. Is this intended - why? (is an ordered DataFrame no longer a DataFrame?) … or is it a bug?

// Edit: Creating an explicit DataFrame again makes it work:

```julia
...
fromData = DataFrame(@from ... @orderby ... end)
...

```

// Edit: Rewritten question so that a naive copy/paste of the code should show the error.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 2, 2019, 2:14pm UTC](https://discourse.julialang.org/t/query-orderby-destroys-dataframe/27114/2 "2019-08-02T14:14:51Z")

</div>

where are these macros coming from?

---

<div class="post-metadata">

**Author:** ![hmmueller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hmmueller/32/9617_2.png) [@hmmueller](https://discourse.julialang.org/u/hmmueller)\
**Post date:** [August 2, 2019, 2:17pm UTC](https://discourse.julialang.org/t/query-orderby-destroys-dataframe/27114/3 "2019-08-02T14:17:05Z")

</div>

Query (I added the using in my question).

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [August 2, 2019, 3:16pm UTC](https://discourse.julialang.org/t/query-orderby-destroys-dataframe/27114/4 "2019-08-02T15:16:10Z")

</div>

Odd, it works for me. (DataFrames v0.19.1, Query v0.12.0.)

```julia
julia-1.3> using DataFrames, Query

julia-1.3> df = DataFrame(A=[1,2,3], B=[4,5,6]);

julia-1.3> fromdata = @from i in df begin
                      @where i.B > 3
                      @select {i.A}
                      @orderby i.A
                      @collect DataFrame
                   end
3x1 query result
A
─
1
2
3

```

First suggestion - a restart. I see many world age errors when I make mistakes in Query… 🙂

I’m not sure, but is this alternative syntax equivalent?

```julia
df |> @filter(_.B > 3) |> @select(:A) |> @orderby(_.A) |> collect |> DataFrame

```

---

<div class="post-metadata">

**Author:** ![hmmueller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hmmueller/32/9617_2.png) [@hmmueller](https://discourse.julialang.org/u/hmmueller)\
**Post date:** [August 24, 2019, 5:58pm UTC](https://discourse.julialang.org/t/query-orderby-destroys-dataframe/27114/5 "2019-08-24T17:58:36Z")

</div>

Thanks - I went down some other track (using sort!) … maybe it’s a problem with versions - I was using 1.1 (and am now on 1.2), you tried it in 1.3. I also did not check which package version I use(d) …

---

<div class="post-metadata">

**Author:** ![xin-jin](https://avatars.discourse-cdn.com/v4/letter/x/b2d939/32.png) [@xin-jin](https://discourse.julialang.org/u/xin-jin)\
**Post date:** [August 24, 2019, 7:37pm UTC](https://discourse.julialang.org/t/query-orderby-destroys-dataframe/27114/6 "2019-08-24T19:37:08Z")

</div>

I just tried your code in 1.2, and it works.

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [August 25, 2019, 6:12pm UTC](https://discourse.julialang.org/t/query-orderby-destroys-dataframe/27114/7 "2019-08-25T18:12:17Z")

</div>

Yeah, I also can’t replicate that error.

---

<div class="post-metadata">

**Author:** ![hmmueller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hmmueller/32/9617_2.png) [@hmmueller](https://discourse.julialang.org/u/hmmueller)\
**Post date:** [August 25, 2019, 6:21pm UTC](https://discourse.julialang.org/t/query-orderby-destroys-dataframe/27114/8 "2019-08-25T18:21:34Z")

</div>

… when commenting in the @orderby? I get it “reliably” (in my Julia 1.1.1 installation - I have not yet upgraded … **//edit: but let me upgrade - maybe I’m happy then!** ) - see the image below. More readably the output for the first two println()s is:

```julia
julia> println(typeof(fromdata))
QueryOperators.EnumerableMap{NamedTuple{(:A,),Tuple{Int64}},QueryOperators.EnumerableFilter{NamedTuple{(:A, :B),Tuple{Int64,Int64}},QueryOperators.EnumerableIterable{NamedTuple{(:A, :B),Tuple{Int64,Int64}},Tables.DataValueRowIterator{NamedTuple{(:A, :B),Tuple{Int64,Int64}},Tables.RowIterator{NamedTuple{(:A, :B),Tuple{Array{Int64,1},Array{Int64,1}}}}}},getfield(Main, Symbol("##2384#2386"))},getfield(Main, Symbol("##2385#2387"))}

julia> println(nrow(fromdata))
ERROR: MethodError: no method matching nrow(::QueryOperators.EnumerableMap{NamedTuple{(:A,),Tuple{Int64}},QueryOperators.EnumerableFilter{NamedTuple{(:A, :B),Tuple{Int64,Int64}},QueryOperators.EnumerableIterable{NamedTuple{(:A, :B),Tuple{Int64,Int64}},Tables.DataValueRowIterator{NamedTuple{(:A, :B),Tuple{Int64,Int64}},Tables.RowIterator{NamedTuple{(:A, :B),Tuple{Array{Int64,1},Array{Int64,1}}}}}},getfield(Main, Symbol("##2384#2386"))},getfield(Main, Symbol("##2385#2387"))})
Closest candidates are:
  nrow(::DataFrame) at C:\Users\h.mueller\.julia\packages\DataFrames\VrZOl\src\dataframe\dataframe.jl:278
  nrow(::SubDataFrame) at C:\Users\h.mueller\.julia\packages\DataFrames\VrZOl\src\subdataframe\subdataframe.jl:114
Stacktrace:
 [1] top-level scope at none:0

```

.

 ![orderbyproblem](https://global.discourse-cdn.com/julialang/original/3X/8/7/874eadbee398b37f5565868f6aa86906fd3034bf.png)

---

<div class="post-metadata">

**Author:** ![hmmueller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hmmueller/32/9617_2.png) [@hmmueller](https://discourse.julialang.org/u/hmmueller)\
**Post date:** [August 25, 2019, 6:31pm UTC](https://discourse.julialang.org/t/query-orderby-destroys-dataframe/27114/9 "2019-08-25T18:31:09Z")

</div>

No - also does not work with 1.2.0 (DataFrames is 0.19.2, Query is 0.12.1).

(Edit: I have now rewritten the original question so that the code shown shows the error - just to make sure that people who copy it without reading the earlier instruction to remove the # will “hopefully” get the error).
