colss is a lightweight expression evaluator that evaluates math-style string expressions on NumPy, Pandas, Polars, and standard Python arrays, reducing verbosity compared to native syntax
Source: https://github.com/SivaPA08/colss
eg:
colss.sumof("exp(sin(a)) + log(b+1)^2 - c^3")
pip install colss
All arrays must be 1D, preferably float64, and C-contiguous. If you have a 2D array:
a = a.ravel()All functions accept a string expression. Just pass the expression — no need to register variables or pass arrays manually.
Evaluates an expression element-wise and returns a NumPy array.
import numpy as np
import colss
a = np.array([1.0, 2.0, 3.0], dtype=np.float64)
b = np.array([4.0, 5.0, 6.0], dtype=np.float64)
colss.query("a + b + 7")
colss.query("a > 1 ? 100 : 0") # ternary
colss.query("sqrt(a) + sin(b)")
colss.query("a ^ 2") # a squared
colss.query("a ^ 2 + b ^ 0.5") # complex expressionReturns the arithmetic mean of the evaluated expression as a scalar.
colss.mean("a")
colss.mean("a + b")
colss.mean("a ^ 2 + b")Returns the sum of the evaluated expression as a scalar.
colss.sumof("a")
colss.sumof("a * 2")
colss.sumof("a + b")Returns the product of all elements of the evaluated expression as a scalar.
colss.prod("a")
colss.prod("a + 1")
colss.prod("a * b")Returns the median of the evaluated expression as a scalar. For even-length arrays, returns the average of the two middle values.
colss.median("a")
colss.median("a + b")
colss.median("a ^ 2 + b")Returns the population standard deviation of the evaluated expression as a scalar.
colss.sd("a")
colss.sd("a + b")
colss.sd("sqrt(a) + b ^ 2")Returns the population variance of the evaluated expression as a scalar.
colss.var("a")
colss.var("a + b")
colss.var("a ^ 2 - b")colss.query() returns a NumPy array, so you can directly use NumPy array methods and operations like .mean(), .sum(), .reshape(), .astype(), .max(), .min(), .std(), .var(), and more on the result.
import numpy as np
import colss
a = np.array([1.0, 2.0, 3.0], dtype=np.float64)
b = np.array([4.0, 5.0, 6.0], dtype=np.float64)
colss.query("a + b").mean()
colss.query("a ^ 2").sum()
colss.query("sqrt(a)").reshape((3, 1))
colss.query("a * b").astype(np.float32)
colss.query("a + 5").max()
colss.query("a + b").min()
colss.query("a ^ 2").std()
colss.query("a ^ 2").var()import pandas as pd
import numpy as np
import colss
df = pd.DataFrame({
"a": [1.0, 2.0, 3.0],
"b": [4.0, 5.0, 6.0]
})
a = df["a"].to_numpy(dtype=np.float64)
b = df["b"].to_numpy(dtype=np.float64)
df["c"] = colss.query("a + b + 7")
m = colss.mean("a + b")
med = colss.median("a")
s = colss.sd("a + b")| Operator | Description |
|---|---|
+ - * / |
Arithmetic |
^ |
Exponentiation — a ^ 2 raises a to the power of 2 |
% |
Modulo |
> < >= <= == != |
Comparison |
and or not |
Logical |
condition ? a : b |
Ternary |
import numpy as np
import colss
a = np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float64)
b = np.array([4.0, 3.0, 2.0, 1.0], dtype=np.float64)
# comparison
colss.query("a > 2")
colss.query("a <= b")
colss.query("a == b")
# logical
colss.query("(a > 1) and (b < 4)")
colss.query("(a > 2) or (b == 1)")
colss.query("not(a > 2)")
# ternary
colss.query("a > 2 ? a * 10 : a + 1")
# conditional
colss.query("if(a>b,a,b)")abs(x) sqrt(x)
log(x) log10(x) exp(x)
sin(x) cos(x) tan(x)
floor(x) ceil(x)
min(x, y) max(x, y)
^is exponentiation, not bitwise XOR.- All arrays used in an expression must be the same length.
- You can use both scalar and vector at the same time.
- A NaN or Inf result raises an error identifying the first offending index.