CastColumns

CastColumns[tab,{col1type1,}]

changes the type of coli to typei in the Tabular object tab.

Details

  • CastColumns is typically used to convert the types of the columns of a Tabular object, for example, converting strings into numbers or dates.
  • The types typei refer to the type of the elements of coli represented as a TabularColumn. Element types include:
  • "MachineInteger"machine-sized integers
    "MachineReal"machine-sized reals
    "Boolean"Boolean (True or False)
    "ByteArray"ByteArray elements representing binary data
    "Categorical"::[categories]encoded by categories
    "CategoricalOrdered"::[categories]ordinal categories (with order)
    "Date"::[]calendar date of any granularity (day, month, μs, etc.)
    "Entity"::[]entity of a given domain
    "InertExpression"general expressions that do not get evaluated until used
    "ListVector"::[type]list of elements of the given type
    "ListVector"::[type,n]list length n of elements of the given type
    "ListTuple"::[type1,type2,]list of elements of types type1, type2,
    "Null"Missing expression
    "Quantity"::[type,unit]quantities of type magnitude and common unit
    "String"strings
    "Time"::[]time of day, as in TimeObject
  • Numerical element types can be given more specifically and include:
  • "Integer8"signed 8-bit integers from through 127
    "UnsignedInteger8"integers 0 through 255
    "Integer16"signed 16-bit integers from through
    "UnsignedInteger16"integers 0 through 65535
    "Integer32"signed 32-bit integers from through
    "UnsignedInteger32"integers 0 through
    "Integer64"signed 64-bit integers from through
    "UnsignedInteger64"integers 0 through
    "Real32"single-precision real (32-bit)
    "Real64"double-precision real (64-bit)
    "ComplexReal32"single-precision complex
    "ComplexReal64"double-precision complex

Examples

open allclose all

Basic Examples  (1)

Change numeric column type to another numeric column type:

Cast the "Integer64" column to "Real64":

Scope  (4)

Take blood sugar measurements on a list of days:

Cast the "date" column to "Date" type:

Take a Tabular object where numeric data has been recorded as strings:

Cast the "val" column to a numeric type:

Change the vector type of a column in a Tabular object:

Cast several columns to the same type simultaneously:

Applications  (2)

Sales Data  (2)

Get retail data from "tsv" format into a Tabular object:

All the dates came as strings:

Recast "Date" into a "Date" format:

Select the data for a given city:

Create a TimeSeries object with "Date" used as the "TimeColumn":

Visualize the sales time series:

Take a large dataset of US COVID data:

Show only the first five rows:

The FIPS code is a number uniquely identifying a US county, but "fips" column has string type:

Efficiently recast the "fips" column into integers:

Properties & Relations  (4)

Use CastColumns[tab,typerules] to change the types of the columns of a Tabular object tab:

The same result can be achieved using Tabular[tab,<|"ElementType"typerules|>]:

Or using ToTabular[tab,"Rows",<|"ElementType"typerules|>]:

Take a Tabular object with string columns:

Instead of using CastColumns, a new schema can be constructed with the new column types:

Impose the schema on the Tabular object:

The action of CastColumns can be reproduced using TransformColumns with appropriate functions:

String column keys containing spaces require the use of explicit quotation marks:

Take a Tabular object with a column of real-valued levels of blood sugar:

CastColumns effectively uses the Automatic conversion method for each column:

Such conversion fails in this case because reals cannot always be faithfully projected into integers:

Use TabularColumn with an explicit conversion method that includes rounding of reals when converting to integers:

Use InsertColumns to reinsert the new column:

Wolfram Research (2025), CastColumns, Wolfram Language function, https://reference.wolfram.com/language/ref/CastColumns.html.

Text

Wolfram Research (2025), CastColumns, Wolfram Language function, https://reference.wolfram.com/language/ref/CastColumns.html.

CMS

Wolfram Language. 2025. "CastColumns." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/CastColumns.html.

APA

Wolfram Language. (2025). CastColumns. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/CastColumns.html

BibTeX

@misc{reference.wolfram_2024_castcolumns, author="Wolfram Research", title="{CastColumns}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/CastColumns.html}", note=[Accessed: 15-January-2025 ]}

BibLaTeX

@online{reference.wolfram_2024_castcolumns, organization={Wolfram Research}, title={CastColumns}, year={2025}, url={https://reference.wolfram.com/language/ref/CastColumns.html}, note=[Accessed: 15-January-2025 ]}