# Spaces in query.jl

**URL:** https://discourse.julialang.org/t/spaces-in-query-jl/8527
**Category:** Data
**Created:** [January 22, 2018, 5:18pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527 "2018-01-22T17:18:05Z")
**Posts on this page:** 16
**Page:** 2

<div class="post-metadata">

### Author: ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)
#### Post date: [November 2, 2018, 4:04pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/21 "2018-11-02T16:04:53Z")

</div>

@davidanthoff How will this work for a situation like this:

```julia
@from i in df begin
	       @where i.Parking_Tax == true
	       @select i
	       @collect DataFrame
       end

```

where the name of the column is “Parking Tax”. Previous version of DataFrames converted the name in “Parking\_Tax” but not the latest version. So now I’m getting: `type NamedTuple has no field Parking_Tax`

I mean this does work but it’s part of a tutorial for beginners and it really makes things ugly and complicated:

```julia
@from i in df begin
	@where getproperty(i, Symbol("Parking Tax")) == true
	@select i
	@collect DataFrame
end

```

* * *

### Update 1

This is acceptable, but if there’s a better way, I’d be grateful to learn about it:

```julia
@from i in df begin
	@where i[Symbol("Parking Tax")] == true
	@select i
	@collect DataFrame
end

```

Thanks

---

<div class="post-metadata">

### Author: ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)
#### Post date: [November 2, 2018, 4:37pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/22 "2018-11-02T16:37:41Z")

</div>

Not sure it applies here, but `foo.”bar”` is valid Julia syntax and calls getproperty with a string.

---

<div class="post-metadata">

### Author: ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)
#### Post date: [November 2, 2018, 4:48pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/23 "2018-11-02T16:48:35Z")

</div>

Thanks, yes, I tried that but it errors out because there is no `getproperty` defined which accepts a string as its second argument.

I ended up renaming the columns

```julia
rename!(df, [n => replace(string(n), " "=>"_") |> Symbol for n in names(df)])

```

---

<div class="post-metadata">

### Author: ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)
#### Post date: [November 2, 2018, 7:13pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/24 "2018-11-02T19:13:47Z")

</div>

If you create macro S\_str then you could use `@where i[S"Parking Tax"]==true` . Is that unacceptable for you too?

I was played with MWE maybe it could be useful for somebody:

```julia
julia> using DataFrames, Query, CSV

julia> macro S_str(a) :(Symbol($a)) end

julia> io = IOBuffer("""Parking Tax,col2
       true,2
       false,6""");

julia> df = CSV.File(io) |> DataFrame
2×2 DataFrame
│ Row │ Parking Tax │ col2 │
│ │ Bool⍰ │ Int64⍰ │
├─────┼─────────────┼────────┤
│ 1 │ true │ 2 │
│ 2 │ false │ 6 │

julia> @from i in df begin
               @where i[S"Parking Tax"]==true
               @select i
               @collect DataFrame
       end
1×2 DataFrame
│ Row │ Parking Tax │ col2 │
│ │ Bool⍰ │ Int64⍰ │
├─────┼─────────────┼────────┤
│ 1 │ true │ 2 │

```

---

<div class="post-metadata">

### Author: ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)
#### Post date: [November 2, 2018, 7:45pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/25 "2018-11-02T19:45:24Z")

</div>

I think that this looks quite nice - but I’d rather stay away from it in a beginners tutorial.

---

<div class="post-metadata">

### Author: ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)
#### Post date: [November 2, 2018, 8:04pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/26 "2018-11-02T20:04:45Z")

</div>

Oh sorry! I missed that it is for beginners. But do you plan to put there this line?

> [@essenciary](#):
>
> rename!(df, [n =\> replace(string(n), " “=\>”\_") |\> Symbol for n in names(df)])

---

<div class="post-metadata">

### Author: ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)
#### Post date: [November 2, 2018, 8:17pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/27 "2018-11-02T20:17:38Z")

</div>

Comprehensions have been previously introduced - but maybe you’re right and should be done with an iteration. 🤔

Edit 1:  
Yes, definitely, good point! At least I can show the iteration and add that it can be done with a comprehension. Having the two versions side by side should clarify the comprehension syntax too.

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [November 2, 2018, 8:44pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/28 "2018-11-02T20:44:33Z")

</div>

`rename!` can also take a function if you want to apply the same transformation to all names:

```julia
julia> rename!(df) do n
           s = replace(string(n), ' ' => '_')
           Symbol(s)
       end

```

which is not a one liner but is probably a bit easier to parse visually.

Overall it feels like renaming columns is the best solution as it also offers this nice extra exercise.

---

<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: [November 2, 2018, 9:37pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/29 "2018-11-02T21:37:57Z")

</div>

I don’t have a better idea than what was posted here already… At the end of the day this boils down to how one can interact with named tuples in julia.

I do think a macro a la `s"foo bar"` that is equivalent to `Symbol("foo bar")` would be nice, but that should probably be done (if at all) in base…

---

<div class="post-metadata">

### Author: ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)
#### Post date: [November 2, 2018, 9:55pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/30 "2018-11-02T21:55:43Z")

</div>

FWIW, you can also do `CSV.File(..., normalizenames=true)` on import to avoid such names.

---

<div class="post-metadata">

### Author: ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)
#### Post date: [November 8, 2018, 2:46pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/31 "2018-11-08T14:46:56Z")

</div>

What about adding a `getproperty` method for strings, so that foo.“bar baz” Just 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: [November 11, 2018, 8:28pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/32 "2018-11-11T20:28:12Z")

</div>

> [@essenciary](#):
>
> What about adding a `getproperty` method for strings, so that foo.“bar baz” Just Works?

I like that idea, but it would have to be implemented in base for `NamedTuple`, otherwise it would be a bad case of type piracy.

---

<div class="post-metadata">

### Author: ![gajomi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gajomi/32/7655_2.png) [@gajomi](https://discourse.julialang.org/u/gajomi)
#### Post date: [April 18, 2019, 7:14pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/33 "2019-04-18T19:14:44Z")

</div>

I just wanted to give a shout out to my fellow `S_str` macro implementors. Working with data in the wild with lots of spaces in the column names I ended up independently stumbling upon this as well as a way to save lots of typing while not having to worry about normalization. If a critical mass of practitioners end up doing this maybe it should get a home of its own… somewhere.

---

<div class="post-metadata">

### Author: ![mantzaris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mantzaris/32/3852_2.png) [@mantzaris](https://discourse.julialang.org/u/mantzaris)
#### Post date: [July 21, 2020, 4:37am UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/34 "2020-07-21T04:37:15Z")

</div>

I just tried this out now with my data having spaces in the column names, and it worked `Symbol("hello world")`

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [July 21, 2020, 6:53am UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/35 "2020-07-21T06:53:25Z")

</div>

With the latest DataFrames version, this works:

```julia
julia> using DataFrames

julia> df = DataFrame("My column name" => rand(5))
5×1 DataFrame
│ Row │ My column name │
│ │ Float64 │
├─────┼────────────────┤
│ 1 │ 0.532892 │
│ 2 │ 0.572881 │
│ 3 │ 0.0794647 │
│ 4 │ 0.189205 │
│ 5 │ 0.19475 │

julia> df."My column name"
5-element Array{Float64,1}:
 0.5328924031274789
 0.5728810432735243
 0.07946465108113787
 0.18920465409371334
 0.19475031867591586

```

---

<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: [July 21, 2020, 6:57pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/36 "2020-07-21T18:57:50Z")

</div>

The Query.jl story just depends on what is supported for standard named tuples. If base added support for `x."field name"` for named tuples, then one could use that syntax in Query.jl as well.

But I think base might think that `x.var"field name"` is good enough? That should work now already.

[Previous page](https://discourse.julialang.org/t/spaces-in-query-jl/8527.md?page=1)
