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.
-
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
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. IfFalse
, atuple
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. IfFalse
, the keys of the dictionary are assumed to be tuples comprising elements of the selectionobjects
(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])