← Back to tsb playground
infer_objects / convert_dtypes
pandas equivalent:
Series.infer_objects() /
DataFrame.infer_objects() /
Series.convert_dtypes() /
DataFrame.convert_dtypes()
What it does
These utilities refine dtypes automatically — useful after reading data from
CSV/JSON where everything starts as object or string:
inferObjectsSeries — promotes an object-typed Series to a
more specific dtype (int, float, bool, string) when all values have a consistent type.
inferObjectsDataFrame — applies per-column inference to every column.
convertDtypesSeries — like inferObjectsSeries but also
parses string columns as numbers when possible.
convertDtypesDataFrame — per-column convertDtypesSeries.
inferObjectsSeries — promote object → typed
import { Series, Dtype, inferObjectsSeries } from "tsb";
// Object series holding integers
const s = new Series({ data: [1, 2, 3], dtype: Dtype.object });
s.dtype.kind; // "object"
const better = inferObjectsSeries(s);
better.dtype.kind; // "int"
better.values; // [1, 2, 3]
// Mixed types — cannot infer, returns original
const mixed = new Series({ data: [1, "a", true], dtype: Dtype.object });
inferObjectsSeries(mixed).dtype.kind; // "object"
// All null — no inference possible
const nulls = new Series({ data: [null, null], dtype: Dtype.object });
inferObjectsSeries(nulls).dtype.kind; // "object"
inferObjectsDataFrame — all columns at once
import { DataFrame, inferObjectsDataFrame } from "tsb";
const df = DataFrame.fromColumns({
ints: [1, 2, 3],
floats: [1.1, 2.2, 3.3],
strs: ["a", "b", "c"],
bools: [true, false, true],
});
const inferred = inferObjectsDataFrame(df);
inferred.col("ints").dtype.kind; // "int"
inferred.col("floats").dtype.kind; // "float"
inferred.col("strs").dtype.kind; // "string"
inferred.col("bools").dtype.kind; // "bool"
convertDtypesSeries — also parses numeric strings
import { Series, convertDtypesSeries } from "tsb";
// String values that look like integers
const ints = new Series({ data: ["1", "2", "3"] });
const result = convertDtypesSeries(ints);
result.dtype.kind; // "int"
result.values; // [1, 2, 3]
// String values that look like floats
const floats = new Series({ data: ["1.5", "2.5", "3.5"] });
convertDtypesSeries(floats).dtype.kind; // "float"
// Non-numeric strings: unchanged
const text = new Series({ data: ["apple", "banana"] });
convertDtypesSeries(text); // same Series, dtype "string"
// Int series with nulls → can convert to float for NA safety
import { Dtype } from "tsb";
const withNull = new Series({ data: [1, null, 3], dtype: Dtype.int64 });
convertDtypesSeries(withNull, { convertIntegerToFloat: true }).dtype.kind;
// "float" (null becomes NaN-compatible)
convertDtypesDataFrame — per-column conversion
import { DataFrame, convertDtypesDataFrame } from "tsb";
// After reading a CSV, all columns come back as strings:
const raw = DataFrame.fromColumns({
age: ["25", "30", "22"],
score: ["88.5", "92.1", "78.0"],
name: ["Alice", "Bob", "Charlie"],
});
const typed = convertDtypesDataFrame(raw);
typed.col("age").dtype.kind; // "int"
typed.col("score").dtype.kind; // "float"
typed.col("name").dtype.kind; // "string" (unchanged — not numeric)
API reference
| Function | Description |
inferObjectsSeries(s, options?) | Infer better dtype for object-typed Series |
inferObjectsDataFrame(df, options?) | Infer better dtypes for all columns |
convertDtypesSeries(s, options?) | Convert to best dtype, including string→number parsing |
convertDtypesDataFrame(df, options?) | Per-column convertDtypesSeries |
InferObjectsOptions
| Option | Type | Default | Description |
objectOnly | boolean | true | Only infer for object-dtype Series (mirrors pandas default) |
ConvertDtypesOptions
| Option | Type | Default | Description |
convertString | boolean | true | Parse string values as numbers when possible |
convertIntegerToFloat | boolean | false | Convert int series with nulls to float |
When to use which
| Use case | Function |
| Promote object columns after creation | inferObjectsSeries / DataFrame |
| Parse CSV/JSON string columns to numbers | convertDtypesSeries / DataFrame |
| Make int columns nullable (float) | convertDtypesSeries(s, { convertIntegerToFloat: true }) |