DataType#
The DataType class represents the data type of a column in a DataFrame. DataType objects are returned by the DataFrame::dtypes() method.
Overview#
Polars supports various data types for efficient data storage and operations:
Type |
Description |
|---|---|
|
True/false values |
|
Signed integers |
|
Unsigned integers |
|
Floating point numbers |
|
UTF-8 encoded strings |
|
Null/missing values |
Constructor#
DataType objects are typically not constructed directly. They are returned by DataFrame methods.
Usage#
use Polars\DataFrame;
$df = new DataFrame([
'name' => ['Alice', 'Bob'],
'age' => [25, 30],
'score' => [95.5, 87.3],
'active' => [true, false]
]);
$types = $df->dtypes();
// Returns array of DataType objects for each column
Type Inference#
When creating a DataFrame from a PHP array, Polars automatically infers the data type based on the values:
PHP Type |
Polars Type |
|---|---|
|
Boolean |
|
Int64 |
|
Float64 |
|
String |
|
Null (or nullable variant) |
Example:
$df = new DataFrame([
'integers' => [1, 2, 3], // Int64
'floats' => [1.5, 2.5, 3.5], // Float64
'strings' => ['a', 'b', 'c'], // String
'booleans' => [true, false, true], // Boolean
'nullable' => [1, null, 3], // Int64 (nullable)
]);