Cast array values to another data type. Can also be invoked as an array instance method. Parameters ---------- arr : Array-like target_type : DataType or str Type to cast to safe : bool, default True Check for overflows or other unsafe conversions op
(arr, target_type=None, safe=None, options=None, memory_pool=None)
| 347 | |
| 348 | |
| 349 | def cast(arr, target_type=None, safe=None, options=None, memory_pool=None): |
| 350 | """ |
| 351 | Cast array values to another data type. Can also be invoked as an array |
| 352 | instance method. |
| 353 | |
| 354 | Parameters |
| 355 | ---------- |
| 356 | arr : Array-like |
| 357 | target_type : DataType or str |
| 358 | Type to cast to |
| 359 | safe : bool, default True |
| 360 | Check for overflows or other unsafe conversions |
| 361 | options : CastOptions, default None |
| 362 | Additional checks pass by CastOptions |
| 363 | memory_pool : MemoryPool, optional |
| 364 | memory pool to use for allocations during function execution. |
| 365 | |
| 366 | Examples |
| 367 | -------- |
| 368 | >>> from datetime import datetime |
| 369 | >>> import pyarrow as pa |
| 370 | >>> arr = pa.array([datetime(2010, 1, 1), datetime(2015, 1, 1)]) |
| 371 | >>> arr.type |
| 372 | TimestampType(timestamp[us]) |
| 373 | |
| 374 | You can use ``pyarrow.DataType`` objects to specify the target type: |
| 375 | |
| 376 | >>> cast(arr, pa.timestamp('ms')) |
| 377 | <pyarrow.lib.TimestampArray object at ...> |
| 378 | [ |
| 379 | 2010-01-01 00:00:00.000, |
| 380 | 2015-01-01 00:00:00.000 |
| 381 | ] |
| 382 | |
| 383 | >>> cast(arr, pa.timestamp('ms')).type |
| 384 | TimestampType(timestamp[ms]) |
| 385 | |
| 386 | Alternatively, it is also supported to use the string aliases for these |
| 387 | types: |
| 388 | |
| 389 | >>> arr.cast('timestamp[ms]') |
| 390 | <pyarrow.lib.TimestampArray object at ...> |
| 391 | [ |
| 392 | 2010-01-01 00:00:00.000, |
| 393 | 2015-01-01 00:00:00.000 |
| 394 | ] |
| 395 | >>> arr.cast('timestamp[ms]').type |
| 396 | TimestampType(timestamp[ms]) |
| 397 | |
| 398 | Returns |
| 399 | ------- |
| 400 | casted : Array |
| 401 | The cast result as a new Array |
| 402 | """ |
| 403 | safe_vars_passed = (safe is not None) or (target_type is not None) |
| 404 | |
| 405 | if safe_vars_passed and (options is not None): |
| 406 | raise ValueError("Must either pass values for 'target_type' and 'safe'" |
nothing calls this directly
no outgoing calls
no test coverage detected