# 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:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![RandomString123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/randomstring123/32/3194_2.png) [@RandomString123](https://discourse.julialang.org/u/RandomString123)
#### Post date: [January 22, 2018, 5:18pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/1 "2018-01-22T17:18:05Z")

</div>

Using DataFrames and CSV it is possible to create a table that has spaces in the column names. When this is done is there a way to reference these columns using query.jl? Or do I need to rename the columns to remove spaces?

---

<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: [January 22, 2018, 11:25pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/2 "2018-01-22T23:25:04Z")

</div>

I think you’ll have to rename them…

---

<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: [January 23, 2018, 12:39pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/3 "2018-01-23T12:39:17Z")

</div>

Yeah, spaces in column names should only be produced when you really ask for them (e.g. because you need to preserve them exactly). I’ve filed [https://github.com/JuliaData/CSV.jl/issues/158](https://github.com/JuliaData/CSV.jl/issues/158).

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [January 23, 2018, 2:29pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/4 "2018-01-23T14:29:08Z")

</div>

What do other similar packages in Python or R do when there are spaces in column names?

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [January 23, 2018, 2:44pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/5 "2018-01-23T14:44:26Z")

</div>

R’s data frames allow column attributes, so a package like Haven will allow variable labels.

I think DataFrames could do this with a simple `Dict` for column meta-data, once I get Julia working on my work computer again I’m going to give it a try.

---

<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: [January 23, 2018, 2:54pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/6 "2018-01-23T14:54:39Z")

</div>

R’s `read.csv` replaces spaces with `.` by default (dots are the equivalent of underscores in R since they can appear in identifiers). haven doesn’t support CSV files, but the accompanying readr package provides `read_csv`, which keeps spaces in column names (without an option to remove them). data.table’s `fread` doesn’t apply any transformation by default either, but support the `check.names` arguments for that.

So the existing programs adopt a variety of solutions. We could at least support an argument, but whether it should be the default isn’t clear.

---

<div class="post-metadata">

### Author: ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)
#### Post date: [January 23, 2018, 3:20pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/7 "2018-01-23T15:20:24Z")

</div>

R also allows backticks like:

```julia
d$`hello world` = 3

```

For DataFramesMeta, I’ve thought about trying to parse backticks as symbols with spaces. It’d be trickier to do that in Query because you need to parse:

```julia
x.`hello world` = 3
# or maybe
x."hello world" = 3 # this parses a little better in Julia

```

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [January 23, 2018, 3:22pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/8 "2018-01-23T15:22:35Z")

</div>

What about `pandas`? (which everybody tells me is the cat’s pyjamas 😉 )

---

<div class="post-metadata">

### Author: ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)
#### Post date: [January 23, 2018, 3:26pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/9 "2018-01-23T15:26:19Z")

</div>

Pandas and R can both use strings for column names.

```julia
d["hello world"]

```

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [January 23, 2018, 3:28pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/10 "2018-01-23T15:28:39Z")

</div>

In that case, maybe the default for Julia’s DataFrames should also be to allow them without renaming?  
Could optionally aliases of the names with \_ replacing the spaces also be added (for ease of typing)?

i.e. `d[Symbol("hello world")` or `d[:hello_world]` would both work

---

<div class="post-metadata">

### Author: ![RandomString123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/randomstring123/32/3194_2.png) [@RandomString123](https://discourse.julialang.org/u/RandomString123)
#### Post date: [January 23, 2018, 3:34pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/11 "2018-01-23T15:34:30Z")

</div>

R was the reason I asked the initial question. While I don’t think spaces are used frequently in base R dplyr is designed to support spaces in columns names by surrounding all column names with backticks (``).

For example, this is basic R code that takes a file date field that was read in as part of CSV data and creates a new field that is the year of the file as an integer. Notice that the field read from the file was labeled “FILE DATE”.

```julia
    x %>% mutate(`year` = format(`FILE DATE`, "%Y"))

```

Personally I do not think supporting spaces is necessary. But I do think that white-space / special character handling should be consistent across the more popular packages to reduce confusion and the need to write code to translate column names between packages.

---

<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: [January 23, 2018, 4:18pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/12 "2018-01-23T16:18:09Z")

</div>

I tend to think we should make it possible to work with variables containing spaces (if only via `Symbol("...")`), but avoid creating them by default because they are inconvenient and not very useful most of the time (AFAICT). We could also allowing indexing with strings, converting them automatically to symbols, but that wouldn’t help for macros.

I’m not a fan of the idea of automatically having aliases matching underscores to spaces. That really sounds like the worst features of R, which make programming unpredictable. This kind of thing should rather be handled by auto-completion.

---

<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: [January 23, 2018, 5:26pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/13 "2018-01-23T17:26:25Z")

</div>

I agree with @nalimilan, that sounds like the best path forward.

For [Query.jl](https://github.com/davidanthoff/Query.jl), this at the end of the day boils down to what supported named tuples have for fields with spaces in them. I think something like `row[Symbol("foo bar")]` actually works, and maybe with this whole new constant folding this might not even result in a type instability?

In theory one could add support to named tuples to index with a string, so that `row["foo bar"]` would work, but that strikes me as too weird.

One thing that would really help here was a string macro that creates a symbol, so something like `row[s"foo bar"]` would work, that to me seems like a good compromise for those cases?

---

<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: [January 23, 2018, 9:48pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/14 "2018-01-23T21:48:17Z")

</div>

> [@davidanthoff](#):
>
> One thing that would really help here was a string macro that creates a symbol, so something like row[s"foo bar"] would work, that to me seems like a good compromise for those cases?

Indeed, a very simple string macro works for that:

```julia-auto
macro s_str(s)
   quote
       Symbol($s)
   end
end

```

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [January 23, 2018, 9:54pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/15 "2018-01-23T21:54:29Z")

</div>

`s` can’t be used, because it’s part of the Regex syntax, although maybe `S` would be fine (and maybe indicates “Symbol” even better).

I like the idea! 👍

---

<div class="post-metadata">

### Author: ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)
#### Post date: [January 23, 2018, 11:12pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/16 "2018-01-23T23:12:12Z")

</div>

It’s been proposed before, but one potential issue is that it doesn’t play nicely with `.`, i.e. `foo.bar"zzz"` parses as `foo.@bar_str("zzz")`.

---

<div class="post-metadata">

### Author: ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)
#### Post date: [January 24, 2018, 4:53am UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/17 "2018-01-24T04:53:39Z")

</div>

Once upon a time when I was super into literate programming I’d have variables in R like `acceleration of gravity in meters per second squared`. I eventually went back to underscores, which make clear where a variable starts and where it ends. But it would be nice in user facing output to have underscores automatically replaced with spaces (tables, graphs, etc.)

---

<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: [January 24, 2018, 8:52am UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/18 "2018-01-24T08:52:08Z")

</div>

> [@bramtayl](#):
>
> But it would be nice in user facing output to have underscores automatically replaced with spaces (tables, graphs, etc.)

I’d rather use variable labels for this. See [Metadata for columns and/or DataFrames · Issue #35 · JuliaData/DataFrames.jl · GitHub](https://github.com/JuliaData/DataFrames.jl/issues/35).

---

<div class="post-metadata">

### Author: ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)
#### Post date: [January 24, 2018, 2:51pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/19 "2018-01-24T14:51:03Z")

</div>

Variable labels would definitely be a nice feature. But one of the whole points of literal programming is that you shouldn’t need variable labels: your variables should be descriptive enough as is. Maybe variable labels that are default to the variable name with spaces instead of underscores?

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [January 24, 2018, 6:17pm UTC](https://discourse.julialang.org/t/spaces-in-query-jl/8527/20 "2018-01-24T18:17:08Z")

</div>

The benefit of variable labels is that it eases the transition from code to published document. If you are able to tie each variable a nice label, then you can generate tables, graphs, etc. that look very good very easily. It saves hours of work in reformatting.

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