Lowercase dataframe column strings with missing values

I have a Data Frame with missing values on it.

using DataFrames

df = DataFrame(
    Y = ["A", "B", "C", "D", "E", "F"],
    Z = ["A", missing, "C", missing, "E", missing]
)

When I tried to transform to lowercase I got a MethodError: no method matching lowercase(::Missing) message, it doesn’t work because of the missing values:

transform(df, :Y => ByRow(x-> lowercase(x)) => :Y)

So I’m using:

broadcast(v -> ismissing(v) ? missing : lowercase(v), df[!, :Z]);
  • Is there any other simple option to transform a column to lowercase keeping the missing values as it is, something like df.Z.str.lower() and tolower(df$Z)?
  • Is it possible to use @pipe with the broadcast function above?

use the passmissing wrapper:

julia> transform(df, :Y => ByRow(passmissing(lowercase)) => :Y)
6Γ—2 DataFrame
 Row β”‚ Y       Z
     β”‚ String  String?
─────┼─────────────────
   1 β”‚ a       A
   2 β”‚ b       missing
   3 β”‚ c       C
   4 β”‚ d       missing
   5 β”‚ e       E
   6 β”‚ f       missing
2 Likes