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

Function index

python/pyarrow/compute.py:418–465  ·  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

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