turicreate.SArray.astype

SArray.astype(dtype, undefined_on_failure=False)

Create a new SArray with all values cast to the given type. Throws an exception if the types are not castable to the given type.

Parameters:
dtype : {int, float, str, list, array.array, dict, datetime.datetime}

The type to cast the elements to in SArray

undefined_on_failure: bool, optional

If set to True, runtime cast failures will be emitted as missing values rather than failing.

Returns:
out : SArray [dtype]

The SArray converted to the type dtype.

Notes

  • The string parsing techniques used to handle conversion to dictionary and list types are quite generic and permit a variety of interesting formats to be interpreted. For instance, a JSON string can usually be interpreted as a list or a dictionary type. See the examples below.
  • For datetime-to-string and string-to-datetime conversions, use sa.datetime_to_str() and sa.str_to_datetime() functions.
  • For array.array to turicreate.Image conversions, use sa.pixel_array_to_image()

Examples

>>> sa = turicreate.SArray(['1','2','3','4'])
>>> sa.astype(int)
dtype: int
Rows: 4
[1, 2, 3, 4]

Given an SArray of strings that look like dicts, convert to a dictionary type:

>>> sa = turicreate.SArray(['{1:2 3:4}', '{a:b c:d}'])
>>> sa.astype(dict)
dtype: dict
Rows: 2
[{1: 2, 3: 4}, {'a': 'b', 'c': 'd'}]