Change Datatype of Column Entries - Maple Help

DataFrame/SubsDatatype

change the datatype for a column in a DataFrame

 Calling Sequence SubsDatatype( DF, index, newdatatype, options )

Parameters

 DF - a DataFrame object index - name, string or integer value; specifies the column index for the chosen column newdatatype - type; the new datatype for the given column

Options

 • conversion : procedure; specifies a procedure to be mapped onto the elements in the given column. This option is entered in the form conversion = procedure.

Description

 • The SubsDatatype command changes the datatype of the entries in a given column of a DataFrame as well as the indicated datatype of the column.
 • Internally, the DataFrame/SubsDatatype command uses the DataSeries/SubsDatatype command to change the datatype.
 • If the conversion option is given, then the values in the DataSeries are converted by this conversion procedure. Otherwise, they are typically not modified, but there are exceptions. Internally, a new Array is constructed with the given datatype, and the current values are used to initialize this Array; this is the step where an automatic conversion could take place.

Examples

Consider some data on berries.

 > $\mathrm{genus}≔⟨"Rubus","Vitis","Fragaria"⟩:$
 > $\mathrm{energy}≔⟨220,288,136⟩:$
 > $\mathrm{carbohydrates}≔⟨11.94,18.1,7.68⟩:$
 > $\mathrm{top_producer}≔⟨\mathrm{Russia},\mathrm{China},\mathrm{USA}⟩:$
 > $\mathrm{berries}≔\mathrm{DataFrame}\left(⟨\mathrm{energy}|\mathrm{carbohydrates}|\mathrm{top_producer}|\mathrm{genus}⟩,\mathrm{columns}=\left[\mathrm{Energy},\mathrm{Carbohydrates},\mathrm{Top Producer},\mathrm{Genus}\right],\mathrm{rows}=\left[\mathrm{Raspberry},\mathrm{Grape},\mathrm{Strawberry}\right],\mathrm{datatypes}=\left[\mathrm{integer},\mathrm{float},\mathrm{anything},\mathrm{string}\right]\right)$
 ${\mathrm{berries}}{≔}\left[\begin{array}{ccccc}{}& {\mathrm{Energy}}& {\mathrm{Carbohydrates}}& {\mathrm{Top Producer}}& {\mathrm{Genus}}\\ {\mathrm{Raspberry}}& {220}& {11.9400000000000}& {\mathrm{Russia}}& {"Rubus"}\\ {\mathrm{Grape}}& {288}& {18.1000000000000}& {\mathrm{China}}& {"Vitis"}\\ {\mathrm{Strawberry}}& {136}& {7.68000000000000}& {\mathrm{USA}}& {"Fragaria"}\end{array}\right]$ (1)

You can get the data types for the columns with the Datatypes command.

 > $\mathrm{Datatypes}\left(\mathrm{berries}\right)$
 $\left[{\mathrm{integer}}{,}{{\mathrm{float}}}_{{8}}{,}{\mathrm{anything}}{,}{\mathrm{string}}\right]$ (2)

You can change the datatype of the Energy column to float:

 > $\mathrm{SubsDatatype}\left(\mathrm{berries},\mathrm{Energy},\mathrm{float}\right)$
 $\left[\begin{array}{ccccc}{}& {\mathrm{Energy}}& {\mathrm{Carbohydrates}}& {\mathrm{Top Producer}}& {\mathrm{Genus}}\\ {\mathrm{Raspberry}}& {220.}& {11.9400000000000}& {\mathrm{Russia}}& {"Rubus"}\\ {\mathrm{Grape}}& {288.}& {18.1000000000000}& {\mathrm{China}}& {"Vitis"}\\ {\mathrm{Strawberry}}& {136.}& {7.68000000000000}& {\mathrm{USA}}& {"Fragaria"}\end{array}\right]$ (3)

This does not change the datatype in place. To permanently change the datatype:

 > $\mathrm{berries}≔\mathrm{SubsDatatype}\left(\mathrm{berries},\mathrm{Energy},\mathrm{float}\right)$
 ${\mathrm{berries}}{≔}\left[\begin{array}{ccccc}{}& {\mathrm{Energy}}& {\mathrm{Carbohydrates}}& {\mathrm{Top Producer}}& {\mathrm{Genus}}\\ {\mathrm{Raspberry}}& {220.}& {11.9400000000000}& {\mathrm{Russia}}& {"Rubus"}\\ {\mathrm{Grape}}& {288.}& {18.1000000000000}& {\mathrm{China}}& {"Vitis"}\\ {\mathrm{Strawberry}}& {136.}& {7.68000000000000}& {\mathrm{USA}}& {"Fragaria"}\end{array}\right]$ (4)
 > $\mathrm{Datatype}\left(\mathrm{berries}\left[\mathrm{Energy}\right]\right)$
 ${{\mathrm{float}}}_{{8}}$ (5)

When working with strings or name conversions, it may be necessary to supply an explicit conversion for values in the column of the data frame:

 > $\mathrm{berries}≔\mathrm{SubsDatatype}\left(\mathrm{berries},\mathrm{Genus},\mathrm{name},\mathrm{=}\left(\mathrm{conversion},x↦\mathrm{convert}\left(x,\mathrm{name}\right)\right)\right)$
 ${\mathrm{berries}}{≔}\left[\begin{array}{ccccc}{}& {\mathrm{Energy}}& {\mathrm{Carbohydrates}}& {\mathrm{Top Producer}}& {\mathrm{Genus}}\\ {\mathrm{Raspberry}}& {220.}& {11.9400000000000}& {\mathrm{Russia}}& {\mathrm{Rubus}}\\ {\mathrm{Grape}}& {288.}& {18.1000000000000}& {\mathrm{China}}& {\mathrm{Vitis}}\\ {\mathrm{Strawberry}}& {136.}& {7.68000000000000}& {\mathrm{USA}}& {\mathrm{Fragaria}}\end{array}\right]$ (6)
 > $\mathrm{berries}≔\mathrm{SubsDatatype}\left(\mathrm{berries},\mathrm{Top Producer},\mathrm{string},\mathrm{=}\left(\mathrm{conversion},x↦\mathrm{convert}\left(x,\mathrm{string}\right)\right)\right)$
 ${\mathrm{berries}}{≔}\left[\begin{array}{ccccc}{}& {\mathrm{Energy}}& {\mathrm{Carbohydrates}}& {\mathrm{Top Producer}}& {\mathrm{Genus}}\\ {\mathrm{Raspberry}}& {220.}& {11.9400000000000}& {"Russia"}& {\mathrm{Rubus}}\\ {\mathrm{Grape}}& {288.}& {18.1000000000000}& {"China"}& {\mathrm{Vitis}}\\ {\mathrm{Strawberry}}& {136.}& {7.68000000000000}& {"USA"}& {\mathrm{Fragaria}}\end{array}\right]$ (7)
 > $\mathrm{Datatypes}\left(\mathrm{berries}\right)$
 $\left[{{\mathrm{float}}}_{{8}}{,}{{\mathrm{float}}}_{{8}}{,}{\mathrm{string}}{,}{\mathrm{name}}\right]$ (8)

Compatibility

 • The DataFrame/SubsDatatype command was introduced in Maple 2017.