class watex.utils.box.Boxspace(**kws)[source]#

Bases: dict

Is a container object exposing keys as attributes.

BowlSpace objects are sometimes used as an output for functions and methods. They extend dictionaries by enabling values to be accessed by key, Boxspace[“value_key”], or by an attribute, Boxspace.value_key. Another option is to use Namespace of collection modules as:

>>> from collections import namedtuple
>>> Boxspace = namedtuple ('Boxspace', [< attribute names >] )

However the explicit class that inhers from build-in dict is easy to handle attributes and to avoid multiple error where the given name in the names attributes does not match the expected attributes to fetch.

Examples

>>> from watex.utils.box import Boxspace
>>> bs = Boxspace(pkg='watex',  objective ='give water', version ='0.1.dev')
>>> bs['pkg']
... 'watex'
>>> bs.pkg
... 'watex'
>>> bs.objective
... 'give water'
>>> bs.version
... '0.1.dev'
watex.utils.box.data2Box(data, /, name=None, use_colname=False, keep_col_data=True, columns=None)[source]#

Transform each data rows as Boxspace object.

Parameters:
  • data (DataFrame) – Data to transform as an object

  • columns (list of str,) – List of str item used to construct the dataframe if tuple or list is passed.

  • name (str, optional) – The object name. When string argument is given, the index value of the data is is used to prefix the name data unless the use_column_name is set to True.

  • use_colname (bool, default=False) – If True the name must be in columns. Otherwise an error raises. However, when use_colname=true, It is recommended to make sure whether each item in column data is distinct i.e. is unique, otherwise, some data will be erased. The number of object should be less than the data size along rows axis.

  • keep_col_data (bool, default=True) – Keep in the data the column that is used to construct the object name. Otherwise, column data whom object created from column name should be dropped.

Returns:

Object – Object that composed of many other objects where the number is equals to data size.

Return type:

BoxSpace, n_objects = data.size

Examples

>>> from watex.utils.box import data2Box
>>> o = data2Box ([2, 3, 4], name = 'borehole')
>>> o.borehole0
{'0': 2}
>>> o = data2Box ({"x": [2, 3, 4], "y":[8, 7, 5]}, name = 'borehole')
>>> o.borehole0.y
8
>>> from watex.utils.box import data2Box
>>> o = data2Box ([2, 3, 4], name = 'borehole', columns ='id')
>>> o.borehole0.id
2
>>> o = data2Box ({"x": [2, 3, 4], "y":[8, 7, 5],
                   "code": ['h2', 'h7', 'h12'] }, name = 'borehole')
>>> o.borehole1.code
'h7'
>>> o = data2Box ({"x": [2, 3, 4], "y":[8, 7, 5], "code": ['h2', 'h7', 'h12'] },
                  name = 'code', use_colname= True )
>>> o.h7.code
'h7'
>>> o = data2Box ({"x": [2, 3, 4], "y":[8, 7, 5], "code": ['h2', 'h7', 'h12']
                   }, name = 'code', use_colname= True, keep_col_data= False  )
>>> o.h7.code # code attribute does no longer exist
AttributeError: code