pyarrow.compute.cast#
- pyarrow.compute.cast(arr,target_type=None,safe=None,options=None,memory_pool=None)[source]#
Cast array values to another data type. Can also be invoked as an arrayinstance method.
- Parameters:
- arrArray-like
- target_type
DataTypeorstr Type to cast to
- safebool, default
True Check for overflows or other unsafe conversions
- options
CastOptions, defaultNone Additional checks pass by CastOptions
- memory_pool
MemoryPool, optional memory pool to use for allocations during function execution.
- Returns:
- casted
Array The cast result as a new Array
- casted
Examples
>>>fromdatetimeimportdatetime>>>importpyarrowaspa>>>arr=pa.array([datetime(2010,1,1),datetime(2015,1,1)])>>>arr.typeTimestampType(timestamp[us])
You can use
pyarrow.DataTypeobjects to specify the target type:>>>cast(arr,pa.timestamp('ms'))<pyarrow.lib.TimestampArray object at ...>[ 2010-01-01 00:00:00.000, 2015-01-01 00:00:00.000]
>>>cast(arr,pa.timestamp('ms')).typeTimestampType(timestamp[ms])
Alternatively, it is also supported to use the string aliases for thesetypes:
>>>arr.cast('timestamp[ms]')<pyarrow.lib.TimestampArray object at ...>[ 2010-01-01 00:00:00.000, 2015-01-01 00:00:00.000]>>>arr.cast('timestamp[ms]').typeTimestampType(timestamp[ms])
On this page

