# How can I assing an Unitful.jl quantity in an idempotent way?

**URL:** https://discourse.julialang.org/t/how-can-i-assing-an-unitful-jl-quantity-in-an-idempotent-way/95864
**Category:** General Usage
**Tags:** physics, unitful
**Created:** [March 10, 2023, 12:28pm UTC](https://discourse.julialang.org/t/how-can-i-assing-an-unitful-jl-quantity-in-an-idempotent-way/95864 "2023-03-10T12:28:14Z")
**Posts on this page:** 5
**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: [March 10, 2023, 12:28pm UTC](https://discourse.julialang.org/t/how-can-i-assing-an-unitful-jl-quantity-in-an-idempotent-way/95864/1 "2023-03-10T12:28:14Z")

</div>

Hi all!

So I need to manipulate some physical quantities. `Unitful.jl` seems to do a very good job at it, but IIUC the idiomatic way to “build” a physical quantity is to multiply the number and the unit:  
`12 * u"m"`.

The point is that I need to attach a quantity from a DataFrame I read from a CSV:

```julia
f = open(model_single_instruction_file)
df_model_master = DataFrame(CSV.File(f));
close(f)

df_model_master[!,mean_power_sym] = df_model_master[:,mean_power_sym] .* u"W";
df_model_master[!, std_power_sym] = df_model_master[:, std_power_sym] .* u"W";

```

But here’s the thing: if, for whatever reason, the second chunk of code gets reevaluated, the unit becomes `W^2`, then `W^3` and so on. **Is there a way to make this idempotent?** I thought for instance to directly build the unit calling `Unitful.Quantity()`, but I don’t know how to do it. May this be an idea?

Thanks!

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [March 10, 2023, 1:54pm UTC](https://discourse.julialang.org/t/how-can-i-assing-an-unitful-jl-quantity-in-an-idempotent-way/95864/2 "2023-03-10T13:54:55Z")

</div>

Hello!

Could the question be rephrased into “how can the organization of the code be modified to avoid evaluating the same stateful code more than once?”?

The problem here is that you are mutating a state (the DataFrame) and you do it repeatedly, likely because of how your code is organized (in a Jupyter notebook perhaps?).

Could the code instead be written in a more functional manner? Or could you introduce a check if the unit is already there, and if so, don’t apply it again?

Otherwise, the multiplicative factor you need is

```julia
u"m" / (unit(column))

```

this should be safe to multiply with no matter if the unit is already there or not:

```julia
julia> (1hr)/unit(1hr) # if the unit is already there, tihs factor is 1
1

julia> unit(1)

julia> (1hr)/unit(1) # if the unit is not there, the factor is 1hr
1 hr

```

---

<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: [March 10, 2023, 4:33pm UTC](https://discourse.julialang.org/t/how-can-i-assing-an-unitful-jl-quantity-in-an-idempotent-way/95864/3 "2023-03-10T16:33:15Z")

</div>

> [@baggepinnen](#):
>
> Otherwise, the multiplicative factor you need is
> 
> ```julia
> u"m" / (unit(column))
> 
> ```

Nice trick, thanks! Anyway, you’re right in saying that I need the code to be evaluated only once. I have been advised to use modules to do that but I’m struggling to grasp how to use them in a local project. I don’t want to go too OT, but for instance having declared a module locally, the only way I can use it is first to `include`ing it, then `using` it. This however produces some error (like defining the same variables twice). Anyways, for now they should prevent reevaluation, but this example is useful nevertheless. Thanks! 👍

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [March 10, 2023, 4:59pm UTC](https://discourse.julialang.org/t/how-can-i-assing-an-unitful-jl-quantity-in-an-idempotent-way/95864/4 "2023-03-10T16:59:57Z")

</div>

While using Unitful, it may also be useful to know about `ustrip`. Here is an example related to the OP:

```julia
julia> ustrip(1u"m")
1

julia> v = [12.3u"W", 4.56u"W"]
2-element Vector{Quantity{Float64, 𝐋^2 𝐌 𝐓^-3, Unitful.FreeUnits{(W,), 𝐋^2 𝐌 𝐓^-3, nothing}}}:
 12.3 W
 4.56 W

julia> v = ustrip.(v)*u"W"
2-element Vector{Quantity{Float64, 𝐋^2 𝐌 𝐓^-3, Unitful.FreeUnits{(W,), 𝐋^2 𝐌 𝐓^-3, nothing}}}:
 12.3 W
 4.56 W

julia> v = v*u"W"
2-element Vector{Quantity{Float64, 𝐋^4 𝐌^2 𝐓^-6, Unitful.FreeUnits{(W^2,), 𝐋^4 𝐌^2 𝐓^-6, nothing}}}:
 12.3 W^2
 4.56 W^2

```

---

<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: [March 10, 2023, 5:11pm UTC](https://discourse.julialang.org/t/how-can-i-assing-an-unitful-jl-quantity-in-an-idempotent-way/95864/5 "2023-03-10T17:11:04Z")

</div>

This too, thanks! It is even more concise.
