Difference between "$i" and :$("$i") in Julia DataFrame

Viewed 38

When i=1, both println(typeof("$i")) and println(typeof(:($("$i")))) give String, and both println("$i") and println(:($("$i"))) give 1. However, the following code

using DataFrames
df = DataFrame()
i = 1
df.:($("$i")) = [0.0]
println(df)

works, but the following code

using DataFrames
df = DataFrame()
i = 1
df."$i" = [0.0]
println(df)

does not work. What is wrong with the latter code?

1 Answers

I would not recommend you to rely on the either syntax. These are corner cases of Julia parser.

The fact that:

df.:($("$i")) = [0.0]

works is most likely not a conscious design decision on Julia parser side. Note that:

df.:($("$i"))

fails.

As it is explained in https://dataframes.juliadata.org/stable/man/getting_started/:

Note that in the forms df."col" and :var"col" variable interpolation into a string using $ does not work.

If you want to use interpolation either directly call setproperty! and getproperty or use getindex/setindex!, in this case:

df[!, "$i"] = [0.0]
Related