Skip to content

List Functions

Functions for slicing, filtering, mapping, sorting, modifying, and aggregating arrays.

from everysk.sdk.engines import Expression
expression_engine = Expression()


Slicing & Indexing

slice

slice(arr, start=None, end=None, step=None)

Returns a portion of arr between start and end (exclusive). Omit either bound to slice from the beginning or to the end. step controls element stride.

import numpy as np

expression_engine.solve('slice(arr, 1, 4)', {'arr': np.array([10, 20, 30, 40, 50])})
# [20, 30, 40]

expression_engine.solve('slice(arr, 0, 5, 2)', {'arr': np.array([10, 20, 30, 40, 50])})
# [10, 30, 50]

at

at(arr, index_row, index_col=None)

Returns the element at index_row. Returns None if the index is out of bounds.

import numpy as np

expression_engine.solve('at(arr, 2)', {'arr': np.array([10, 20, 30, 40])})
# 30

expression_engine.solve('at(arr, -1)', {'arr': np.array([10, 20, 30, 40])})
# 40

first

first(arr)

Returns the first element of arr. Alias for at(arr, 0).

import numpy as np

expression_engine.solve('first(arr)', {'arr': np.array([10, 20, 30])})
# 10

last

last(arr)

Returns the last element of arr. Alias for at(arr, -1).

import numpy as np

expression_engine.solve('last(arr)', {'arr': np.array([10, 20, 30])})
# 30

top

top(arr, count)

Returns the count largest elements in arr, in descending order.

import numpy as np

expression_engine.solve('top(arr, 3)', {'arr': np.array([5, 1, 4, 2, 3])})
# [5, 4, 3]

bottom

bottom(arr, count)

Returns the count smallest elements in arr, in ascending order.

import numpy as np

expression_engine.solve('bottom(arr, 3)', {'arr': np.array([5, 1, 4, 2, 3])})
# [1, 2, 3]


Filtering & Mapping

filter

filter(arr, mask)

Returns only the elements of arr for which mask evaluates to True. Masks can be any boolean expression.

# Keep only positive values
filter('returns', 'returns' > 0)

# Keep positions matching two asset classes
filter('market_value', 'instrument_type' in ["Equity", "ETF"])

map

map(arr, mask_1, value_if_mask_1, , mask_n, value_if_mask_n)

Replaces values in arr based on ordered mask-value pairs. For each element, the output is the value paired with the first matching mask. Elements that match no mask are returned unchanged.

# Reclassify instrument types into broad buckets
map('instrument_type',
    'instrument_type' == "Bond",        "Fixed Income",
    'instrument_type' == "Equity",      "Equity",
    'instrument_type' == "Future",      "Derivatives")

where_np

where_np(condition, x, y)

Returns an array where each element is drawn from x when condition is True, and from y otherwise. Equivalent to numpy.where.

import numpy as np

expression_engine.solve('where_np(cond, x, y)', {
    'cond': np.array([True, False, True]),
    'x': np.array([1, 2, 3]),
    'y': np.array([4, 5, 6])
})
# [1, 5, 3]


Modification

delete

delete(arr, index)

Removes the element at index from arr. index can be a single integer or a list of integers.

import numpy as np

expression_engine.solve('delete(arr, 1)', {'arr': np.array([10, 20, 30])})
# [10, 30]

expression_engine.solve('delete(arr, [0, 2])', {'arr': np.array([10, 20, 30])})
# [20]

insert

insert(arr, index, value)

Inserts value into arr at position index. value can be a scalar or array.

import numpy as np

expression_engine.solve('insert(arr, 1, 99)', {'arr': np.array([10, 20, 30])})
# [10, 99, 20, 30]

expression_engine.solve('insert(arr, 1, [99, 100])', {'arr': np.array([10, 20, 30])})
# [10, 99, 100, 20, 30]

append

append(arr, value)

Appends value to the end of arr. value can be a scalar or array.

import numpy as np

expression_engine.solve('append(arr, 40)', {'arr': np.array([10, 20, 30])})
# [10, 20, 30, 40]

expression_engine.solve('append(arr, [40, 50])', {'arr': np.array([10, 20, 30])})
# [10, 20, 30, 40, 50]

resize

resize(arr, size)

Resizes arr to size elements. If size is larger than the original array, elements are repeated from the beginning.

import numpy as np

expression_engine.solve('resize(arr, 5)', {'arr': np.array([1, 2, 3])})
# [1, 2, 3, 1, 2]

expression_engine.solve('resize(arr, 2)', {'arr': np.array([1, 2, 3])})
# [1, 2]

trim

trim(arr, trim='fb', value=None)

Removes leading and/or trailing occurrences of value from arr.

trim Behavior
'fb' Remove from both front and back (default)
'f' Remove from the front only
'b' Remove from the back only
import numpy as np

expression_engine.solve('trim(arr, "fb", 0)', {'arr': np.array([0, 0, 1, 2, 0, 0])})
# [1, 2]

expression_engine.solve('trim(arr, "f", 0)', {'arr': np.array([0, 0, 1, 2, 0, 0])})
# [1, 2, 0, 0]

expression_engine.solve('trim(arr, "b", 0)', {'arr': np.array([0, 0, 1, 2, 0, 0])})
# [0, 0, 1, 2]


Reordering & Repetition

sort

sort(arr, order='asc', na_position='last')

Sorts arr. The sort is stable (equal elements preserve their original order).

Parameter Values Default
order 'asc', 'desc' 'asc'
na_position 'first', 'last' 'last'
import numpy as np

expression_engine.solve('sort(arr)', {'arr': np.array([3, 1, 4, 1, 5])})
# [1, 1, 3, 4, 5]

expression_engine.solve('sort(arr, "desc")', {'arr': np.array([3, 1, 4, 1, 5])})
# [5, 4, 3, 1, 1]

flip

flip(arr)

Reverses the order of elements in arr.

import numpy as np

expression_engine.solve('flip(arr)', {'arr': np.array([1, 2, 3, 4, 5])})
# [5, 4, 3, 2, 1]

roll

roll(arr, shift)

Circularly shifts elements of arr by shift positions. A positive shift moves elements to the right (wrapping the end around to the front).

import numpy as np

expression_engine.solve('roll(arr, 2)', {'arr': np.array([1, 2, 3, 4, 5])})
# [4, 5, 1, 2, 3]

expression_engine.solve('roll(arr, -1)', {'arr': np.array([1, 2, 3, 4, 5])})
# [2, 3, 4, 5, 1]

shift

shift(arr, window, fill_value=None)

Shifts arr by window positions. A positive window moves elements to the right (forward in time); a negative window moves them left. Vacated positions are filled with fill_value.

import numpy as np

expression_engine.solve('shift(arr, 2)', {'arr': np.array([1, 2, 3, 4, 5])})
# [None, None, 1, 2, 3]

expression_engine.solve('shift(arr, -2, 0)', {'arr': np.array([1, 2, 3, 4, 5])})
# [3, 4, 5, 0, 0]

tile

tile(arr, repeats)

Repeats the entire array repeats times end-to-end.

import numpy as np

expression_engine.solve('tile(arr, 3)', {'arr': np.array([1, 2, 3])})
# [1, 2, 3, 1, 2, 3, 1, 2, 3]

repeat

repeat(arr, repeats)

Repeats each element of arr repeats times in place.

import numpy as np

expression_engine.solve('repeat(arr, 2)', {'arr': np.array([1, 2, 3])})
# [1, 1, 2, 2, 3, 3]


Combining & Grouping

concatenate

concatenate(arr_1, arr_2, , arr_n)

Concatenates multiple arrays into a single array.

import numpy as np

expression_engine.solve('concatenate(a, b)', {
    'a': np.array([1, 2]),
    'b': np.array([3, 4])
})
# [1, 2, 3, 4]

unique

unique(arr)

Returns the distinct elements of arr, preserving first-occurrence order.

import numpy as np

expression_engine.solve('unique(arr)', {'arr': np.array([3, 1, 2, 1, 3])})
# [3, 1, 2]

group_by

group_by(groups, values, agg)

Groups values by the corresponding labels in groups and applies a NumPy aggregation function to each group. Results are returned in order of first appearance of each group label.

Supported values for agg: 'sum', 'mean', 'median', 'min', 'max', 'std', 'var', 'prod', 'size'.

import numpy as np

expression_engine.solve('group_by(groups, values, "sum")', {
    'groups': np.array(['a', 'b', 'a', 'b']),
    'values': np.array([1.0, 2.0, 3.0, 4.0])
})
# [4., 6.]   (a: 1+3=4, b: 2+4=6)

expression_engine.solve('group_by(groups, values, "mean")', {
    'groups': np.array(['x', 'y', 'x']),
    'values': np.array([10.0, 20.0, 30.0])
})
# [20., 20.]   (x: mean(10,30)=20, y: mean(20)=20)


Inspection

all

all(arr)

Returns True if every element in arr is truthy.

import numpy as np

expression_engine.solve('all(arr)', {'arr': np.array([True, True, True])})
# True

expression_engine.solve('all(arr)', {'arr': np.array([True, False, True])})
# False

any

any(arr)

Returns True if at least one element in arr is truthy.

import numpy as np

expression_engine.solve('any(arr)', {'arr': np.array([False, False, True])})
# True

expression_engine.solve('any(arr)', {'arr': np.array([False, False, False])})
# False

len

len(arr)

Returns the number of elements in arr.

import numpy as np

expression_engine.solve('len(arr)', {'arr': np.array([10, 20, 30])})
# 3

count

count(arr)

Alias for len. Returns the number of elements in arr.

import numpy as np

expression_engine.solve('count(arr)', {'arr': np.array([10, 20, 30])})
# 3