Method: Polars::StringExpr#split

Defined in:
lib/polars/string_expr.rb

#split(by, inclusive: false, literal: true, strict: true) ⇒ Expr

Split the string by a substring.

Examples:

df = Polars::DataFrame.new({"s" => ["foo bar", "foo-bar", "foo bar baz"]})
df.select(Polars.col("s").str.split(" "))
# =>
# shape: (3, 1)
# ┌───────────────────────┐
# │ s                     │
# │ ---                   │
# │ list[str]             │
# ╞═══════════════════════╡
# │ ["foo", "bar"]        │
# │ ["foo-bar"]           │
# │ ["foo", "bar", "baz"] │
# └───────────────────────┘

Parameters:

  • by (String)

    Substring to split by.

  • inclusive (Boolean) (defaults to: false)

    If true, include the split character/string in the results.

  • literal (Boolean) (defaults to: true)

    Treat by as a literal string, not as a regular expression.

  • strict (Boolean) (defaults to: true)

    Raise an error if the underlying pattern is not a valid regex, otherwise mask out with a null value.

Returns:



1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
# File 'lib/polars/string_expr.rb', line 1253

def split(by, inclusive: false, literal: true, strict: true)
  by_rbexpr = Utils.parse_into_expression(by, str_as_lit: true)

  if !literal
    if inclusive
      return Utils.wrap_expr(
        _rbexpr.str_split_regex_inclusive(by_rbexpr, strict)
      )
    end
    return Utils.wrap_expr(_rbexpr.str_split_regex(by_rbexpr, strict))
  end

  if inclusive
    return Utils.wrap_expr(_rbexpr.str_split_inclusive(by_rbexpr))
  end
  Utils.wrap_expr(_rbexpr.str_split(by_rbexpr))
end