MCPcopy Create free account
hub / github.com/apache/arrow / index

Function index

python/pyarrow/compute.py:417–464  ·  view source on GitHub ↗

Find the index of the first occurrence of a given value. Parameters ---------- data : Array-like value : Scalar-like object The value to search for. start : int, optional end : int, optional memory_pool : MemoryPool, optional If not passed, will allo

(data, value, start=None, end=None, *, memory_pool=None)

Source from the content-addressed store, hash-verified

415
416
417def index(data, value, start=None, end=None, *, memory_pool=None):
418 """
419 Find the index of the first occurrence of a given value.
420
421 Parameters
422 ----------
423 data : Array-like
424 value : Scalar-like object
425 The value to search for.
426 start : int, optional
427 end : int, optional
428 memory_pool : MemoryPool, optional
429 If not passed, will allocate memory from the default memory pool.
430
431 Returns
432 -------
433 index : int
434 the index, or -1 if not found
435
436 Examples
437 --------
438 >>> import pyarrow as pa
439 >>> import pyarrow.compute as pc
440 >>> arr = pa.array(["Lorem", "ipsum", "dolor", "sit", "Lorem", "ipsum"])
441 >>> pc.index(arr, "ipsum")
442 <pyarrow.Int64Scalar: 1>
443 >>> pc.index(arr, "ipsum", start=2)
444 <pyarrow.Int64Scalar: 5>
445 >>> pc.index(arr, "amet")
446 <pyarrow.Int64Scalar: -1>
447 """
448 if start is not None:
449 if end is not None:
450 data = data.slice(start, end - start)
451 else:
452 data = data.slice(start)
453 elif end is not None:
454 data = data.slice(0, end)
455
456 if not isinstance(value, pa.Scalar):
457 value = pa.scalar(value, type=data.type)
458 elif data.type != value.type:
459 value = pa.scalar(value.as_py(), type=data.type)
460 options = IndexOptions(value=value)
461 result = call_function('index', [data], options, memory_pool)
462 if start is not None and result.as_py() >= 0:
463 result = pa.scalar(result.as_py() + start, type=pa.int64())
464 return result
465
466
467def take(data, indices, *, boundscheck=True, memory_pool=None):

Callers

nothing calls this directly

Calls 3

as_pyMethod · 0.80
sliceMethod · 0.45
scalarMethod · 0.45

Tested by

no test coverage detected