Detail: IndexHierarchy: Constructor

Overview: IndexHierarchy: Constructor

IndexHierarchy.__init__(indices, *, indexers=array([], dtype=int64), name=<object object>, blocks=None, own_blocks=False)[source]

Initializer.

Parameters:
  • indices – list of Index objects

  • indexers – a 2D indexer array

  • name – name of the IndexHierarchy

  • blocks

  • own_blocks

>>> a = np.array([[0, 0, 1, 1], [0, 1, 0, 1]])
>>> a.flags.writeable = False
>>> sf.IndexHierarchy((sf.Index(('a', 'b')), sf.Index((1024, 2048))), indexers=a)
<IndexHierarchy>
a                1024
a                2048
b                1024
b                2048
<<U1>            <int64>
classmethod IndexHierarchy.from_index_items(items, *, index_constructor=None, name=None)[source]

Given an iterable of pairs of label, IndexBase, produce an IndexHierarchy where the labels are depth 0, the indices are depth 1. While the provided IndexBase can be Index or IndexHierarchy, across all pairs all depths must be the same.

Parameters:
  • items – iterable of pairs of label, IndexBase.

  • index_constructor – Optionally provide index constructor for outermost index.

>>> ix1 = sf.Index(('a', 'b', 'c'), name='x')
>>> ix1
<Index: x>
a
b
c
<<U1>
>>> ix2 = sf.Index((1024, 2048, 4096), name='y')
>>> ix2
<Index: y>
1024
2048
4096
<int64>
>>> ih1 = sf.IndexHierarchy.from_index_items(((ix1.name, ix1), (ix2.name, ix2)), name='ih1')
>>> ih1
<IndexHierarchy: ih1>
x                     a
x                     b
x                     c
y                     1024
y                     2048
y                     4096
<<U1>                 <object>
>>> ih2 = sf.IndexHierarchy.from_index_items(((ix2.name, ix2), (ix1.name, ix1)), name='ih2')
>>> ih2
<IndexHierarchy: ih2>
y                     1024
y                     2048
y                     4096
x                     a
x                     b
x                     c
<<U1>                 <object>
>>> sf.IndexHierarchy.from_index_items(((ih1.name, ih1), (ih2.name, ih2)))
<IndexHierarchy>
ih1              x     a
ih1              x     b
ih1              x     c
ih1              y     1024
ih1              y     2048
ih1              y     4096
ih2              y     1024
ih2              y     2048
ih2              y     4096
ih2              x     a
ih2              x     b
ih2              x     c
<<U3>            <<U1> <object>
classmethod IndexHierarchy.from_labels(labels, *, name=None, reorder_for_hierarchy=False, index_constructors=None, depth_reference=None, continuation_token=<object object>)[source]

Construct an IndexHierarchy from an iterable of labels, where each label is tuple defining the component labels for all hierarchies.

Parameters:
  • labels – an iterator or generator of tuples.

  • *

  • name

  • reorder_for_hierarchy – an optional argument that will ensure the resulting index is arranged in a tree-like structure.

  • index_constructors

  • depth_reference

  • continuation_token – a Hashable that will be used as a token to identify when a value in a label should use the previously encountered value at the same depth.

Returns:

IndexHierarchy

>>> sf.IndexHierarchy.from_labels((('a', 1024, True), ('a', 2048, True), ('a', 2048, False), ('b', 1024, True)), name='x')
<IndexHierarchy: x>
a                   1024    True
a                   2048    True
a                   2048    False
b                   1024    True
<<U1>               <int64> <bool>
classmethod IndexHierarchy.from_labels_delimited(labels, *, delimiter=' ', name=None, index_constructors=None)[source]

Construct an IndexHierarchy from an iterable of labels, where each label is string defining the component labels for all hierarchies using a string delimiter. All components after splitting the string by the delimited will be literal evaled to produce proper types; thus, strings must be quoted.

Parameters:

labels – an iterator or generator of tuples.

Returns:

static_frame.IndexHierarchy

>>> sf.IndexHierarchy.from_labels_delimited(("'a'|1024|False", "'b'|1024|True", "'b'|2048|False"), delimiter='|')
<IndexHierarchy>
a                1024    False
b                1024    True
b                2048    False
<<U1>            <int64> <bool>
classmethod IndexHierarchy.from_names(names)[source]

Construct a zero-length IndexHierarchy from an iterable of names, where the length of names defines the zero-length depth.

Parameters:

names – Iterable of hashable names per depth.

>>> sf.IndexHierarchy.from_names(('x', 'y', 'z'))
<IndexHierarchy: ('x', 'y', 'z')>
<float64>                         <float64> <float64>
classmethod IndexHierarchy.from_pandas(value)[source]

Given a Pandas index, return the appropriate IndexBase derived class.

>>> mi = pd.MultiIndex.from_product((('a', 'b'), (1024, 2048)))
>>> sf.IndexHierarchy.from_pandas(mi)
<IndexHierarchy>
a                1024
a                2048
b                1024
b                2048
<object>         <int64>
classmethod IndexHierarchy.from_product(*levels, name=None, index_constructors=None)[source]

Given groups of iterables, return an IndexHierarchy made of the product of a values in those groups, where the first group is the top-most hierarchy.

Parameters:
  • *levels – index initializers (or Index instances) for each level

  • name

  • index_consructors

Returns:

static_frame.IndexHierarchy

>>> sf.IndexHierarchy.from_product(('a', 'b'), ('1517-04-01', '1620-11-21'), name='x', index_constructors=(sf.Index, sf.IndexDate))
<IndexHierarchy: x>
a                   1517-04-01
a                   1620-11-21
b                   1517-04-01
b                   1620-11-21
<<U1>               <datetime64[D]>
classmethod IndexHierarchy.from_tree(tree, *, name=None, index_constructors=None)[source]

Convert into a IndexHierarchy a dictionary defining keys to either iterables or nested dictionaries of the same.

Returns:

static_frame.IndexHierarchy

>>> sf.IndexHierarchy.from_tree({'a': {1024: (False, True), 2048: (True,)}})
<IndexHierarchy>
a                1024    False
a                1024    True
a                2048    True
<<U1>            <int64> <bool>
classmethod IndexHierarchy.from_values_per_depth(values, *, name=None, depth_reference=None, index_constructors=None)[source]

Construct an IndexHierarchy from a 2D NumPy array, or a collection of 1D arrays per depth.

Very similar implementation to _from_type_blocks(), but avoids creating TypeBlocks instance.

Returns:

IndexHierarchy

>>> sf.IndexHierarchy.from_values_per_depth((('a', 'a', 'b', 'b'), (0, 1, 0, 1)))
<IndexHierarchy>
a                0
a                1
b                0
b                1
<<U1>            <int64>
>>> sf.IndexHierarchy.from_values_per_depth((range(0, 12, 2), range(6)))
<IndexHierarchy>
0                0
2                1
4                2
6                3
8                4
10               5
<int64>          <int64>

IndexHierarchy: Constructor | Exporter | Attribute | Method | Dictionary-Like | Display | Selector | Iterator | Operator Binary | Operator Unary | Accessor Values | Accessor Datetime | Accessor String | Accessor Transpose | Accessor Regular Expression | Accessor Hashlib | Accessor Type Clinic