# How to create a DataFrame from code literals, especially with Unitful?

**URL:** https://discourse.julialang.org/t/how-to-create-a-dataframe-from-code-literals-especially-with-unitful/135613
**Category:** New to Julia
**Tags:** dataframes, unitful
**Created:** [February 13, 2026, 2:57pm UTC](https://discourse.julialang.org/t/how-to-create-a-dataframe-from-code-literals-especially-with-unitful/135613 "2026-02-13T14:57:57Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)
#### Post date: [February 13, 2026, 2:57pm UTC](https://discourse.julialang.org/t/how-to-create-a-dataframe-from-code-literals-especially-with-unitful/135613/1 "2026-02-13T14:57:57Z")

</div>

Hi all! I’m working with some physical quantities and I’d like to use Unitful.jl for that. I am wondering two things:

- what is the most easily maintainable, and/or efficient way to crate a DataFrame from code literals (i.e. straight from Julia code, no csv or imports)?
- how to do this when a column has a type of some `Quantity` (for instance, `u"cm"`)?
- bonus: how to avoid the type ending up having `Union`s or optional (like `Float64?`, iiuc?) values

As an example, I managed to do this:

```julia-auto
df = DataFrame(Time = String[], Measure = Float64[])
push!(df, ["09:29", 1.1])
push!(df, ["09:30", 1.2])
push!(df, ["09:31", 1.1])
# [...]
df[!, :Measure] .*= u"cm"

```

but i feel like there is a better way. Thanks!

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [February 13, 2026, 4:01pm UTC](https://discourse.julialang.org/t/how-to-create-a-dataframe-from-code-literals-especially-with-unitful/135613/2 "2026-02-13T16:01:25Z")

</div>

I’m not sure if I really understand the question you’re asking, but is something like this helpful?

```julia-auto
julia> using DataFrames, Unitful 

julia> df = DataFrame([(; Time="09:29", Measure=1.1u"cm"),
                       (; Time="09:30", Measure=1.2u"cm"),
                       (; Time="09:31", Measure=1.1u"cm")])
3×2 DataFrame
 Row │ Time Measure   
     │ String Quantity… 
─────┼───────────────────
   1 │ 09:29 1.1 cm
   2 │ 09:30 1.2 cm
   3 │ 09:31 1.1 cm

```

---

<div class="post-metadata">

### Author: ![JonasWickman](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JonasWickman](https://discourse.julialang.org/u/JonasWickman)
#### Post date: [February 13, 2026, 4:37pm UTC](https://discourse.julialang.org/t/how-to-create-a-dataframe-from-code-literals-especially-with-unitful/135613/3 "2026-02-13T16:37:16Z")

</div>

Expanding on `@Mason`’s answer, doing the stacking the other way around also works,

```julia-auto
df = DataFrame(
    Time = ["09:29", "09:30", "09:31"],
    Measure = [1.1u"cm", 1.2u"cm", 1.1u"cm"]
)

3×2 DataFrame
 Row │ Time Measure   
     │ String Quantity… 
─────┼───────────────────
   1 │ 09:29 1.1 cm
   2 │ 09:30 1.2 cm
   3 │ 09:31 1.1 cm

```

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [February 13, 2026, 4:42pm UTC](https://discourse.julialang.org/t/how-to-create-a-dataframe-from-code-literals-especially-with-unitful/135613/4 "2026-02-13T16:42:23Z")

</div>

Yeah, lots of ways to skin the cat here. You can also avoid repeating the names if you want:

```julia-auto
julia> DataFrame([
               ("09:29", 1.1u"cm"),
               ("09:30", 1.2u"cm"),
               ("09:31", 1.1u"cm")
           ],
           ["Time", "Measure"])
3×2 DataFrame
 Row │ Time Measure
     │ String Quantity…
─────┼─────────────────────
   1 │ 09:29 1.1 cm
   2 │ 09:30 1.2 cm
   3 │ 09:31 1.1 cm

```

---

<div class="post-metadata">

### Author: ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)
#### Post date: [February 13, 2026, 6:23pm UTC](https://discourse.julialang.org/t/how-to-create-a-dataframe-from-code-literals-especially-with-unitful/135613/5 "2026-02-13T18:23:52Z")

</div>

> [@Mason](#):
>
> I’m not sure if I really understand the question you’re asking, but is something like this helpful?
> 
> ```julia-auto
> julia> using DataFrames, Unitful 
> 
> julia> df = DataFrame([(; Time="09:29", Measure=1.1u"cm"),
> (; Time="09:30", Measure=1.2u"cm"),
> (; Time="09:31", Measure=1.1u"cm")])
> 3×2 DataFrame
> Row │ Time Measure   
> │ String Quantity… 
> ─────┼───────────────────
> 1 │ 09:29 1.1 cm
> 2 │ 09:30 1.2 cm
> 3 │ 09:31 1.1 cm
> 
> ```

Yes you understood correctly. Thank you for your answer, i wasn’t aware of this DataFrame constructor. I was wondering if this approach however could be made more efficient, given that it creates a new `NamedTuple` for each data entry. But on the other hand, i guess the compiler can optimize this away?

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [February 13, 2026, 6:25pm UTC](https://discourse.julialang.org/t/how-to-create-a-dataframe-from-code-literals-especially-with-unitful/135613/6 "2026-02-13T18:25:52Z")

</div>

Yes that’s right, the compiler will optimize away the `NamedTuple` construction, although now that you mention it, it _wont_ optimize away the splitting of the array into an array of `Time` and an array of `Measure` properties, so from that point of view, @JonasWickman’s suggestion is likely more efficient.

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [February 13, 2026, 6:29pm UTC](https://discourse.julialang.org/t/how-to-create-a-dataframe-from-code-literals-especially-with-unitful/135613/7 "2026-02-13T18:29:49Z")

</div>

By the way, your times can also be properly semantic objects with units if you use the `Dates` standard library:

```julia-auto
julia> using Dates, DataFrames, Unitful

julia> df = DataFrame(
           Time = [Time("09:29"), Time("09:30"), Time("09:31")],
           Measure = [1.1u"cm", 1.2u"cm", 1.1u"cm"]
       )
3×2 DataFrame
 Row │ Time Measure   
     │ Time Quantity… 
─────┼─────────────────────
   1 │ 09:29:00 1.1 cm
   2 │ 09:30:00 1.2 cm
   3 │ 09:31:00 1.1 cm

```

and then you can do things like e.g.

```julia-auto
julia> diff(df.Time)
2-element Vector{Nanosecond}:
 60000000000 nanoseconds
 60000000000 nanoseconds

```

---

<div class="post-metadata">

### Author: ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)
#### Post date: [February 13, 2026, 6:30pm UTC](https://discourse.julialang.org/t/how-to-create-a-dataframe-from-code-literals-especially-with-unitful/135613/8 "2026-02-13T18:30:17Z")

</div>

Yes I think the same, anyway thanks!

@JonasWickman probably your solution is the most efficient, but I think keeping it row first helps readability given that I want to check these data from time to time, anyway I’ll keep that in mind, thanks.

---

<div class="post-metadata">

### Author: ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)
#### Post date: [February 13, 2026, 6:35pm UTC](https://discourse.julialang.org/t/how-to-create-a-dataframe-from-code-literals-especially-with-unitful/135613/9 "2026-02-13T18:35:45Z")

</div>

> [@mbauman](#):
>
> Yeah, lots of ways to skin the cat here. You can also avoid repeating the names if you want:
> 
> ```julia-auto
> julia> DataFrame([
> ("09:29", 1.1u"cm"),
> ("09:30", 1.2u"cm"),
> ("09:31", 1.1u"cm")
> ],
> ["Time", "Measure"])
> 3×2 DataFrame
> Row │ Time Measure
> │ String Quantity…
> ─────┼─────────────────────
> 1 │ 09:29 1.1 cm
> 2 │ 09:30 1.2 cm
> 3 │ 09:31 1.1 cm
> 
> ```

Didn’t realized this either, thanks! I guess it’s a matter of wether to keep the schema in the tuples (more robust if you accidentally invert some data), or as an external parameter (that you can share among different DataFrames to make sure they all have a consistent interface). Actually I would have expected to be a constructor that accepts the header name first, and then the data, given that is the order in which you normally see it listed. Possibly i can think of a macro to do provide names, types and data all at once, since i use this style quite a lot
