larray.LArray

class larray.LArray(data, axes=None, title=None, meta=None, dtype=None)[source]

A LArray object represents a multidimensional, homogeneous array of fixed-size items with labeled axes.

The function aslarray() can be used to convert a NumPy array or Pandas DataFrame into a LArray.

Parameters
datascalar, tuple, list or NumPy ndarray

Input data.

axescollection (tuple, list or AxisCollection) of axes (int, str or Axis), optional

Axes.

titlestr, optional

Deprecated. See ‘meta’ below.

metalist of pairs or dict or OrderedDict or Metadata, optional

Metadata (title, description, author, creation_date, …) associated with the array. Keys must be strings. Values must be of type string, int, float, date, time or datetime.

dtypetype, optional

Datatype for the array. Defaults to None (inferred from the data).

Warning

Metadata is not kept when actions or methods are applied on an array except for operations modifying the object in-place, such as: pop[age < 10] = 0. Do not add metadata to an array if you know you will apply actions or methods on it before dumping it.

See also

sequence

Create a LArray by sequentially applying modifications to the array along axis.

ndtest

Create a test LArray with increasing elements.

zeros

Create a LArray, each element of which is zero.

ones

Create a LArray, each element of which is 1.

full

Create a LArray filled with a given value.

empty

Create a LArray, but leave its allocated memory unchanged (i.e., it contains “garbage”).

Examples

>>> age = Axis([10, 11, 12], 'age')
>>> sex = Axis('sex=M,F')
>>> time = Axis([2007, 2008, 2009], 'time')
>>> axes = [age, sex, time]
>>> data = np.zeros((len(axes), len(sex), len(time)))
>>> LArray(data, axes)
age  sex\time  2007  2008  2009
 10         M   0.0   0.0   0.0
 10         F   0.0   0.0   0.0
 11         M   0.0   0.0   0.0
 11         F   0.0   0.0   0.0
 12         M   0.0   0.0   0.0
 12         F   0.0   0.0   0.0
>>> # with metadata (Python <= 3.5)
>>> arr = LArray(data, axes, meta=[('title', 'my title'), ('author', 'John Smith')])
>>> # with metadata (Python 3.6+)
>>> arr = LArray(data, axes, meta=Metadata(title='my title', author='John Smith'))  # doctest: +SKIP

Array creation functions

>>> full(axes, 10.0)
age  sex\time  2007  2008  2009
 10         M  10.0  10.0  10.0
 10         F  10.0  10.0  10.0
 11         M  10.0  10.0  10.0
 11         F  10.0  10.0  10.0
 12         M  10.0  10.0  10.0
 12         F  10.0  10.0  10.0
>>> arr = empty(axes)
>>> arr['F'] = 1.0
>>> arr['M'] = -1.0
>>> arr
age  sex\time  2007  2008  2009
 10         M  -1.0  -1.0  -1.0
 10         F   1.0   1.0   1.0
 11         M  -1.0  -1.0  -1.0
 11         F   1.0   1.0   1.0
 12         M  -1.0  -1.0  -1.0
 12         F   1.0   1.0   1.0
>>> bysex = sequence(sex, initial=-1, inc=2)
>>> bysex
sex   M  F
     -1  1
>>> sequence(age, initial=10, inc=bysex)
sex\age  10  11  12
      M  10   9   8
      F  10  11  12
Attributes
dataNumPy ndarray

Data.

axesAxisCollection

Axes.

metaMetadata

Returns metadata of the array.

__init__(self, data, axes=None, title=None, meta=None, dtype=None)[source]

Initialize self. See help(type(self)) for accurate signature.

Methods

__init__(self, data[, axes, title, meta, dtype])

Initialize self.

align(self, other[, join, fill_value, axes])

Align two arrays on their axes with the specified join method.

all(*axes_and_groups[, out, skipna, keepaxes])

Test whether all selected elements evaluate to True.

all_by(*axes_and_groups[, out, skipna, keepaxes])

Test whether all selected elements evaluate to True.

any(*axes_and_groups[, out, skipna, keepaxes])

Test whether any selected elements evaluate to True.

any_by(*axes_and_groups[, out, skipna, keepaxes])

Test whether any selected elements evaluate to True.

append(self, axis, value[, label])

Adds an array to self along an axis.

apply(self, transform, \*args, \*\*kwargs)

Apply a transformation function to array elements.

apply_map(self, mapping[, dtype])

Apply a transformation mapping to array elements.

argmax(\*args, \*\*kwargs)

argmin(\*args, \*\*kwargs)

argsort(\*args, \*\*kwargs)

as_table(self[, maxlines, edgeitems, light, …])

Deprecated.

astype(dtype[, order, casting, subok, copy])

Copy of the array, cast to a specified type.

broadcast_with(self, target)

Returns an array that is (NumPy) broadcastable with target.

clip(self[, minval, maxval, out])

Clip (limit) the values in an array.

combine_axes(self[, axes, sep, wildcard])

Combine several axes into one.

compact(self)

Detects and removes “useless” axes (ie axes for which values are constant over the whole axis)

copy(self)

Returns a copy of the array.

cumprod(self[, axis])

Returns the cumulative product of array elements.

cumsum(self[, axis])

Returns the cumulative sum of array elements along an axis.

describe(self, \*args, \*\*kwargs)

Descriptive summary statistics, excluding NaN values.

describe_by(self, \*args, \*\*kwargs)

Descriptive summary statistics, excluding NaN values, along axes or for groups.

diff(self[, axis, d, n, label])

Calculates the n-th order discrete difference along a given axis.

divnot0(self, other)

Divides array by other, but returns 0.0 where other is 0.

drop(self[, labels])

Return array without some labels or indices along an axis.

drop_labels(\*args, \*\*kwargs)

dump(self[, header, wide, value_name, …])

Dump array as a 2D nested list.

eq(self, other[, rtol, atol, nans_equal])

Compares self with another array element-wise and returns an array of booleans.

equals(self, other[, rtol, atol, …])

Compares self with another array and returns True if they have the same axes and elements, False otherwise.

expand(self[, target_axes, out, readonly])

Expands array to target_axes.

extend(self, axis, other)

Adds an array to self along an axis.

filter(self[, collapse])

Filters the array along the axes given as keyword arguments.

growth_rate(self[, axis, d, label])

Calculates the growth along a given axis.

ignore_labels(self[, axes])

Ignore labels from axes (replace those axes by “wildcard” axes).

indexofmax(self[, axis])

Returns indices of the maximum values along a given axis.

indexofmin(self[, axis])

Returns indices of the minimum values along a given axis.

indicesofsorted(self[, axis, ascending, kind])

Returns the indices that would sort this array.

insert(self, value[, before, after, pos, …])

Inserts value in array along an axis.

isin(self, test_values[, assume_unique, invert])

Computes whether each element of this array is in test_values.

items(self[, axes, ascending])

Returns a (label, value) view of the array along axes.

keys(self[, axes, ascending])

Returns a view on the array labels along axes.

labelofmax(self[, axis])

Returns labels of the maximum values along a given axis.

labelofmin(self[, axis])

Returns labels of the minimum values along a given axis.

labelsofsorted(self[, axis, ascending, kind])

Returns the labels that would sort this array.

max(*axes_and_groups[, out, skipna, keepaxes])

Get maximum of array elements along given axes/groups.

max_by(*axes_and_groups[, out, skipna, keepaxes])

Get maximum of array elements for the given axes/groups.

mean(*axes_and_groups[, dtype, out, skipna, …])

Computes the arithmetic mean.

mean_by(*axes_and_groups[, dtype, out, …])

Computes the arithmetic mean.

median(*axes_and_groups[, out, skipna, keepaxes])

Computes the arithmetic median.

median_by(*axes_and_groups[, out, skipna, …])

Computes the arithmetic median.

min(*axes_and_groups[, out, skipna, keepaxes])

Get minimum of array elements along given axes/groups.

min_by(*axes_and_groups[, out, skipna, keepaxes])

Get minimum of array elements for the given axes/groups.

nonzero(self)

Returns the indices of the elements that are non-zero.

percent(self, \*axes)

Returns an array with values given as percent of the total of all values along given axes.

percentile(q, *axes_and_groups[, out, …])

Computes the qth percentile of the data along the specified axis.

percentile_by(q, *axes_and_groups[, out, …])

Computes the qth percentile of the data for the specified axis.

posargmax(\*args, \*\*kwargs)

posargmin(\*args, \*\*kwargs)

posargsort(\*args, \*\*kwargs)

prepend(self, axis, value[, label])

Adds an array before self along an axis.

prod(*axes_and_groups[, dtype, out, skipna, …])

Computes the product of array elements along given axes/groups.

prod_by(*axes_and_groups[, dtype, out, …])

Computes the product of array elements for the given axes/groups.

ptp(*axes_and_groups[, out])

Returns the range of values (maximum - minimum).

ratio(self, \*axes)

Returns an array with all values divided by the sum of values along given axes.

rationot0(self, \*axes)

Returns a LArray with values array / array.sum(axes) where the sum is not 0, 0 otherwise.

reindex(self[, axes_to_reindex, new_axis, …])

Reorder and/or add new labels in axes.

rename(self[, renames, to, inplace])

Renames axes of the array.

reshape(self, target_axes)

Given a list of new axes, changes the shape of the array.

reshape_like(self, target)

Same as reshape but with an array as input.

reverse(self[, axes])

Reverse axes of an array

roll(self[, axis, n])

Rolls the cells of the array n-times to the right along axis.

set(self, value, \*\*kwargs)

Sets a subset of array to value.

set_axes(self[, axes_to_replace, new_axis, …])

Replace one, several or all axes of the array.

set_labels(self[, axis, labels, inplace])

Replaces the labels of one or several axes of the array.

shift(self, axis[, n])

Shifts the cells of the array n-times to the right along axis.

sort_axes(self[, axes, ascending])

Sorts axes of the array.

sort_axis(\*args, \*\*kwargs)

sort_values(self[, key, axis, ascending])

Sorts values of the array.

split_axes(self[, axes, sep, names, regex, …])

Split axes and returns a new array

split_axis(\*args, \*\*kwargs)

std(*axes_and_groups[, dtype, ddof, out, …])

Computes the sample standard deviation.

std_by(*axes_and_groups[, dtype, ddof, out, …])

Computes the sample standard deviation.

sum(*axes_and_groups[, dtype, out, skipna, …])

Computes the sum of array elements along given axes/groups.

sum_by(*axes_and_groups[, dtype, out, …])

Computes the sum of array elements for the given axes/groups.

to_clipboard(self, \*args, \*\*kwargs)

Sends the content of the array to clipboard.

to_csv(self, filepath[, sep, na_rep, wide, …])

Writes array to a csv file.

to_excel(self[, filepath, sheet, position, …])

Writes array in the specified sheet of specified excel workbook.

to_frame(self[, fold_last_axis_name, dropna])

Converts LArray into Pandas DataFrame.

to_hdf(self, filepath, key)

Writes array to a HDF file.

to_series(self[, name, dropna])

Converts LArray into Pandas Series.

to_stata(self, filepath_or_buffer, \*\*kwargs)

Writes array to a Stata .dta file.

transpose(self, \*args)

Reorder axes.

unique(self[, axes, sort, sep])

Returns unique values (optionally along axes)

values(self[, axes, ascending])

Returns a view on the values of the array along axes.

var(*axes_and_groups[, dtype, ddof, out, …])

Computes the unbiased variance.

var_by(*axes_and_groups[, dtype, ddof, out, …])

Computes the unbiased variance.

with_axes(\*args, \*\*kwargs)

with_total(*args[, op, label])

Add aggregated values (sum by default) along each axis.

Attributes

T

Reorder axes.

axes

data

df

Converts LArray into Pandas DataFrame.

dtype

Returns the type of the data of the array.

i

Allows selection of a subset using indices of labels.

iflat

Access the array by index as if it was flat (one dimensional) and all its axes were combined.

info

Describes a LArray (metadata + shape and labels for each axis).

ipoints

Allows selection of arbitrary items in the array based on their N-dimensional index.

item

memory_used

Returns the memory consumed by the array in human readable form.

meta

Returns metadata of the array.

nbytes

Returns the number of bytes used to store the array in memory.

ndim

Returns the number of dimensions of the array.

plot

Plots the data of the array into a graph (window pop-up).

points

Allows selection of arbitrary items in the array based on their N-dimensional label index.

series

Converts LArray into Pandas Series.

shape

Returns the shape of the array as a tuple.

size

Returns the number of elements in array.

title