Enums#

policyengine_core.enums (renamed from .indexed_enums) contains definitions for enumerable types, which can be used to specify categorical data types.

Enum#

class policyengine_core.enums.enum.Enum(value)[source]#

Bases: Enum

Enum based on enum34, whose items have an index.

classmethod encode(array: Union[EnumArray, ndarray]) EnumArray[source]#

Encode an array of enum items or string identifiers into an EnumArray.

Parameters:

array – The input array to encode. Can be an EnumArray, a NumPy array of enum items, or a NumPy array of string identifiers.

Returns:

An EnumArray containing the encoded values.

Examples

>>> string_array = np.array(["ITEM_1", "ITEM_2", "ITEM_3"])
>>> encoded_array = MyEnum.encode(string_array)
>>> encoded_array
EnumArray([1, 2, 3], dtype=int8)
>>> item_array = np.array([MyEnum.ITEM_1, MyEnum.ITEM_2, MyEnum.ITEM_3])
>>> encoded_array = MyEnum.encode(item_array)
>>> encoded_array
EnumArray([1, 2, 3], dtype=int8)

EnumArray#

class policyengine_core.enums.enum_array.EnumArray(input_array: numpy.int_, possible_values: Optional[Type[Enum]] = None)[source]#

Bases: ndarray

Numpy array subclass representing an array of enum items.

EnumArrays are encoded as int arrays to improve performance

decode() object_[source]#

Return the array of enum items corresponding to self.

For instance:

>>> enum_array = household('housing_occupancy_status', period)
>>> enum_array[0]
>>> 2  # Encoded value
>>> enum_array.decode()[0]
<HousingOccupancyStatus.free_lodger: 'Free lodger'>

Decoded value: enum item

decode_to_str() str_[source]#

Return the array of string identifiers corresponding to self.

For instance:

>>> enum_array = household('housing_occupancy_status', period)
>>> enum_array[0]
>>> 2  # Encoded value
>>> enum_array.decode_to_str()[0]
'free_lodger'  # String identifier