# Displaying unitful dataframes

**URL:** https://discourse.julialang.org/t/displaying-unitful-dataframes/69331
**Category:** General Usage
**Tags:** dataframes, tables, io, unitful, prettytables
**Created:** [October 6, 2021, 7:47pm UTC](https://discourse.julialang.org/t/displaying-unitful-dataframes/69331 "2021-10-06T19:47:15Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 6, 2021, 7:47pm UTC](https://discourse.julialang.org/t/displaying-unitful-dataframes/69331/1 "2021-10-06T19:47:15Z")

</div>

What is the recommended procedure for displaying Unitful Dataframes?  
Example:

```julia
using DataFrames, Unitful
d = (0:10:1000)u"m"
v = 1500u"m/s" .+ 1.5u"1/s"*d
t = [0u"s"; cumsum(diff(d) ./ v[1:end-1])]
df = DataFrame(depth=d, velocity=v, time=t)

```

`df` displays like this:

```julia
 Row │ depth velocity time
     │ Quantity… Quantity… Quantity…    
─────┼────────────────────────────────────────
   1 │ 0 m 1500.0 m s^-1 0.0 s
   2 │ 10 m 1515.0 m s^-1 0.00666667 s
   3 │ 20 m 1530.0 m s^-1 0.0132673 s
  ⋮ │ ⋮ ⋮ ⋮
  99 │ 980 m 2970.0 m s^-1 0.457052 s
 100 │ 990 m 2985.0 m s^-1 0.460419 s
 101 │ 1000 m 3000.0 m s^-1 0.463769 s

```

Ideally, the units should appear clearly and only below the column names. Thank you.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [October 6, 2021, 9:18pm UTC](https://discourse.julialang.org/t/displaying-unitful-dataframes/69331/2 "2021-10-06T21:18:26Z")

</div>

I don’t know if someone has made this, but it definitely sounds like a good PR for PrettyTables, or a UnitfulDataFrames package, depending on the former’s policy on dependencies (Add it to the list of packages that would be obsoleted with [https://github.com/JuliaLang/Pkg.jl/issues/1285](https://github.com/JuliaLang/Pkg.jl/issues/1285))

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 7, 2021, 7:34am UTC](https://discourse.julialang.org/t/displaying-unitful-dataframes/69331/3 "2021-10-07T07:34:07Z")

</div>

Thank you for the feedback.

It seems that [PrettyTables.jl](https://ronisbr.github.io/PrettyTables.jl) can already be used _as is_ but one needs to labor a bit and precondition the inputs as follows:

```julia
using DataFrames, Unitful, PrettyTables

d = (0:10:1000)u"m"
v = 1500u"m/s" .+ 1.5u"1/s"*d
t = [0u"s"; cumsum(diff(d) ./ v[1:end-1])]
df = DataFrame(depth=d, velocity=v, time=t)
sunits = [unit(d[1]), unit(v[1]), unit(t[1])]
header = permutedims([names(df) sunits])
pretty_table(ustrip.(df), header; formatters = ft_printf("%.4f",[3]))

```

Result:

```julia
┌───────┬──────────┬────────┐
│ depth │ velocity │ time │
│ m │ m s^-1 │ s │
├───────┼──────────┼────────┤
│ 0 │ 1500.0 │ 0.0000 │
│ 10 │ 1515.0 │ 0.0067 │
│ 20 │ 1530.0 │ 0.0133 │
│ 30 │ 1545.0 │ 0.0198 │
│ 40 │ 1560.0 │ 0.0263 │
│ 50 │ 1575.0 │ 0.0327 │
│ 60 │ 1590.0 │ 0.0390 │
│ ⋮ │ ⋮ │ ⋮ │
└───────┴──────────┴────────┘
              94 rows omitted

```

_ **NB:** use keyword argument_ `crop=:none` _to show all_

Copying @Ronis_BR to ask if this could be automated.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [October 7, 2021, 8:12am UTC](https://discourse.julialang.org/t/displaying-unitful-dataframes/69331/4 "2021-10-07T08:12:21Z")

</div>

Single-argument `ustrip` is a bit dangerous. If this is automated, it should probably use `ustrip.(units, quantities)`
