# Weird behaviour when replace the empty string in row with DataFramesMeta.jl

**URL:** <https://discourse.julialang.org/t/weird-behaviour-when-replace-the-empty-string-in-row-with-dataframesmeta-jl/96165>\
**Category:** General Usage\
**Tags:** strings, dataframes, dataframesmeta\
**Created:** [March 16, 2023, 6:09am UTC](https://discourse.julialang.org/t/weird-behaviour-when-replace-the-empty-string-in-row-with-dataframesmeta-jl/96165 "2023-03-16T06:09:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hungpham3112](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hungpham3112/32/34315_2.png) [@hungpham3112](https://discourse.julialang.org/u/hungpham3112)\
**Post date:** [March 16, 2023, 6:09am UTC](https://discourse.julialang.org/t/weird-behaviour-when-replace-the-empty-string-in-row-with-dataframesmeta-jl/96165/1 "2023-03-16T06:09:59Z")

</div>

I have a snippet

```julia
string_number_df2 = DataFrame(
	    A = ["1", "", "3"],
	    B = ["4", "5", ""],
	)	

result = @chain string_number_df2 begin
    @rtransform :B = replace(:B, "" => missing)
end

```

Expectation:

```julia
A | B
"1" | "4"
"" | "5"
"3" | missing

```

but the actual behavior is:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/3/b3374cec09e0da37cbba75520fefcb8ba20c9855.png)

It seems `replace` function parse the `"4"` as `"" * "4" * ""` and then replace the empty string with missing string. I can’t understand why the function works in that way.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [March 16, 2023, 6:24am UTC](https://discourse.julialang.org/t/weird-behaviour-when-replace-the-empty-string-in-row-with-dataframesmeta-jl/96165/2 "2023-03-16T06:24:40Z")

</div>

This is not related to DataFrames.jl. It is how `replace` works:

```julia
julia> replace("I", "" => missing)
"missingImissing"

```

Instead do:

```julia
julia> result = @chain string_number_df2 begin
           @transform :B = replace(:B, "" => missing)
       end
3×2 DataFrame
 Row │ A B
     │ String String?
─────┼─────────────────
   1 │ 1 4
   2 │ 5
   3 │ 3 missing

```

---

<div class="post-metadata">

**Author:** ![hungpham3112](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hungpham3112/32/34315_2.png) [@hungpham3112](https://discourse.julialang.org/u/hungpham3112)\
**Post date:** [March 16, 2023, 6:47am UTC](https://discourse.julialang.org/t/weird-behaviour-when-replace-the-empty-string-in-row-with-dataframesmeta-jl/96165/3 "2023-03-16T06:47:28Z")

</div>

I’m new to Julia, and I’m used to programming in Python where everything is an object. I used to think that I could convert a string object to a missing object, so I was confused when I encountered the string “missing4missing” in Julia.

---

<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:** [March 16, 2023, 7:26am UTC](https://discourse.julialang.org/t/weird-behaviour-when-replace-the-empty-string-in-row-with-dataframesmeta-jl/96165/4 "2023-03-16T07:26:51Z")

</div>

If you use `replace` on a collection of strings instead (the entire dataframe column), then the result is what you expected:

```julia
using DataFrames
df = DataFrame(A = ["1", "", "3"], B = ["4", "5", ""])
df.B = replace(df.B, "" => missing)
df

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [March 16, 2023, 8:20am UTC](https://discourse.julialang.org/t/weird-behaviour-when-replace-the-empty-string-in-row-with-dataframesmeta-jl/96165/5 "2023-03-16T08:20:28Z")

</div>

> [@hungpham3112](#):
>
> everything is an object

It is the same with Julia. Just you need to notice that `replace` has two signatures (quoting its docstring):

```julia
  replace(A, old_new::Pair...; [count::Integer])

Return a copy of collection A where, for each pair old=>new in old_new,
all occurrences of old are replaced by new. Equality is
determined using isequal.
If count is specified, then replace at most count occurrences in total.

```

and

```julia
 replace(s::AbstractString, pat=>r, [pat2=>r2, ...]; [count::Integer])

  Search for the given pattern pat in s, and replace each occurrence with r.
If count is provided, replace at most count occurrences.

```

You wanted to call the first (which works on collections like a vector), but you used `@rtransform` which applied replace to each individual string instead of the collection as a whole (the `r` letter in front of `transform` signaled this choice).
