Module: Polars::Convert

Included in:
Polars
Defined in:
lib/polars/convert.rb

Instance Method Summary collapse

Instance Method Details

#from_hash(data, schema: nil, columns: nil) ⇒ DataFrame

Construct a DataFrame from a hash of arrays.

This operation clones data, unless you pass in a Hash<String, Series>.

Examples:

data = {"a" => [1, 2], "b" => [3, 4]}
Polars.from_hash(data)
# =>
# shape: (2, 2)
# ┌─────┬─────┐
# │ a   ┆ b   │
# │ --- ┆ --- │
# │ i64 ┆ i64 │
# ╞═════╪═════╡
# │ 1   ┆ 3   │
# │ 2   ┆ 4   │
# └─────┴─────┘

Parameters:

  • data (Hash)

    Two-dimensional data represented as a hash. Hash must contain arrays.

  • schema (Object) (defaults to: nil)

    The DataFrame schema may be declared in several ways:

    • As a hash of \{name:type} pairs; if type is nil, it will be auto-inferred.
    • As an array of column names; in this case types are automatically inferred.
    • As an array of [name,type] pairs; this is equivalent to the hash form.

    If you supply an array of column names that does not match the names in the underlying data, the names given here will overwrite them. The number of names given in the schema should match the underlying data dimensions.

  • columns (Array) (defaults to: nil)

    Column labels to use for resulting DataFrame. If specified, overrides any labels already present in the data. Must match data dimensions.

Returns:



39
40
41
42
43
44
45
46
# File 'lib/polars/convert.rb', line 39

def from_hash(data, schema: nil, columns: nil)
  Utils.wrap_df(
    DataFrame.hash_to_rbdf(
      data,
      schema: schema || columns
    )
  )
end