# Why does Julia prefix columnames and colors with a colon :?

**URL:** <https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198>\
**Category:** New to Julia\
**Created:** [December 1, 2018, 12:42am UTC](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198 "2018-12-01T00:42:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [December 1, 2018, 12:42am UTC](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198/1 "2018-12-01T00:42:04Z")

</div>

Why does Julia prefix variables and colors with a colon : ?

For example:  
`iris[:SepalWidth]`

` plot!(collect(1:10), rand(10), color=:red, label="red")`

---

<div class="post-metadata">

**Author:** ![alejandromerchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandromerchan/32/10500_2.png) [@alejandromerchan](https://discourse.julialang.org/u/alejandromerchan)\
**Post date:** [December 1, 2018, 12:45am UTC](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198/2 "2018-12-01T00:45:15Z")

</div>

That’s the representation for symbols. See [Metaprogramming · The Julia Language](https://docs.julialang.org/en/v1/manual/metaprogramming/#Symbols-1)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [December 1, 2018, 12:47am UTC](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198/3 "2018-12-01T00:47:03Z")

</div>

Just writing `red` would mean the variable `red`.

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [December 1, 2018, 1:40am UTC](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198/4 "2018-12-01T01:40:22Z")

</div>

And what would this mean:

iris[SepalWidth]

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [December 1, 2018, 2:06am UTC](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198/5 "2018-12-01T02:06:01Z")

</div>

That depends on whether SepalWidth is defined or not. It would work if it was a variable with value

```julia
SepalWidth = :SepalWidth

```

You can think of symbols as convenient values that have a special role in Julia programs. Presumably here `iris` is a dictionary with keys that are symbols.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 1, 2018, 2:08am UTC](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198/6 "2018-12-01T02:08:56Z")

</div>

See also: [Symbol (programming) - Wikipedia](https://en.wikipedia.org/wiki/Symbol_(programming))

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [December 1, 2018, 4:57pm UTC](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198/7 "2018-12-01T16:57:59Z")

</div>

I meant if it’s not a variable but a column name.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [December 1, 2018, 5:00pm UTC](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198/8 "2018-12-01T17:00:02Z")

</div>

If the item (`SepalWidth`) isn’t recognized as a value (such as integer, string, or a symbol), it will be looked up as a variable name. If the variable doesn’t exist, an error results.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 1, 2018, 7:29pm UTC](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198/9 "2018-12-01T19:29:26Z")

</div>

Note that you can also write `iris.SepalWidth`:

```julia
julia> iris.SepalWidth
150-element Array{Float64,1}:
 3.5
 3.0
 3.2
 3.1

```

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [December 2, 2018, 8:18am UTC](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198/10 "2018-12-02T08:18:36Z")

</div>

They are actually a nice feature, it’s probably a bit unfortunate that dataframes doesn’t also let you use the more ordinary indexing style with Ints just as easily. I’m pretty sure it is still the case, so it’s tempting to guess it was a design decision

```julia
xplot(x,y, c, l) = plot!(x, y, color=Symbol(c), label=String(l));
xplot(collect(1:10), rand(10), "red", :red)
```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 2, 2018, 8:22am UTC](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198/11 "2018-12-02T08:22:57Z")

</div>

> [@y4lu](#):
>
> it’s probably a bit unfortunate that dataframes doesn’t also let you use the more ordinary indexing style with Ints just as easily

Since column positions can be pretty accidental, I don’t think it is robust practice to use them for indexing in a dataframe.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [December 2, 2018, 8:31am UTC](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198/12 "2018-12-02T08:31:37Z")

</div>

That is true, but you could easily still opt in by using symbols, and have Ints as a fallback  
Add: `>?DataFrame` does have a few examples with Integer indexing

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 2, 2018, 9:20am UTC](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198/13 "2018-12-02T09:20:44Z")

</div>

Note that

> [@y4lu](#):
>
> dataframes doesn’t also let you use the more ordinary indexing style with Ints just as easily

is not correct — it works just fine, see the [relevant methods](https://github.com/JuliaData/DataFrames.jl/blob/51512559e8fe6eacce052401b53e1569a45d1b8d/src/dataframe/dataframe.jl#L248) in the source.

I was merely pointing out that while it works for DataFrames, I don’t think it is good practice for working with data. Merely regenerating data with a slightly different column order or an extra column can break code very easily.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [December 2, 2018, 9:38am UTC](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198/14 "2018-12-02T09:38:39Z")

</div>

Yes i just spotted that, and it’s mostly normal too. df[1] and df[:,1] are a little different still (or subject to change?) compared to operations on `df2 = reduce(hcat, getfields(df, :columns))`

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 2, 2018, 9:43am UTC](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198/15 "2018-12-02T09:43:43Z")

</div>

> [@y4lu](#):
>
> df[1] and df[:,1] are a little different still

I don’t quite understand what you mean here.

> [@y4lu](#):
>
> df2 = reduce(hcat, getfields(df, :columns))

I don’t know what `getfields` is. Did you misspell `getfield`? In any case, there is a `columns` accessor.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [December 2, 2018, 9:49am UTC](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198/16 "2018-12-02T09:49:32Z")

</div>

> [@Tamas\_Papp](#):
>
> Did you misspell `getfield` ?

That would be correct

> [@Tamas\_Papp](#):
>
> there is a `columns` accessor.

There was a `df.columns`, i’m not sure if there’s others

`df[:,1]` gives a copy warning, and `df2[1]` is scalar

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [December 2, 2018, 10:09am UTC](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198/17 "2018-12-02T10:09:53Z")

</div>

So how do you do matrix multiplication?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 2, 2018, 10:41am UTC](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198/18 "2018-12-02T10:41:52Z")

</div>

I mean the `column(::DataFrame)`, which is exported and part of the API. You should avoid accessing fields of a composite type unless that is explicitly declared as the way to work work with them.

> [@y4lu](#):
>
> So how do you do matrix multiplication?

Generally, for eg OLS, you form a design matrix. That is not a direct `hcat` of the columns though, eg for categorical variables, etc.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [December 2, 2018, 11:03am UTC](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198/19 "2018-12-02T11:03:44Z")

</div>

This one? [https://juliastats.github.io/StatsModels.jl/latest/formula.html](https://juliastats.github.io/StatsModels.jl/latest/formula.html)

.- I think i can more or less sum up my view as being ‘Documentation is great when you need it, but not needing it is still kind of preferable’, fwiw  
I’m quite probably terrible at API design myself though =) Singling out dataframes because it’s commonly one of the first packages people might use, and that is when user friendliness really either shines or hurts

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [December 3, 2018, 5:30am UTC](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198/20 "2018-12-03T05:30:19Z")

</div>

`columns()` is in the unexported names on DataFrames v0.14.1

[Next page](https://discourse.julialang.org/t/why-does-julia-prefix-columnames-and-colors-with-a-colon/18198.md?page=2)
