The functions that operate on larry can be divided into the following broad categories:
Functions and examples
Below you’ll find the functions in each category along with examples. All of the examples assume that you have already imported larry:
>>> from la import larry
The reference guide for the larry methods, as opposed to functions, can be found in larry methods.
The alignment functions help you align one of more larrys.
Align two larrys using one of five join methods.
Parameters :  lar1 : larry
lar2 : larry
join : {‘inner’, ‘outer’, ‘left’, ‘right’, ‘skip’, list}, optional
cast : bool, optional


Returns :  lar3 : larry
lar4 : larry

See also
Examples
Create two larrys:
>>> lar1 = larry([1, 2])
>>> lar2 = larry([1, 2, 3])
The default join method is an inner join:
>>> lar3, lar4 = la.align(lar1, lar2)
>>> lar3
label_0
0
1
x
array([1, 2])
>>> lar4
label_0
0
1
x
array([1, 2])
An outer join adds a missing value (NaN) to lar1, therefore the the dtype of lar1 is changed from int to float:
>>> lar3, lar4 = la.align(lar1, lar2, join='outer')
>>> lar3
label_0
0
1
2
x
array([ 1., 2., NaN])
>>> lar4
label_0
0
1
2
x
array([1, 2, 3])
Align two larrys but return Numpy arrays and label instead of larrys.
This function is the same as la.align() except that instead of returning two larrys, the components of the two larrys are returned (two Numpy arrays, a label, and flags for whether the two Numpy arrays are views of the data arrays of the corresponding input larrys).
Parameters :  lar1 : larry
lar2 : larry
join : {‘inner’, ‘outer’, ‘left’, ‘right’, ‘skip’, list}, optional
cast : bool, optional


Returns :  x1 : ndarray
x2 : ndarray
label : list of lists
x1isview : bool
x2isview : bool

See also
Notes
The returned Numpy arrays are views of the corresponding input larrys if the labels of the two input larrys are the same along all axes. If the labels are not the same along any axis then a copy is returned.
Examples
Create two larrys:
>>> y1 = larry([1, 2])
>>> y2 = larry([1, 2, 3])
The default join method is an inner join:
>>> x1, x2, label, x1isview, x2isview = la.flarry._align_raw(lar1, lar2)
>>> x1
array([1, 2])
>>> x2
array([1, 2])
>>> label
[[0, 1]]
>>> x1isview
False
>>> x2isview
False
An outer join adds a missing value (NaN) to lar1, therefore the the dtype of lar1 is changed from int to float:
>>> x1, x2, label, x1isview, x2isview = la.flarry._align_raw(lar1, lar2, join='outer')
>>> x1
array([ 1., 2., NaN])
>>> x2
array([1, 2, 3])
>>> label
[[0, 1, 2]]
>>> x1isview
False
>>> x2isview
False
If the labels are already aligned, then a view of the data array is returned:
>>> lar1 = larry([1, 2])
>>> lar2 = larry([3, 4])
>>> x1, x2, label, x1isview, x2isview = la.flarry._align_raw(lar1, lar2)
>>> x1isview
True
>>> x2isview
True
Align many larrys along potentially different axes.
Parameters :  lars : array_like
axis : {int, array_like}, optional
join : {‘inner’, ‘outer’, ‘left’, ‘right’}, optional


Returns :  (lar1, lar2, ...) : tuple

Examples
Create three larrys:
>>> l1 = la.larry([1, 2, 3, 4], [['a', 'b', 'c', 'd']])
>>> l2 = la.larry([[4, 5], [6, 7]], [['x', 'y'], ['c', 'd']])
>>> l3 = la.larry([8, 9, 10], [['c', 'd', 'e']])
Align the first axis of the first larry with the second axis of the second larry using an inner join:
>>> a1, a2 = la.align_axis([l1, l2], axis=[0, 1])
>>> a1
label_0
c
d
x
array([3, 4])
>>> a2
label_0
x
y
label_1
c
d
x
array([[4, 5],
[6, 7]])
Align the first axis of two larrys with an outer join:
>>> a1, a2 = la.align_axis([l1, l3], join='outer')
>>> a1
label_0
a
b
c
d
e
x
array([ 1., 2., 3., 4., nan])
>>> a2
label_0
a
b
c
d
e
x
array([ nan, nan, 8., 9., 10.])
Align multiple larrys with an inner join:
>>> a1, a2, a3 = la.align_axis([l1, l2, l3], axis=[0, 1, 0])
>>> a1
label_0
c
d
x
array([3, 4])
>>> a2
label_0
x
y
label_1
c
d
x
array([[4, 5],
[6, 7]])
>>> a3
label_0
c
d
x
array([8, 9])
Return True if labels of two given larrys are aligned along specified axis.
Parameters :  lar1 : larry
lar2 : larry
axis : {int, None}, optional


Returns :  y : bool

See also
Examples
Make two 2d larrys that are aligned along columns but not rows:
>>> lar1 = larry([[1, 2], [3, 4]], [['row1', 'row2'], ['col1', 'col2']])
>>> lar2 = larry([[1, 2], [3, 4]], [['row2', 'row1'], ['col1', 'col2']])
The two larrys are not aligned:
>>> la.isaligned(lar1, lar2)
False
>>> la.isaligned(lar1, lar2, axis=0)
False
But the columns of the two larrys are aligned:
>>> la.isaligned(lar1, lar2, axis=1)
True
Union of labels along specified axis.
Parameters :  axis : int
args : larrys


Returns :  out : list

See also
Examples
>>> import la
>>> y1 = larry([[1, 2], [3, 4]], [['a', 'b'], ['c', 'd']])
>>> y2 = larry([[1, 2], [3, 4]], [['e', 'b'], ['f', 'd']])
>>> la.union(0, y1, y2)
['a', 'b', 'e']
>>> la.union(1, y1, y2)
['c', 'd', 'f']
Sorted list containing the intersection of labels along specified axis.
Parameters :  axis : int
args : larrys


Returns :  out : list

See also
Examples
>>> import la
>>> y1 = larry([[1, 2], [3, 4]], [['a', 'b'], ['c', 'd']])
>>> y2 = larry([[1, 2], [3, 4]], [['e', 'b'], ['f', 'd']])
>>> la.intersection(0, y1, y2)
['b']
>>> la.intersection(1, y1, y2)
['d']
The binary functions combine two larrys into one.
Binary operation on two larrys using given function and join method.
Parameters :  func : function
lar1 : larry
lar2 : larry
join : {‘inner’, ‘outer’, ‘left’, ‘right’, list}, optional
cast : bool, optional
missone : {scalar, ‘ignore’}, optional
misstwo : {scalar, ‘ignore’}, optional
**kwargs : Keyword arguments, optional


Returns :  lar3 : larry

See also
Examples
Create two larrys:
>>> from la import nan
>>> lar1 = larry([1, 2, nan], [['a', 'b', 'c']])
>>> lar2 = larry([1, nan, nan], [['a', 'b', 'dd']])
The default is an inner join (note that lar1 and lar2 have two labels in common):
>>> la.binaryop(np.add, lar1, lar2)
label_0
a
b
x
array([ 2., NaN])
If one data element is missing in one larry but not in the other, then you can replace the missing value with missone (here 0):
>>> la.binaryop(np.add, lar1, lar2, missone=0)
label_0
a
b
x
array([ 2., 2.])
An outer join:
>>> la.binaryop(np.add, lar1, lar2, join='outer')
label_0
a
b
c
dd
x
array([ 2., NaN, NaN, NaN])
An outer join with single and double missing values replaced by zero:
>>> la.binaryop(np.add, lar1, lar2, join='outer', missone=0, misstwo=0)
label_0
a
b
c
dd
x
array([ 2., 2., 0., 0.])
Sum of two larrys using given join and fill methods.
Parameters :  lar1 : larry
lar2 : larry
join : {‘inner’, ‘outer’, ‘left’, ‘right’, list}, optional
cast : bool, optional
missone : {scalar, ‘ignore’}, optional
misstwo : {scalar, ‘ignore’}, optional


Returns :  y : larry

See also
Notes
This is a convenience function that calls la.binaryop() with func set to numpy.add.
Examples
Create two larrys:
>>> from la import nan
>>> lar1 = larry([1, 2, nan], [['a', 'b', 'c']])
>>> lar2 = larry([1, nan, nan], [['a', 'b', 'dd']])
The default is an inner join (note that lar1 and lar2 have two labels in common):
>>> la.add(lar1, lar2)
label_0
a
b
x
array([ 2., NaN])
which is the same result you get with lar1 + lar2:
>>> lar1 + lar2
label_0
a
b
x
array([ 2., NaN])
If one data element is missing in one larry but not in the other, then you can replace the missing value with missone (here 0):
>>> la.add(lar1, lar2, missone=0)
label_0
a
b
x
array([ 2., 2.])
An outer join:
>>> la.add(lar1, lar2, join='outer')
label_0
a
b
c
dd
x
array([ 2., NaN, NaN, NaN])
An outer join with single and double missing values replaced by zero:
>>> la.add(lar1, lar2, join='outer', missone=0, misstwo=0)
label_0
a
b
c
dd
x
array([ 2., 2., 0., 0.])
Difference of two larrys using given join and fill methods.
Parameters :  lar1 : larry
lar2 : larry
join : {‘inner’, ‘outer’, ‘left’, ‘right’, list}, optional
cast : bool, optional
missone : {scalar, ‘ignore’}, optional
misstwo : {scalar, ‘ignore’}, optional


Returns :  y : larry

See also
Notes
This is a convenience function that calls la.binaryop() with func set to numpy.subtract.
Examples
Create two larrys:
>>> from la import nan
>>> lar1 = larry([1, 2, nan], [['a', 'b', 'c']])
>>> lar2 = larry([1, nan, nan], [['a', 'b', 'dd']])
The default is an inner join (note that lar1 and lar2 have two labels in common):
>>> la.subtract(lar1, lar2)
label_0
a
b
x
array([ 0., NaN])
which is the same result you get with lar1  lar2:
>>> lar1  lar2
label_0
a
b
x
array([ 0., NaN])
If one data element is missing in one larry but not in the other, then you can replace the missing value with missone (here 0):
>>> la.subtract(lar1, lar2, missone=0)
label_0
a
b
x
array([ 0., 2.])
An outer join:
>>> la.subtract(lar1, lar2, join='outer')
label_0
a
b
c
dd
x
array([ 0., NaN, NaN, NaN])
An outer join with single and double missing values replaced by zero:
>>> la.subtract(lar1, lar2, join='outer', missone=0, misstwo=0)
label_0
a
b
c
dd
x
array([ 0., 2., 0., 0.])
Multiply two larrys elementwise using given join and fill methods.
Parameters :  lar1 : larry
lar2 : larry
join : {‘inner’, ‘outer’, ‘left’, ‘right’, list}, optional
cast : bool, optional
missone : {scalar, ‘ignore’}, optional
misstwo : {scalar, ‘ignore’}, optional


Returns :  y : larry

See also
Notes
This is a convenience function that calls la.binaryop() with func set to numpy.multiply.
Examples
Create two larrys:
>>> from la import nan
>>> lar1 = larry([1, 2, nan], [['a', 'b', 'c']])
>>> lar2 = larry([1, nan, nan], [['a', 'b', 'dd']])
The default is an inner join (note that lar1 and lar2 have two labels in common):
>>> la.multiply(lar1, lar2)
label_0
a
b
x
array([ 1., NaN])
which is the same result you get with lar1 * lar2:
>>> lar1 * lar2
label_0
a
b
x
array([ 1., NaN])
If one data element is missing in one larry but not in the other, then you can replace the missing value with missone (here 1):
>>> la.multiply(lar1, lar2, missone=1)
label_0
a
b
x
array([ 1., 2.])
An outer join:
>>> la.multiply(lar1, lar2, join='outer')
label_0
a
b
c
dd
x
array([ 1., NaN, NaN, NaN])
An outer join with single and double missing values replaced by one:
>>> la.multiply(lar1, lar2, join='outer', missone=1, misstwo=1)
label_0
a
b
c
dd
x
array([ 1., 2., 1., 1.])
Divide two larrys elementwise using given join and fill methods.
Parameters :  lar1 : larry
lar2 : larry
join : {‘inner’, ‘outer’, ‘left’, ‘right’, list}, optional
cast : bool, optional
missone : {scalar, ‘ignore’}, optional
misstwo : {scalar, ‘ignore’}, optional


Returns :  y : larry

See also
Notes
This is a convenience function that calls la.binaryop() with func set to numpy.divide.
Examples
Create two larrys:
>>> from la import nan
>>> lar1 = larry([1, 2, nan], [['a', 'b', 'c']])
>>> lar2 = larry([1, nan, nan], [['a', 'b', 'dd']])
The default is an inner join (note that lar1 and lar2 have two labels in common):
>>> la.divide(lar1, lar2)
label_0
a
b
x
array([ 1., NaN])
which is the same result you get with lar1 / lar2:
>>> lar1 / lar2
label_0
a
b
x
array([ 1., NaN])
If one data element is missing in one larry but not in the other, then you can replace the missing value with missone (here 1):
>>> la.divide(lar1, lar2, missone=1)
label_0
a
b
x
array([ 1., 2.])
An outer join:
>>> la.divide(lar1, lar2, join='outer')
label_0
a
b
c
dd
x
array([ 1., NaN, NaN, NaN])
An outer join with single and double missing values replaced by one:
>>> la.divide(lar1, lar2, join='outer', missone=1, misstwo=1)
label_0
a
b
c
dd
x
array([ 1., 2., 1., 1.])
Functions that quickly generate larrys.
Return a new larry of given shape and type, filled with zeros.
Parameters :  shape : {int, tuple}, optional
label : list, optional
dtype : datatype, optional
order : {‘C’, ‘F’}, optional


Returns :  lar : larry

See also
empty, ones, lrange
Examples
A basic, 1d larry using the ‘dtype’ argument:
>>> la.zeros(3, dtype='i4')
label_0
0
1
2
x
array([0, 0, 0], dtype=np.int32)
A multidimensional larry:
>>> la.zeros(2, 3, 3)
label_0
0
1
label_1
0
1
2
label_2
0
1
2
x
array([[[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]],
.
[[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]]])
Using the ‘label’ argument:
>>> la.zeros(label=[['a', 'b']])
label_0
a
b
x
array([0., 0.])
Return a new larry of given shape and type, filled with ones.
Parameters :  shape : {int, tuple}, optional
label : list, optional
dtype : datatype, optional
order : {‘C’, ‘F’}, optional


Returns :  lar : larry

See also
empty, zeros, lrange
Examples
A basic, 1d larry using the ‘dtype’ argument:
>>> la.ones(3, dtype='i4')
label_0
0
1
2
x
array([1, 1, 1], dtype=np.int32)
A multidimensional larry:
>>> la.ones(2, 3, 3)
label_0
0
1
label_1
0
1
2
label_2
0
1
2
x
array([[[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]],
.
[[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]]])
Using the ‘label’ argument:
>>> la.ones(label=[['a', 'b']])
label_0
a
b
x
array([1., 1.])
Return a new larry of given shape and type, without initializing entries.
Parameters :  shape : {int, tuple}, optional
label : list, optional
dtype : datatype, optional
order : {‘C’, ‘F’}, optional


Returns :  lar : larry

See also
zeros, ones, lrange
Examples
A basic, 1d larry using the ‘dtype’ argument:
>>> la.empty(3, dtype='i4')
label_0
0
1
2
3
4
x
array([0, 7, 987], dtype=np.int32)
A multidimensional larry:
>>> la.empty(2, 3, 3)
label_0
0
1
label_1
0
1
2
label_2
0
1
2
x
array([[[ 0e0, 0e0, 0e0],
[ 0e0, 0e0, 0e0],
[ 0e0, 0e0, 0e0]],
[[ 3.2e254, 0e0, 0e0],
[ 0e0., 0e0, 0e0],
[ 0e0, 0e0, 0e0]]])
Using the ‘label’ argument:
>>> la.empty(label=[['a', 'b']])
label_0
a
b
x
array([0e0, 3.2e256])
Return a new larry of sequential integers, shaped according to input.
Parameters :  shape : {int, tuple}, optional
label : list, optional
start : int, optional
step : int, optional
dtype : datatype, optional


Returns :  lar : larry

See also
empty, ones, zeros
Examples
A basic, 1d lrange using the ‘dtype’ argument:
>>> la.lrange(3, dtype='f4')
label_0
0
1
2
x
array([0.0, 1.0, 2.0], dtype=np.float32)
A multidimensional lrange:
>>> la.lrange((2,3,3))
label_0
0
1
label_1
0
1
2
label_2
0
1
2
x
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
.
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]]])
Using the label keyword:
>>> la.lrange(label=[['a', 'b']])
label_0
a
b
x
array([0, 1])
Functions that return larrys containing random samples.
Random samples from a uniform distribution in a given shape.
The random samples are from a uniform distribution over [0, 1).
Parameters :  args : n ints, optional
kwargs : keyword arguments, optional


Returns :  Z : larry or float

See also
Examples
A single random sample:
>>> la.rand()
0.64323350463488804
A shape (2, 2) random larry:
>>> la.rand(2, 2)
label_0
0
1
label_1
0
1
x
array([[ 0.09277439, 0.94194077],
[ 0.72887997, 0.41124147]])
A shape (2, 2) random larry with given labels:
>>> la.rand(label=[['row1', 'row2'], ['col1', 'col2']])
label_0
row1
row2
label_1
col1
col2
x
array([[ 0.3449072 , 0.40397174],
[ 0.7791279 , 0.86084403]])
Results are repeatable if you set the state of the random number generator outside of la.rand:
>>> import numpy as np
>>> rs = np.random.RandomState([1, 2, 3])
>>> la.randn(randn=rs.randn)
0.89858244820995015
>>> la.randn(randn=rs.randn)
0.25528876596298244
>>> rs = np.random.RandomState([1, 2, 3])
>>> la.randn(randn=rs.randn)
0.89858244820995015
>>> la.randn(randn=rs.randn)
0.25528876596298244
Random samples from the “standard normal” distribution in a given shape.
The random samples are from a “normal” (Gaussian) distribution of mean 0 and variance 1.
Parameters :  args : n ints, optional
kwargs : keyword arguments, optional


Returns :  Z : larry or float

See also
Examples
A single random sample:
>>> la.randn()
0.33086946957034052
A shape (2, 2) random larry:
>>> la.randn(2, 2)
label_0
0
1
label_1
0
1
x
array([[0.08182341, 0.79768108],
[0.23584547, 1.80118376]])
A shape (2, 2) random larry with given labels:
>>> la.randn(label=[['row1', 'row2'], ['col1', 'col2']])
label_0
row1
row2
label_1
col1
col2
x
array([[ 0.10737701, 0.24947824],
[ 1.51021208, 1.00280387]])
Results are repeatable if you set the state of the random number generator outside of la.rand:
>>> import numpy as np
>>> rs = np.random.RandomState([1, 2, 3])
>>> la.randn(randn=rs.randn)
0.89858244820995015
>>> la.randn(randn=rs.randn)
0.25528876596298244
>>> rs = np.random.RandomState([1, 2, 3])
>>> la.randn(randn=rs.randn)
0.89858244820995015
>>> la.randn(randn=rs.randn)
0.25528876596298244
Miscellaneous functions that operate on larrys.
Find the unique elements of a larry.
Returns the sorted unique elements of a larry as a Numpy array. There are two optional outputs in addition to the unique elements: the indices of the input larry that give the unique values, and the indices of the unique array that reconstruct the input array.
Parameters :  ar : larry
return_index : bool, optional
return_inverse : bool, optional


Returns :  unique : ndarray
unique_indices : ndarray, optional
unique_inverse : ndarray, optional

Examples
>>> la.unique(larry([1, 1, 2, 2, 3, 3]))
array([1, 2, 3])
>>> lar = larry([[1, 1], [2, 3]])
>>> la.unique(lar)
array([1, 2, 3])
Return the indices of the original larry that give the unique values:
>>> lar = larry(['a', 'b', 'b', 'c', 'a'])
>>> u, indices = la.unique(lar, return_index=True)
>>> u
array(['a', 'b', 'c'],
dtype='S1')
>>> indices
array([0, 1, 3])
>>> lar[indices]
array(['a', 'b', 'c'],
dtype='S1')
Reconstruct the input array (the data portion of the larry, not the label portion) from the unique values:
>>> lar = larry([1, 2, 6, 4, 2, 3, 2])
>>> u, indices = la.unique(lar, return_inverse=True)
>>> u
array([1, 2, 3, 4, 6])
>>> indices
array([0, 1, 4, 3, 1, 2, 1])
>>> u[indices]
array([1, 2, 6, 4, 2, 3, 2])
Stack 2d larrys to make a 3d larry.
Parameters :  mode : {‘union’, ‘intersection’}
kwargs : name=larry


Returns :  out : larry

Raises :  ValueError :

Examples
>>> import la
>>> y1 = la.larry([[1, 2], [3, 4]])
>>> y2 = la.larry([[5, 6], [7, 8]])
>>> la.stack('union', name1=y1, othername=y2)
label_0
othername
name1
label_1
0
1
label_2
0
1
x
array([[[ 5., 6.],
[ 7., 8.]],
.
[[ 1., 2.],
[ 3., 4.]]])
Convert a 3d larry of shape (n, m, k) to a 2d larry of shape (m*k, n).
Parameters :  lar : 3d larry


Returns :  y : 2d larry

See also
Examples
First make a 3d larry:
>>> x = np.ones((2,2)).cumsum(0)  1
>>> lar = la.larry(x, [['r1', 'r2'], ['c1', 'c2']])
>>> lar = lar.insertaxis(0, "name")
>>> lar
label_0
name
label_1
r1
r2
label_2
c1
c2
x
array([[[ 0., 0.],
[ 1., 1.]]])
Then make a panel:
>>> la.panel(lar)
label_0
('r1', 'c1')
('r1', 'c2')
('r2', 'c1')
('r2', 'c2')
label_1
name
x
array([[ 0.],
[ 0.],
[ 1.],
[ 1.]])
Covariance matrix adjusted for missing (NaN) values.
Note: Only works on 2d larrys.
The mean of each row is assumed to be zero. So rows are not demeaned and therefore the covariance is normalized by the number of columns, not by the number of columns minus 1.
Parameters :  lar : larry


Returns :  out : larry

Raises :  ValueError :

Sort 2d larry by the row or column corresponding to given label element.
Parameters :  lar : larry
element : str
axis : int
reverse : bool, optional


Returns :  lar2 : larry

Examples
Create a larry:
>>> label = [['a','b'], ['c','d','e']]
>>> x = np.array([[1, 2, 3], [3, 1, 2]])
>>> lar = la.larry(x, label)
Sort larry by row ‘b’ along the first axis:
>>> la.sortby(lar, 'b', axis=0)
label_0
a
b
label_1
d
e
c
x
array([[2, 3, 1],
[1, 2, 3]])