idxhound packageΒΆ

class idxhound.Selection(obj, *, mapping=False, **kwargs)ΒΆ

Selection that tracks indexes across applications.

Parameters
  • obj (np.ndarray or iterable) – Selection object supported by numpy advanced indexing.

  • mapping (bool) – Whether the first argument is a mapping (primarily for internal use).

array_to_dict(x, *args, **kwargs)ΒΆ

Convert an array to a dictionary of key-value pairs, using this instance as the first selection object.

Note

See array_to_dict() for details.

Example

>>> cities = idxhound.Selection(['Rome', 'Berlin', 'Paris', 'London'])
>>> population = [2.873, 3.769, 2.148, 8.982]
>>> cities.array_to_dict(population)
{'Rome': 2.873, 'Berlin': 3.769, 'Paris': 2.148, 'London': 8.982}
compose(other)ΒΆ

Evaluate the composite selection equivalent to applying this selection followed by another.

Note

Selection composition is not commutative.

Parameters

other (Selection) – Selection to apply subsequently.

Returns

composite – Composite selection equivalent to applying this selection followed by another.

Return type

Selection

dict_to_array(d, *args, **kwargs)ΒΆ

Convert a dictionary of key-value pairs to an array, using this instance as the first selection object.

Note

See dict_to_array() for details.

>>> cities = idxhound.Selection(['Rome', 'Berlin', 'Paris', 'London'])
>>> population = {'Rome': 2.873, 'Berlin': 3.769, 'London': 8.982}
>>> cities.dict_to_array(population)
array([2.873, 3.769,   nan, 8.982])
classmethod from_iterable(keys)ΒΆ

Create a selection object from an iterable sequence of keys.

Note

Calling the constructor directly is suitable in most cases, but this method may be useful if the keys are provided in the form of an iterator or another type not easily castable to a numpy array, such as a set.

Parameters

keys (iterable) – Sequence of keys.

Returns

obj – Selection with the given sequence of keys.

Return type

Selection

Examples

>>> idxhound.Selection.from_iterable('abc')
Selection([('a', 0), ('b', 1), ('c', 2)])
idxhound.array_to_dict(x, *objects, squeeze=True)ΒΆ

Convert an array to a dictionary of key-value pairs.

Parameters
  • x (np.ndarray) – Array to convert.

  • *objects (iterable[Selection]) – Selection objects corresponding to the axes of the array.

  • squeeze (bool) – If True, return a simple key when the array is one-dimensional. If False, a tuple with one element is returned (cf. dict_to_array()).

Returns

d – Dictionary of key-value pairs encoding the array.

Return type

dict

Example

>>> cities = ['Rome', 'Berlin', 'Paris', 'London']
>>> population = [2.873, 3.769, 2.148, 8.982]
>>> idxhound.array_to_dict(population, idxhound.Selection(cities))
{'Rome': 2.873, 'Berlin': 3.769, 'Paris': 2.148, 'London': 8.982}
idxhound.dict_to_array(d, *objects, fill_value=nan, dtype=None, squeezed=True, ignore_missing_keys=False)ΒΆ

Convert a dictionary of key-value pairs to an array.

Parameters
  • d (dict) – Dictionary to convert.

  • *objects (iterable[Selection]) – Selection objects corresponding to the axes of the array.

  • fill_value (numbers.Number) – Value used for missing data.

  • dtype – Data type of the resultant array.

  • squeezed (bool) – If True and one selection object is provided, the keys of the dictionary are assumed to be elements of the selection. If False, the keys of the dictionary are assumed to be tuples comprising elements of the selection objects (cf. array_to_dict()).

  • ignore_missing_keys (bool) – If True, keys in the dictionary that are not present in the selection objects are silently dropped.

Returns

x – Array encoded by the dictionary of key-value pairs.

Return type

np.ndarray

Example

>>> cities = ['Rome', 'Berlin', 'Paris', 'London']
>>> population = {'Rome': 2.873, 'Berlin': 3.769, 'London': 8.982}
>>> idxhound.dict_to_array(population, idxhound.Selection(cities))
array([2.873, 3.769,   nan, 8.982])