ndtools.comparison.builtins module#

ANY = ANY#

Comparable that is always evaluated as True.

Examples

import numpy as np
from ndtools import ANY

np.arange(3) == ANY  # -> array([True, True, True])
NEVER = NEVER#

Comparable that is always evaluated as False.

Examples

import numpy as np
from ndtools import NEVER

np.arange(3) == NEVER  # -> array([False, False, False])
class AnyType[source]#

Bases: Combinable, Equatable

Comparable that is always evaluated as True.

It is singleton and all instances created by AnyType() are thus identical. ndtools provides it as ndtools.ANY.

Examples

import numpy as np
from ndtools import ANY

np.arange(3) == ANY  # -> array([True, True, True])
Return type:

Self

class NeverType[source]#

Bases: Combinable, Equatable

Comparable that is always evaluated as False.

It is singleton and all instances created by NeverType() are thus identical. ndtools provides it as ndtools.NEVER.

Examples

import numpy as np
from ndtools import NEVER

np.arange(3) == NEVER  # -> array([False, False, False])
Return type:

Self

class Match(pat: str, case: bool = True, flags: int = 0, na: Any = None)[source]#

Bases: Combinable, Equatable

Comparable that matches regular expression to each array element.

It uses pandas.Series.str.fullmatch so the same options are available.

Parameters:
  • pat (str) – Character sequence or regular expression.

  • case (bool) – If True, case sensitive matching will be performed.

  • flags (int) – Regular expression flags, e.g. re.IGNORECASE.

  • na (Any) – Fill value for missing values. The default value depends on data type of the array. For object-dtype, numpy.nan will be used. For StringDtype, pandas.NA will be used.

Examples

import numpy as np
from ndtools import Match

np.array(["a", "aa"]) == Match("a+")  # -> array([True, True])
pat: str#

Character sequence or regular expression.

case: bool = True#

If True, case sensitive matching will be performed.

flags: int = 0#

Regular expression flags, e.g. re.IGNORECASE.

na: Any = None#

Fill value for missing values.

class Range(lower: Any, upper: Any, bounds: Literal['[]', '[)', '(]', '()'] = '[)')[source]#

Bases: Combinable, Orderable

Comparable that implements equivalence with a certain range.

Parameters:
  • lower (Any) – Lower value of the range. If None is specified, then the lower value comparison will be skipped.

  • lower – Upper value of the range. If None is specified, then the upper value comparison will be skipped.

  • bounds (Literal['[]', '[)', '(]', '()']) – Type of bounds of the range. []: Lower-closed and upper-closed. [): Lower-closed and upper-open (default). (]: Lower-open and upper-closed. (): Lower-open and upper-open.

  • upper (Any)

Examples

import numpy as np
from ndtools import Range

np.arange(3) == Range(1, 2) # -> array([False, True, False])
np.arange(3) < Range(1, 2)  # -> array([True, False, False])
np.arange(3) > Range(1, 2)  # -> array([False, False, True])

np.arange(3) == Range(None, 2)  # -> array([True, True, False])
np.arange(3) == Range(1, None)  # -> array([False, True, True])
np.arange(3) == Range(None, None)  # -> array([True, True, True])
lower: Any#

Lower value of the range.

upper: Any#

Upper value of the range.

bounds: Literal['[]', '[)', '(]', '()'] = '[)'#

Type of bounds of the range.

property is_lower_open: bool#

Check if the lower bound is open.

property is_lower_closed: bool#

Check if the lower bound is closed.

property is_upper_open: bool#

Check if the upper bound is open.

property is_upper_closed: bool#

Check if the upper bound is closed.

class Where(func: Callable[[...], Any], *args: Any, **kwargs: Any)[source]#

Bases: Combinable, Equatable

Comparable that applies a boolean function for multidimensional arrays.

Parameters:
  • func (Callable[[...], Any]) – Boolean function that takes func(array, *args, **kwargs).

  • *args (Any) – Positional arguments to be passed to the function.

  • **kwargs (Any) – Keyword arguments to be passed to the function.

Examples

import numpy as np
from ndtools import Where
from numpy.char import isupper

np.array(["A", "b"]) == Where(isupper)  # -> array([True, False])
func: Callable[[...], Any]#

Boolean function that takes func(array, *args, **kwargs).

args: Any#

Positional arguments to be passed to the function.

kwargs: Any#

Keyword arguments to be passed to the function.