Apply, reduce and group¶
Running functions over rows and columns, and grouping.
Apply¶
apply ¶
apply(func, by='row', keylist=None, **kwargs)
Apply a function to each row and collect the results in a new Daf.
Use it to convert one table into another, with a plain Python function that turns one row into the new row. This Daf is not changed. Only the new Daf gets rows.
The function gets a row and returns what to add to the new Daf:
- A dict adds one row.
- A list of dicts adds a row for each dict, so one row can become several.
- None, an empty dict or an empty list adds nothing, so the row is left out.
The new Daf takes its columns from the first row returned. A later dict is placed by column name, and a column it lacks is NULL. A key that the first row did not have is dropped without an error. So when the columns are known, have the function return all of them in every row. The new Daf has no keyfield.
With by='table' the function gets the whole Daf, and its result is returned
as it is. Use that to run any function on the table. by='col' is not
supported.
The function gets the rows as dicts, or as KeyedList objects if itermode is
keyedlist. The extra keyword arguments are passed on to it. To work on only
some rows, give the keylist, or select them first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
The function to apply. It takes a row, or the Daf, and the keyword arguments. |
required |
by
|
str
|
|
'row'
|
keylist
|
T_la | T_lota | None
|
Keys of the rows to include. All rows if None. This may be removed. |
None
|
**kwargs
|
Any
|
Keyword arguments passed on to the function. |
{}
|
Returns:
| Type | Description |
|---|---|
'Daf'
|
The new Daf, or the result of the function for |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
|
Examples:
Create a small Daf of orders, with the city they went to, a quantity and an amount:
>>> daf = Daf(cols=['city', 'qty', 'amount'], lol=[['Denver', 2, 25.0], ['Austin', 6, 24.0], ['Denver', 1, 30.5]])
>>> daf
| city | qty | amount |
| -----: | --: | -----: |
| Denver | 2 | 25.0 |
| Austin | 6 | 24.0 |
| Denver | 1 | 30.5 |
%% daf rows=3; cols=3; keyfield=''; name=''
Make a new table, with a row of new values for each row:
>>> daf.apply(lambda row: {'city': row['city'].upper(), 'amount_x2': row['amount'] * 2})
| city | amount_x2 |
| -----: | --------: |
| DENVER | 50.0 |
| AUSTIN | 48.0 |
| DENVER | 61.0 |
%% daf rows=3; cols=2; keyfield=''; name=''
Keep only some rows. A function that returns None drops the row:
>>> daf.apply(lambda row: row if row['amount'] > 24.5 else None)
| city | qty | amount |
| -----: | --: | -----: |
| Denver | 2 | 25.0 |
| Denver | 1 | 30.5 |
%% daf rows=2; cols=3; keyfield=''; name=''
Make several rows from one row, by returning a list:
>>> daf.apply(lambda row: [{'city': row['city'], 'part': 1}, {'city': row['city'], 'part': 2}] if row['qty'] == 2 else None)
| city | part |
| -----: | ---: |
| Denver | 1 |
| Denver | 2 |
%% daf rows=2; cols=2; keyfield=''; name=''
Extra keyword arguments go to the function:
>>> stock = Daf(cols=['id', 'qty'], keyfield='id', lol=[['a1', 3], ['b2', 10], ['c3', 7]])
>>> stock.apply(lambda row, factor: {'id': row['id'], 'qty': row['qty'] * factor}, factor=10)
| id | qty |
| -: | --: |
| a1 | 30 |
| b2 | 100 |
| c3 | 70 |
%% daf rows=3; cols=2; keyfield=''; name=''
Only the rows of some keys. They come in the order of the table, not of the list:
>>> stock.apply(lambda row: {'id': row['id']}, keylist=['c3', 'a1']).col('id')
['a1', 'c3']
With by='table', the function gets the whole Daf, and its result is returned as it is:
>>> stock.apply(lambda daf: len(daf), by='table')
3
apply_in_place ¶
apply_in_place(func, by='row', rowkeys=None, **kwargs)
Apply a function to each row and store the results in this Daf.
With by='row' the function gets each row as a dict. It must return a dict.
Its values are written back by column name. A column that the dict lacks
keeps its value, and a key that is not a column is ignored. The order of
the keys does not matter, and the row keeps its length.
With by='row_klist' the function gets each row as a
KeyedList. It changes the row and returns
nothing, so no dict is built for each row. The cost of row grows with the
number of columns, because each row is turned into a dict of all its columns.
The cost of row_klist does not. In a test that changed one column, the two
took the same time at about 6 columns. At 1,000 columns and 2,000 rows,
row_klist took 0.005 s and row took 0.245 s. With 3 columns and 200,000
rows, row was faster, 0.25 s against 0.30 s. Add or delete no keys in the
KeyedList, because that changes the length of the row, not the columns.
The key index is rebuilt when it is next needed. Use apply() to get a new Daf.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., T_ma | None]
|
The function to apply to each row. It takes a row and the keyword arguments. |
required |
by
|
str
|
|
'row'
|
rowkeys
|
T_la | T_lota | None
|
Keys of the rows to include. All rows if None. The Daf needs a keyfield. |
None
|
**kwargs
|
Any
|
Keyword arguments passed on to the function. |
{}
|
Returns:
| Type | Description |
|---|---|
'Daf'
|
This Daf, which has been changed. |
Raises:
| Type | Description |
|---|---|
ValueError
|
With |
NotImplementedError
|
|
Examples:
Create a small Daf of orders:
>>> daf = Daf(cols=['city', 'qty', 'amount'], lol=[['Denver', 2, 25.0], ['Austin', 6, 24.0], ['Denver', 1, 30.5]])
>>> daf
| city | qty | amount |
| -----: | --: | -----: |
| Denver | 2 | 25.0 |
| Austin | 6 | 24.0 |
| Denver | 1 | 30.5 |
%% daf rows=3; cols=3; keyfield=''; name=''
Add 1 to every amount, in place. With by='row', the function returns the changed row:
>>> daf.apply_in_place(lambda row: {**row, 'amount': row['amount'] + 1})
| city | qty | amount |
| -----: | --: | -----: |
| Denver | 2 | 26.0 |
| Austin | 6 | 25.0 |
| Denver | 1 | 31.5 |
%% daf rows=3; cols=3; keyfield=''; name=''
Set every amount to 0. With by='row_klist', the function changes the row and returns nothing:
>>> daf.apply_in_place(lambda row: row.__setitem__('amount', 0), by='row_klist')
| city | qty | amount |
| -----: | --: | -----: |
| Denver | 2 | 0 |
| Austin | 6 | 0 |
| Denver | 1 | 0 |
%% daf rows=3; cols=3; keyfield=''; name=''
Only the rows of some keys:
>>> stock = Daf(cols=['id', 'qty'], keyfield='id', lol=[['a1', 3], ['b2', 10], ['c3', 7]])
>>> stock.apply_in_place(lambda row: {'qty': row['qty'] * 2}, rowkeys=['b2', 'c3'])
| id | qty |
| -: | --: |
| a1 | 3 |
| b2 | 20 |
| c3 | 14 |
%% daf rows=3; cols=2; keyfield='id'; name=''
update_row ¶
update_row(row, da)
Update a row with the items of a dict, and return the row.
This is a small helper to use inside apply(), as in
daf.apply(lambda row: Daf.update_row(row, {'z': 0})). It is a static method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row
|
T_rma
|
The row, as a dict. It is changed. |
required |
da
|
T_da
|
The items to put in the row. |
required |
Returns:
| Type | Description |
|---|---|
T_rma
|
The same row. |
Examples:
Merge new values into a row, here a dict:
>>> Daf.update_row({'name': 'Ana Lopez'}, {'age': 34})
{'name': 'Ana Lopez', 'age': 34}
apply_colwise ¶
apply_colwise(target_col, func, *, default=0)
Compute one column from the other columns of each row, in place.
The function gets each row, as a dict, and returns the value for target_col.
If the function raises an error for a row, that row gets default. If
target_col is not a column, it is added at the right, and the rows are
filled with default first. Use it for a ratio or a total of other columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_col
|
str
|
The column to store the result in. |
required |
func
|
Callable[[T_da], Any]
|
A function that takes a row and returns the value. |
required |
default
|
Any
|
The value for a row where the function raises an error. |
0
|
Returns:
| Type | Description |
|---|---|
'Daf'
|
This Daf, which has been changed. |
Examples:
Create a small Daf of profit and cost. One cost is 0:
>>> daf = Daf(cols=['profit', 'cost'], lol=[[450, 1500], [120, 0]])
>>> daf
| profit | cost |
| -----: | ---: |
| 450 | 1500 |
| 120 | 0 |
%% daf rows=2; cols=2; keyfield=''; name=''
Add a column of profit over cost. A row where the function fails gets the default:
>>> daf.apply_colwise('margin', lambda row: row['profit'] / row['cost'], default=-1.0)
| profit | cost | margin |
| -----: | ---: | -----: |
| 450 | 1500 | 0.3 |
| 120 | 0 | -1.0 |
%% daf rows=2; cols=3; keyfield=''; name=''
apply_to_col ¶
apply_to_col(col, func, **kwargs)
Replace each value of a column by the result of a function, in place.
The function gets each value, and returns the new one. Keyword arguments are passed on to it.
All the new values are made before any is stored, so if the function raises, the column is
left as it was. Use apply_in_place() when the new value needs other columns of the row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
col
|
str
|
The column name. |
required |
func
|
Callable
|
A function that takes a value and returns the new value. |
required |
**kwargs
|
Any
|
Keyword arguments passed on to the function with each value. |
{}
|
Returns:
| Type | Description |
|---|---|
'Daf'
|
This Daf, which has been changed. |
Examples:
Create a small Daf of orders and their prices:
>>> daf = Daf(cols=['id', 'price'], lol=[['o1', 12.5], ['o2', 4.0]])
>>> daf
| id | price |
| -: | ----: |
| o1 | 12.5 |
| o2 | 4.0 |
%% daf rows=2; cols=2; keyfield=''; name=''
Double each price, in place:
>>> daf.apply_to_col('price', lambda value: value * 2)
| id | price |
| -: | ----: |
| o1 | 25.0 |
| o2 | 8.0 |
%% daf rows=2; cols=2; keyfield=''; name=''
Pass more arguments to the function as keywords:
>>> daf.apply_to_col('price', lambda value, factor: value * factor, factor=10)
| id | price |
| -: | ----: |
| o1 | 250.0 |
| o2 | 80.0 |
%% daf rows=2; cols=2; keyfield=''; name=''
apply_formulas ¶
apply_formulas(formulas_daf)
Fill cells from spreadsheet like formulas, in place.
formulas_daf is a Daf of the same shape. Each cell holds a Python
expression as text. An empty cell is skipped. The result of each expression is
stored in the same cell of this Daf. The formulas are evaluated again and again
until no cell changes, so a cell may use the result of another. If the cells
still change after 100 passes, it raises RuntimeError. This is a limit on the
passes, not a check for circular formulas. A circle of formulas that settles, such
as two cells that copy each other, raises nothing.
In a formula, $d is this Daf, $r is the row number of the cell and $c is
its column number. References are absolute unless you build them from $r and
$c. So sum($d[$r, :$c]) is the sum of the cells to the left in the same row.
Other examples:
$d[14,20]+$d[15,25] the sum of two cells
max(0,$d[($r-1),$c]) the cell above, but not below 0
$d[($r-1),$c] * 0.15 15 percent of the cell above
Warning: the formulas are run with eval(). Never use formulas from a source
you do not trust.
An error in a formula prints the cell and the formula, and is raised again. The
retmode of this Daf is restored, and the key index is rebuilt when it is next
needed. The cells that were already changed stay changed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formulas_daf
|
'Daf'
|
The formulas, with the same shape as this Daf. |
required |
Returns:
| Type | Description |
|---|---|
'Daf'
|
This Daf, which has been changed. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
The shapes differ, or the formulas never settle. |
Examples:
Create a small Daf of numbers, with an empty column and an empty row for totals:
>>> daf = Daf(cols=['A', 'B', 'C'], lol=[[1, 2, 0], [4, 5, 0], [7, 8, 0], [0, 0, 0]])
>>> daf
| A | B | C |
| -: | -: | -: |
| 1 | 2 | 0 |
| 4 | 5 | 0 |
| 7 | 8 | 0 |
| 0 | 0 | 0 |
%% daf rows=4; cols=3; keyfield=''; name=''
Make a Daf of formulas, the same shape. Each row is summed into column C, and each column into the last row:
>>> f_daf = Daf(cols=['A', 'B', 'C'], lol=[
... ['', '', 'sum($d[$r,:$c])'],
... ['', '', 'sum($d[$r,:$c])'],
... ['', '', 'sum($d[$r,:$c])'],
... ['sum($d[:$r,$c])', 'sum($d[:$r,$c])', 'sum($d[:$r,$c])']])
>>> f_daf
| A | B | C |
| --------------: | --------------: | --------------: |
| | | sum($d[$r,:$c]) |
| | | sum($d[$r,:$c]) |
| | | sum($d[$r,:$c]) |
| sum($d[:$r,$c]) | sum($d[:$r,$c]) | sum($d[:$r,$c]) |
%% daf rows=4; cols=3; keyfield=''; name=''
Apply the formulas, to fill in the totals:
>>> daf.apply_formulas(f_daf)
| A | B | C |
| -: | -: | -: |
| 1 | 2 | 3 |
| 4 | 5 | 9 |
| 7 | 8 | 15 |
| 12 | 15 | 27 |
%% daf rows=4; cols=3; keyfield=''; name=''
Reduce¶
reduce ¶
reduce(
func: Callable[..., Any],
by: Literal["row", "table", "sparse_row"] = "row",
cols: T_cs | None = None,
initial_da: T_ma | None = None,
indirect_col: str = "",
silent_error: bool = False,
**kwargs: Any,
) -> T_da
reduce(
func: Callable[..., Any],
*,
by: Literal["col"],
cols: T_cs | None = None,
initial_da: T_ma | None = None,
indirect_col: str = "",
silent_error: bool = False,
**kwargs: Any,
) -> T_la
reduce(
func: Callable[..., Any],
by: str = "row",
cols: T_cs | None = None,
initial_da: T_ma | None = None,
indirect_col: str = "",
silent_error: bool = False,
**kwargs: Any,
) -> T_ma | T_la
reduce(
func,
by="row",
cols=None,
initial_da=None,
indirect_col="",
silent_error=False,
**kwargs,
)
Combine all the rows into one result, using a function.
The function gets each row, and the result so far, and returns the new result.
A sum or a count is a reduction. With by='row', the function is called as
func(row, result, cols=cols, **kwargs). The result starts as 0 for each of
the columns, or as initial_da, which is changed in place. It returns a dict with
every column. Columns that are not in cols are empty. A key of the result that
is not a column of the Daf is dropped, so a reduction by row gives one value for
each column. For a result of another shape, use by='table', or a loop.
A plain sum, with sum_da() or sum_kl() and no other options, adds down each
column instead of across each row. That is about 5 times faster, and gives the same
totals. The function is then not called for each row.
With by='row', each row is a new KeyedList
that points at the row of the table. It reads like a dict, as in row['x'] or
row.get('x', 0). Writing to it changes the table. A function that needs a real
dict, for example to pass to json.dumps(), can call dict(row). A row that is
shorter or longer than the columns gets a KeyedList of a copy, padded with NULL
or cut to fit.
The standard functions are sum_kl() and count_values_kl(). They read the
KeyedList directly. Daf.sum_da and Daf.count_values_da are replaced by them
here, since they give the same results.
With by='col' the function gets each column as a list and the result so far
as a list. With by='table' it gets this Daf and cols, and its result is
returned as it is. With indirect_col, rows are read from the dict in that
column, and the result starts as initial_da or an empty dict. by='sparse_row' says the
same, and is not needed.
A plain sum of sparse rows, with sum_da() or sum_kl() and no other options,
chooses its loop once, from the first 100 rows. No key is tested inside the loop.
- With no
cols, every key of each row is added. - With
colsno longer than the mean number of keys in a row, each column is looked up in each row. - With longer
cols, every key is added, and the columns are picked at the end.
The totals are the same in each case. With cols, the result holds the columns
that some row has, in the order of cols. A column that no row has is left out,
not given as 0. To sum nearly every key, give no cols and pick the totals needed.
An error in the function stops the reduction. With silent_error=True the
row is skipped. The result may then be missing rows, with no sign of it. An
empty Daf gives an empty dict for by='row'.
To reduce part of the table, select the rows or columns first, or give cols.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
The reduction function. For example |
required |
by
|
str
|
|
'row'
|
cols
|
T_cs | None
|
The columns included in the reduction. All columns if None. |
None
|
initial_da
|
T_ma | None
|
The result to start from, instead of zeros. |
None
|
indirect_col
|
str
|
A column that holds a dict. Its dicts are the rows, as with |
''
|
silent_error
|
bool
|
If True, a row for which the function raises is skipped. |
False
|
**kwargs
|
Any
|
Keyword arguments passed on to the function. |
{}
|
Returns:
| Type | Description |
|---|---|
T_ma | T_la
|
A dict for |
Raises:
| Type | Description |
|---|---|
ValueError
|
|
NotImplementedError
|
|
Examples:
Create a small Daf of orders:
>>> daf = Daf(cols=['city', 'qty', 'amount'], lol=[['Denver', 2, 25.0], ['Austin', 6, 24.0], ['Denver', 1, 30.5]])
>>> daf
| city | qty | amount |
| -----: | --: | -----: |
| Denver | 2 | 25.0 |
| Austin | 6 | 24.0 |
| Denver | 1 | 30.5 |
%% daf rows=3; cols=3; keyfield=''; name=''
Total two columns:
>>> daf.reduce(Daf.sum_da, cols=['qty', 'amount'])
{'city': '', 'qty': 9, 'amount': 79.5}
A sparse sum, of the dict in each row:
>>> carts_daf = Daf(cols=['id', 'cart'], lol=[['c1', {'apples': 3, 'pears': 2}], ['c2', {'pears': 4, 'plums': 6}]])
>>> carts_daf
| id | cart |
| -: | ------------------------: |
| c1 | {'apples': 3, 'pears': 2} |
| c2 | {'pears': 4, 'plums': 6} |
%% daf rows=2; cols=2; keyfield=''; name=''
>>> carts_daf.reduce(Daf.sum_da, by='sparse_row', indirect_col='cart')
{'apples': 3, 'pears': 6, 'plums': 6}
>>> carts_daf.reduce(Daf.sum_da, by='sparse_row', indirect_col='cart', cols=['plums', 'pears'])
{'plums': 6, 'pears': 6}
Start from a result other than zeros, with initial_da:
>>> daf.reduce(Daf.sum_da, cols=['qty'], initial_da={'city': '', 'qty': 100, 'amount': ''})
{'city': '', 'qty': 109, 'amount': ''}
sum_da adds only numbers. Text in a column is skipped, with no error:
>>> mixed = Daf(cols=['id', 'qty'], lol=[['o1', 3], ['o2', 'unknown'], ['o3', 7]])
>>> mixed.reduce(Daf.sum_da, cols=['qty'])
{'id': '', 'qty': 10}
A function of your own that fails on a row stops the reduction. With silent_error=True, that
row is skipped:
>>> def add_qty(row, result, cols=None):
... result['qty'] = result['qty'] + row['qty']
... return result
>>> mixed.reduce(add_qty, cols=['qty'])
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> mixed.reduce(add_qty, cols=['qty'], silent_error=True)
{'id': '', 'qty': 10}
With by='col', the function gets each column as a list, and the result so far as a list.
Here it counts the different values in each column:
>>> daf.reduce(lambda col, result: result + [len(set(col))], by='col')
[2, 3, 3]
With by='table', the function gets the Daf and cols, and its result is returned as it is:
>>> daf.reduce(lambda table, cols: {'rows': len(table)}, by='table')
{'rows': 3}
daf_sum ¶
daf_sum(by='row', cols=None, indirect_col='', **kwargs)
Add up the columns, using reduce() and sum_kl().
Cells that cannot be added, such as text, are skipped with no error. See
sum_kl for what that means for a bad number. A column
that is not in cols is empty in the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
by
|
str
|
How the function is applied. See |
'row'
|
cols
|
T_cs | None
|
The columns to sum. All if None. |
None
|
indirect_col
|
str
|
A column that holds a dict. Required for |
''
|
**kwargs
|
Any
|
Keyword arguments passed on to |
{}
|
Returns:
| Type | Description |
|---|---|
T_da
|
A dict with a total for each column. |
Examples:
Create a small Daf of orders:
>>> daf = Daf(cols=['city', 'qty', 'amount'], lol=[['Denver', 2, 25.0], ['Austin', 6, 24.0], ['Denver', 1, 30.5]])
>>> daf
| city | qty | amount |
| -----: | --: | -----: |
| Denver | 2 | 25.0 |
| Austin | 6 | 24.0 |
| Denver | 1 | 30.5 |
%% daf rows=3; cols=3; keyfield=''; name=''
Total one column:
>>> daf.daf_sum(cols=['amount'])
{'city': '', 'qty': '', 'amount': 79.5}
Sum the dict in each row, with by='sparse_row':
>>> customers = Daf(cols=['id', 'cart'], lol=[
... ['c1', {'apples': 1}], ['c2', {'pears': 1}], ['c3', {'apples': 1, 'pears': 1}]])
>>> customers.daf_sum(by='sparse_row', indirect_col='cart')
{'apples': 2, 'pears': 2}
sum ¶
sum(colnames_ls=None)
Total the columns, and return a dict of the totals.
Each total starts as a float. Empty cells are skipped. Text that is a number,
such as '2', is converted and added. Other text raises ValueError, so give
colnames_ls to leave those columns out.
reduce(Daf.sum_da) adds differently. It skips every value that is not a number,
including the text '2', and never raises. So the two can give different totals
for a column that holds text. See the example. The totals are converted to the
dtypes, if the Daf has them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
colnames_ls
|
T_cs | None
|
The columns to total. All if None. Name only the columns that hold numbers. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
A dict that maps each column name to its total. |
Raises:
| Type | Description |
|---|---|
ValueError
|
A cell cannot be converted to a number. |
Examples:
Total every column of a small Daf of orders:
>>> Daf(cols=['qty', 'amount'], lol=[[2, 25.0], [6, 24.0]]).sum()
{'qty': 8.0, 'amount': 49.0}
A column with text of digits and an empty cell. sum() converts the text '6' and adds it.
reduce(Daf.sum_da) skips it, so the totals differ:
>>> mixed = Daf(cols=['qty'], lol=[[2], ['6'], [''], [1.5]])
>>> mixed
| qty |
| --: |
| 2 |
| 6 |
| |
| 1.5 |
%% daf rows=4; cols=1; keyfield=''; name=''
>>> mixed.sum(['qty']), mixed.reduce(Daf.sum_da, cols=['qty'])
({'qty': 9.5}, {'qty': 3.5})
sum_np ¶
sum_np(colnames_ls=None)
Total the columns with NumPy, and return a dict of the totals.
This needs NumPy. A blank, None or NaN cell counts as 0, as it does in sum().
Use colnames_ls to pass only the columns that hold numbers. A column that holds
text raises TypeError and names the column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
colnames_ls
|
T_cs | None
|
The columns to total. All if None. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
A dict that maps each column name to its total. An empty Daf gives an empty dict. |
Raises:
| Type | Description |
|---|---|
KeyError
|
A name in |
TypeError
|
A column holds text or other values that cannot be added. |
Examples:
Total every column with numpy:
>>> Daf(cols=['qty', 'amount'], lol=[[2, 25.0], [6, 24.0]]).sum_np()
{'qty': 8, 'amount': 49.0}
>>> Daf(cols=['qty', 'amount'], lol=[[2, 25.0], [6, '']]).sum_np()
{'qty': 8, 'amount': 25.0}
Only some columns:
>>> Daf(cols=['qty', 'amount'], lol=[[2, 25.0], [6, 24.0]]).sum_np(['amount'])
{'amount': 49.0}
sum_kl ¶
sum_kl(
row_kl,
reduction_da,
*,
cols=None,
astype=None,
is_sparse=False,
diagnose=False,
)
Add the values of a row to a running total. This is the standard sum for reduce().
It gives the same result as sum_da(), and is faster on the rows that
reduce() gives it, which are KeyedList
objects. It reads the values of the row directly, instead of looking up each
one through the row. reduce() uses it in place of sum_da(), so a caller
that passes Daf.sum_da gets this speed too.
A value that cannot be added, such as text or an empty cell, is skipped, with no
error. That keeps the sum fast, and lets a sum of all columns pass over the text
columns. But a bad number, such as abc in an int column, is then left out of
the total with no sign. Call check_schema() first to find such cells.
A column that the row does not have adds 0. With astype or is_sparse, or a
row that is not a KeyedList, the work is passed to sum_da().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row_kl
|
Any
|
The current row. |
required |
reduction_da
|
T_rma
|
The running total. It is changed. |
required |
cols
|
T_cs | None
|
The columns to add. All if None. |
None
|
astype
|
Type | None
|
A type to convert each value to before adding: int, float or str. |
None
|
is_sparse
|
bool
|
If True, loop over the row's own keys, which is faster when a row holds few of many columns. |
False
|
diagnose
|
bool
|
Not used. |
False
|
Returns:
| Type | Description |
|---|---|
T_rma
|
The running total. |
Examples:
Add the values of a KeyedList row to a running total. Text is skipped:
>>> from daffodil.keyedlist import KeyedList
>>> Daf.sum_kl(KeyedList(['qty', 'note'], [2, 'rush']), {'qty': 10, 'note': 0}, cols=['qty', 'note'])
{'qty': 12, 'note': 0}
astype and is_sparse work as in sum_da(), which has an
example of each.
sum_da ¶
sum_da(
row_da,
reduction_da,
*,
cols=None,
astype=None,
is_sparse=False,
diagnose=False,
)
Add the values of a row to a running total. Use it with reduce().
This is a static method. A value that cannot be added, such as text or an empty
cell, is skipped with no error. See sum_kl for what that
means for a bad number. The total is changed and returned. With astype, each value is
first converted to that type. reduce() uses sum_kl() in its place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row_da
|
T_ma
|
The current row. |
required |
reduction_da
|
T_rma
|
The running total. It is changed. |
required |
cols
|
T_cs | None
|
The columns to add. All if None. |
None
|
astype
|
Type | None
|
A type to convert each value to before adding: int, float or str. |
None
|
is_sparse
|
bool
|
If True, loop over the row's own keys, which is faster when a row holds few of many columns. |
False
|
diagnose
|
bool
|
Not used. |
False
|
Returns:
| Type | Description |
|---|---|
T_rma
|
The running total. |
Examples:
Add the values of a row to a running total:
>>> Daf.sum_da({'qty': 2, 'amount': 25.0}, {'qty': 10, 'amount': 100.0}, cols=['qty', 'amount'])
{'qty': 12, 'amount': 125.0}
Convert each value before adding, with astype. A value that cannot be converted is skipped:
>>> Daf.sum_da({'qty': '3', 'note': 'rush'}, {'qty': 10, 'note': 0}, cols=['qty', 'note'], astype=int)
{'qty': 13, 'note': 0}
>>> Daf.sum_da({'qty': '3', 'amount': '12.5'}, {'qty': 10, 'amount': 100.0}, astype=float)
{'qty': 13.0, 'amount': 112.5}
With is_sparse=True, the loop runs over the row's own keys, not over cols. That is faster
when a row holds few of many columns. The total is the same:
>>> Daf.sum_da({'apples': 2}, {'apples': 3, 'pears': 5}, cols=['apples', 'pears'], is_sparse=True)
{'apples': 5, 'pears': 5}
diff_da ¶
diff_da(d1_da, d2_da, keys=None)
Subtract one dict from another, for the keys you name.
This is a static method. A key that is missing, or an empty value, counts as 0. Keys that you do not name are left out. The values must be numbers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d1_da
|
T_ma
|
The first dict. |
required |
d2_da
|
T_ma
|
The dict to subtract. |
required |
keys
|
T_ls | str | None
|
The key or keys to include. If None, the result is empty. |
None
|
Returns:
| Type | Description |
|---|---|
T_da
|
A dict of the differences. |
Examples:
Subtract one row from another, in the key columns given:
>>> Daf.diff_da({'qty': 5, 'city': 'Denver'}, {'qty': 2, 'city': 'Austin'}, keys=['qty'])
{'qty': 3}
count_values_kl ¶
count_values_kl(
row_kl, reduction_da, cols, *, omit_nulls=False
)
Add one row to running counts of the values in each column. This is the standard count for reduce().
It gives the same result as count_values_da(), and is faster on the rows that
reduce() gives it, which are KeyedList
objects. reduce() uses it in place of count_values_da().
A cell that holds a dict is passed to count_values_da(), one column at a
time, as is a row that is not a KeyedList. A value that cannot be a dict key,
such as a list, raises TypeError. Leave a column of lists out with cols.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row_kl
|
Any
|
The current row. |
required |
reduction_da
|
T_da
|
The running counts. They are changed. |
required |
cols
|
T_cs
|
The columns to count. |
required |
omit_nulls
|
bool
|
If True, empty cells are not counted. |
False
|
Returns:
| Type | Description |
|---|---|
T_da
|
The running counts. |
Raises:
| Type | Description |
|---|---|
TypeError
|
A cell holds a value that cannot be counted, such as a list. |
Examples:
Count the value of a KeyedList row into a running count:
>>> from daffodil.keyedlist import KeyedList
>>> Daf.count_values_kl(KeyedList(['city'], ['Denver']), {'city': {'Denver': 2}}, ['city'])
{'city': {'Denver': 3}}
Empty cells are counted as '', unless omit_nulls=True:
>>> row = KeyedList({'city': ''})
>>> Daf.count_values_kl(row, {'city': {}}, cols=['city'])
{'city': {'': 1}}
>>> Daf.count_values_kl(row, {'city': {}}, cols=['city'], omit_nulls=True)
{'city': {}}
count_values_da ¶
count_values_da(
row_da, reduction_da, cols, *, omit_nulls=False
)
Add one row to running counts of the values in each column. Use it with reduce().
This is a static method. The counts are a dict that maps each column to a dict
of value and count. The counts are changed and returned. A cell that holds a
dict is added to the counts for that column with sum_da(). Use omit_nulls
to skip empty cells.
A value that cannot be a dict key, such as a list, raises TypeError. There is
no check for it beforehand, to keep the count fast. Leave a column of lists
out with cols.
reduce() uses count_values_kl() in its place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row_da
|
T_ma
|
The current row. |
required |
reduction_da
|
T_da
|
The running counts. They are changed. |
required |
cols
|
T_cs
|
The columns to count. |
required |
omit_nulls
|
bool
|
If True, empty cells are not counted. |
False
|
Returns:
| Type | Description |
|---|---|
T_da
|
The running counts. |
Raises:
| Type | Description |
|---|---|
TypeError
|
A cell holds a value that cannot be counted, such as a list. |
Examples:
Count the value of a row into a running count:
>>> Daf.count_values_da({'city': 'Denver'}, {}, ['city'])
{'city': {'Denver': 1}}
Empty cells are counted as '', unless omit_nulls=True:
>>> Daf.count_values_da({'city': ''}, {'city': {}}, cols=['city'])
{'city': {'': 1}}
>>> Daf.count_values_da({'city': ''}, {'city': {}}, cols=['city'], omit_nulls=True)
{'city': {}}
sum_dodis ¶
sum_dodis(this_dodi, accum_dodi)
Add one dict of dicts of numbers into another, in place.
This is a static method. For each key, the numbers of the inner dicts are
added with sum_da(). A key that is new is stored as it is, not copied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
this_dodi
|
T_dodi
|
The counts to add. |
required |
accum_dodi
|
T_dodi
|
The running totals. They are changed. |
required |
Examples:
Add a dict of counts into a running total of counts, in place:
>>> total = {'cart': {'apples': 2, 'pears': 1}}
>>> Daf.sum_dodis({'cart': {'apples': 1}}, total)
>>> total
{'cart': {'apples': 3, 'pears': 1}}
Group¶
groupby ¶
groupby(
colname="", colnames=None, omit_nulls=False, cols=None
)
Split the Daf into several Daf instances, one for each value of a column.
The result is a KeyedList of Daf, a klodaf. It
works as a dict: groups[key], keys(), items(), len() and in. Its values
also have positions, so groups.values()[0] is the first group. Each key is a value
found in the column, in the order first seen. Each value is a Daf of the rows that
have it, with all columns, or only the columns in cols.
With all columns, a group holds the same row lists as this Daf. Nothing is copied,
so it is fast and uses little memory. But a cell changed in a group is also changed
here. To keep this Daf as it is, group a copy made with copy(level='editable').
Adding a column to a group makes new rows, and this Daf does not get the column.
Use cols when you need only a few of many columns. The rows are then new lists of
the columns kept. The other columns are never copied, and this Daf is not changed.
It is much faster for a wide table.
With several columns, as a list, the keys are tuples of their values. See
groupby_cols(). With omit_nulls, rows that have an empty value in the
column are left out.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
colname
|
str
|
The column to group by. |
''
|
colnames
|
T_ls | None
|
Several columns to group by. Use this or |
None
|
omit_nulls
|
bool
|
If True, leave out rows that have an empty value. |
False
|
cols
|
T_cs | None
|
The columns that each group keeps, in this order. If None, all columns. A keyfield is kept only if all its columns are kept. |
None
|
Returns:
| Type | Description |
|---|---|
T_klodaf
|
A KeyedList that maps each value, or tuple of values, to a Daf. |
Raises:
| Type | Description |
|---|---|
KeyError
|
The group column, or a name in |
Examples:
Create a small Daf of orders:
>>> daf = Daf(cols=['city', 'qty', 'amount'], lol=[['Denver', 2, 25.0], ['Austin', 6, 24.0], ['Denver', 1, 30.5]])
>>> daf
| city | qty | amount |
| -----: | --: | -----: |
| Denver | 2 | 25.0 |
| Austin | 6 | 24.0 |
| Denver | 1 | 30.5 |
%% daf rows=3; cols=3; keyfield=''; name=''
Group the orders by city. Each group is a Daf:
>>> groups = daf.groupby('city')
>>> list(groups)
['Denver', 'Austin']
>>> groups['Denver']
| city | qty | amount |
| -----: | --: | -----: |
| Denver | 2 | 25.0 |
| Denver | 1 | 30.5 |
%% daf rows=2; cols=3; keyfield=''; name=''
Keep only some columns in each group, with cols:
>>> daf.groupby('city', cols=['amount'])['Denver']
| amount |
| -----: |
| 25.0 |
| 30.5 |
%% daf rows=2; cols=1; keyfield=''; name=''
Group by two columns together. Each key is then a tuple:
>>> customers = Daf(cols=['id', 'city', 'plan'], keyfield='id', lol=[
... ['c1', 'Denver', 'basic'], ['c2', 'Denver', 'pro'], ['c3', 'Austin', 'basic'], ['c4', 'Denver', 'basic'], ['c5', '', 'pro']])
>>> list(customers.groupby(colnames=['city', 'plan']).keys())
[('Denver', 'basic'), ('Denver', 'pro'), ('Austin', 'basic'), ('', 'pro')]
Leave out the rows with an empty value in the column:
>>> list(customers.groupby('city').keys())
['Denver', 'Austin', '']
>>> list(customers.groupby('city', omit_nulls=True).keys())
['Denver', 'Austin']
groupby_cols ¶
groupby_cols(colnames, cols=None)
Split the Daf by the values of several columns.
The result is a KeyedList of Daf, as groupby() gives. Each key is a tuple of the
values in the columns, even for one column. Each value is a Daf of the rows that have them, with all
columns, or only the columns in cols. With all columns, the rows are not
copied. With cols, they are new lists of the columns kept, and the other
columns are never copied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
colnames
|
T_ls
|
The columns to group by. |
required |
cols
|
T_cs | None
|
The columns that each group keeps, in this order. If None, all columns. A keyfield is kept only if all its columns are kept. |
None
|
Returns:
| Type | Description |
|---|---|
T_klodaf
|
A KeyedList that maps each tuple of values to a Daf. |
Raises:
| Type | Description |
|---|---|
KeyError
|
A name in |
Examples:
Create a small Daf of orders:
>>> daf = Daf(cols=['city', 'qty', 'amount'], lol=[['Denver', 2, 25.0], ['Austin', 6, 24.0], ['Denver', 1, 30.5]])
>>> daf
| city | qty | amount |
| -----: | --: | -----: |
| Denver | 2 | 25.0 |
| Austin | 6 | 24.0 |
| Denver | 1 | 30.5 |
%% daf rows=3; cols=3; keyfield=''; name=''
Group the orders by city. Each key is a tuple:
>>> list(daf.groupby_cols(['city']))
[('Denver',), ('Austin',)]
Keep only some columns in each group, with cols:
>>> daf.groupby_cols(['city'], cols=['amount'])[('Denver',)]
| amount |
| -----: |
| 25.0 |
| 30.5 |
%% daf rows=2; cols=1; keyfield=''; name=''
groupby_repeated_cols ¶
groupby_repeated_cols(colnames, exclude=None)
Group the rows whose key repeats, and leave out every key that appears only once.
Use it to find rows that share a key of one or more columns, such as two images with the
same hash. Each group holds every row with its key, in table order, so its first row is
the earliest. The groups come in the order of their first rows. An empty result means no
key repeats. groupby_cols() gives every key, but here a key seen once makes no group.
Each key is a tuple of the values, even for one column. Python NULL is a value like any
other, so two rows with a blank in the same key column match. To keep a row from matching
at all, as when its first key column is 0 or blank, name those values in exclude. A row
is left out if any column named there holds one of its values. The values must match the
stored type. A Daf read from a CSV file holds text, such as '0', until apply_dtypes().
The groups share their rows with this Daf, as in groupby_cols(). SqlDaf has the same
method, which finds the keys in SQL and gives each group as a selection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
colnames
|
T_ls
|
The columns that make the key of a row. |
required |
exclude
|
Dict[str, T_la] | None
|
For each column named, the values that keep a row out of every group. |
None
|
Returns:
| Type | Description |
|---|---|
T_klodaf
|
A KeyedList that maps each repeated key, a tuple of values, to a Daf of its rows. |
Raises:
| Type | Description |
|---|---|
KeyError
|
A name in |
Examples:
Create a small Daf of files, with a checksum and a size. A checksum of 0 means none was made:
>>> daf = Daf(cols=['file_id', 'checksum', 'size'],
... lol=[['f1', 7301, 2048], ['f2', 0, 2048], ['f3', 7301, 2048], ['f4', 0, 2048], ['f5', 8842, 2048]])
>>> daf
| file_id | checksum | size |
| ------: | -------: | ---: |
| f1 | 7301 | 2048 |
| f2 | 0 | 2048 |
| f3 | 7301 | 2048 |
| f4 | 0 | 2048 |
| f5 | 8842 | 2048 |
%% daf rows=5; cols=3; keyfield=''; name=''
Find the files whose checksum and size repeat, leaving out the files with no checksum:
>>> repeats_klodaf = daf.groupby_repeated_cols(['checksum', 'size'], exclude={'checksum': [0, '']})
>>> list(repeats_klodaf.keys())
[(7301, 2048)]
>>> repeats_klodaf[(7301, 2048)].col('file_id')
['f1', 'f3']
group_where ¶
group_where(where, *, indirect_col=None)
Group the rows by the result of a function.
The function gets each row and returns a key, or a list of keys, or None. A row
goes into the group of each key. A list of keys puts the same row in several
groups. None leaves the row out. The groups are Daf instances in a KeyedList, as
groupby() gives. The rows are not copied, so changing a cell in a group changes it
here too.
To split the rows in two, have the function return True or False. A side with no
rows then has no group, so read each side with .get().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
where
|
Callable[[Any], Any]
|
The function. It gets a row and returns None, a key, or an iterable of keys. |
required |
indirect_col
|
str | None
|
A column that holds a dict, to read names that are not columns from. |
None
|
Returns:
| Type | Description |
|---|---|
T_klodaf
|
A KeyedList that maps each key to a Daf. |
Examples:
Create a small Daf of orders:
>>> daf = Daf(cols=['city', 'qty', 'amount'], lol=[['Denver', 2, 25.0], ['Austin', 6, 24.0], ['Denver', 1, 30.5]])
>>> daf
| city | qty | amount |
| -----: | --: | -----: |
| Denver | 2 | 25.0 |
| Austin | 6 | 24.0 |
| Denver | 1 | 30.5 |
%% daf rows=3; cols=3; keyfield=''; name=''
Put each order in two groups, its city and 'all'. The function returns the groups of a row:
>>> groups = daf.group_where(lambda row: [row['city'], 'all'])
>>> {key: len(group) for key, group in groups.items()}
{'Denver': 2, 'all': 3, 'Austin': 1}
With indirect_col, the function reads the keys of the dict in that column as columns. Here
each cart that has apples goes into one group:
>>> customers = Daf(cols=['id', 'cart'], lol=[
... ['c1', {'apples': 1}], ['c2', {'pears': 1}], ['c3', {'apples': 1, 'pears': 1}]])
>>> groups = customers.group_where(lambda row: 'apples' if row['apples'] else None, indirect_col='cart')
>>> groups['apples'].col('id')
['c1', 'c3']
groupby_cols_reduce ¶
groupby_cols_reduce(
groupby_colnames,
func,
by="row",
reduce_cols=None,
diagnose=False,
**kwargs,
)
Group the rows by several columns and reduce each group to one row.
Use it to total the numbers for each combination of a few identifying columns.
The result has one row for each combination. It has the group columns first and
then the reduce_cols. It has no keyfield. For each group, func is used as in
reduce().
With reduce_cols, and by of row or sparse_row, each group holds only those
columns, and the indirect_col if there is one. A function that reads another
column of the row will not find it.
For example, group by gender, religion and zip code, and sum the counts of several causes in each group. The number of rows is the number of combinations that occur.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
groupby_colnames
|
T_ls
|
The columns that identify a group. |
required |
func
|
Callable[..., Any]
|
The reduction function. See |
required |
by
|
str
|
|
'row'
|
reduce_cols
|
T_la | None
|
The columns to reduce. |
None
|
diagnose
|
bool
|
If True, print the progress with a timestamp: the grouping, the number of groups, and the first groups. |
False
|
**kwargs
|
Any
|
Keyword arguments passed on to the function. |
{}
|
Returns:
| Type | Description |
|---|---|
'Daf'
|
The Daf with one row for each group. |
Examples:
Create a small Daf of orders:
>>> daf = Daf(cols=['city', 'qty', 'amount'], lol=[['Denver', 2, 25.0], ['Austin', 6, 24.0], ['Denver', 1, 30.5]])
>>> daf
| city | qty | amount |
| -----: | --: | -----: |
| Denver | 2 | 25.0 |
| Austin | 6 | 24.0 |
| Denver | 1 | 30.5 |
%% daf rows=3; cols=3; keyfield=''; name=''
Total the amount for each city. Only the column grouped by and the totals are kept:
>>> daf.groupby_cols_reduce(['city'], Daf.sum_da, reduce_cols=['amount'])
| city | amount |
| -----: | -----: |
| Denver | 55.5 |
| Austin | 24.0 |
%% daf rows=2; cols=2; keyfield=''; name=''
groupby_reduce ¶
groupby_reduce(
colname,
func,
by="row",
reduce_cols=None,
diagnose=False,
**kwargs,
)
Group the rows by one column and reduce each group to one row.
The result has one row for each value in the column, and its keyfield is that
column. The columns in reduce_cols hold the reduced values. Other columns are
empty. For each group, func is used as in reduce().
With by='sparse_row', the result stays concise. It has two columns: this column,
and the column named by indirect_col. That cell holds the dict of the group, with
only the keys found in the group. Each group's dict may have other keys. With
reduce_cols and a plain sum, each dict holds the columns its group has, in the
order of reduce_cols, as reduce() describes.
With reduce_cols, and by of row or sparse_row, each group holds only those
columns, and the indirect_col if there is one. The other columns are never copied,
which is much faster for a wide table. A function that reads another column of the
row will not find it. The result still has all the columns of this Daf, and the
columns that are not reduced are empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
colname
|
str
|
The column to group by. |
required |
func
|
Callable[..., Any]
|
The reduction function. See |
required |
by
|
str
|
How the function is applied. See |
'row'
|
reduce_cols
|
T_cs | None
|
The columns to reduce. All except |
None
|
diagnose
|
bool
|
If True, print the progress with a timestamp: the grouping, the number of groups, and the first groups. |
False
|
**kwargs
|
Any
|
Keyword arguments passed on to the function. |
{}
|
Returns:
| Type | Description |
|---|---|
'Daf'
|
The Daf with one row for each group. |
Examples:
Create a small Daf of orders:
>>> daf = Daf(cols=['city', 'qty', 'amount'], lol=[['Denver', 2, 25.0], ['Austin', 6, 24.0], ['Denver', 1, 30.5]])
>>> daf
| city | qty | amount |
| -----: | --: | -----: |
| Denver | 2 | 25.0 |
| Austin | 6 | 24.0 |
| Denver | 1 | 30.5 |
%% daf rows=3; cols=3; keyfield=''; name=''
Total the amount for each city:
>>> daf.groupby_reduce('city', Daf.sum_da, reduce_cols=['amount'])
| city | qty | amount |
| -----: | --: | -----: |
| Denver | | 55.5 |
| Austin | | 24.0 |
%% daf rows=2; cols=3; keyfield='city'; name=''
For sparse rows, give indirect_col. Each group's dicts are added into one dict, in one cell:
>>> customers = Daf(cols=['id', 'city', 'cart'], lol=[
... ['c1', 'Denver', {'apples': 1}], ['c2', 'Denver', {'pears': 1}], ['c3', 'Austin', {'apples': 1, 'pears': 1}]])
>>> customers.groupby_reduce('city', Daf.sum_da, indirect_col='cart')
| city | cart |
| -----: | ------------------------: |
| Denver | {'apples': 1, 'pears': 1} |
| Austin | {'apples': 1, 'pears': 1} |
%% daf rows=2; cols=2; keyfield='city'; name=''
multi_groupby ¶
multi_groupby(
groupby_colnames, colnames=None, omit_nulls=False
)
Group the rows by each of several columns, one column at a time.
The result is a dict of KeyedLists. The first key is the column. The second key is a
value in that column. Each innermost value is a Daf of the rows that have it,
with all columns, or only the columns in colnames.
This is not a grouping by combinations. Use groupby_cols() for that.
The groups are not reduced.
With all columns, the groups hold the same row lists as this Daf, as in
groupby(). A cell changed in a group is also changed here, and in the groups of
the other columns. To keep this Daf as it is, group a copy made with
copy(level='editable').
Use colnames when you need only a few of many columns. The rows are then new lists
of the columns kept. The other columns are never copied, and this Daf is not changed.
It is much faster for a wide table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
groupby_colnames
|
T_cs
|
The columns to group by, one at a time. |
required |
colnames
|
T_cs | None
|
The columns that each group keeps, in this order. If None, all columns. A keyfield is kept only if all its columns are kept. |
None
|
omit_nulls
|
bool
|
If True, leave out rows that have an empty value. |
False
|
Returns:
| Type | Description |
|---|---|
Dict[str, T_klodaf]
|
A dict that maps each column to a KeyedList of value and Daf. |
Raises:
| Type | Description |
|---|---|
KeyError
|
A group column, or a name in |
Examples:
Create a small Daf of orders:
>>> daf = Daf(cols=['city', 'qty', 'amount'], lol=[['Denver', 2, 25.0], ['Austin', 6, 24.0], ['Denver', 1, 30.5]])
>>> daf
| city | qty | amount |
| -----: | --: | -----: |
| Denver | 2 | 25.0 |
| Austin | 6 | 24.0 |
| Denver | 1 | 30.5 |
%% daf rows=3; cols=3; keyfield=''; name=''
Group the orders by city, and separately by quantity:
>>> groups = daf.multi_groupby(['city', 'qty'])
>>> list(groups), list(groups['city'])
(['city', 'qty'], ['Denver', 'Austin'])
Keep only some columns in each group:
>>> daf.multi_groupby(['city'], colnames=['amount'])['city']['Denver']
| amount |
| -----: |
| 25.0 |
| 30.5 |
%% daf rows=2; cols=1; keyfield=''; name=''
Leave out the rows with an empty value, in each grouping:
>>> customers = Daf(cols=['id', 'city', 'plan'], lol=[
... ['c1', 'Denver', 'basic'], ['c2', 'Denver', ''], ['c3', '', 'basic']])
>>> {name: list(groups.keys()) for name, groups in customers.multi_groupby(['city', 'plan'], omit_nulls=True).items()}
{'city': ['Denver'], 'plan': ['basic']}
multi_groupby_reduce ¶
multi_groupby_reduce(
colnames,
func,
by="row",
reduce_cols=None,
diagnose=False,
**kwargs,
)
Group by each of several columns and reduce each group to one row.
This is multi_groupby() followed by groupby_reduce() for each column. The
result is a dict. Each key is a column. Each value is a Daf with one row for
each value of that column, and that column as its keyfield.
With reduce_cols, and by of row or sparse_row, each group holds only those
columns, and the indirect_col if there is one. A function that reads another
column of the row will not find it. The result still has all the columns of this
Daf, and the columns that are not reduced are empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
colnames
|
T_cs
|
The columns to group by, one at a time. |
required |
func
|
Callable[..., Any]
|
The reduction function. See |
required |
by
|
str
|
|
'row'
|
reduce_cols
|
T_cs | None
|
The columns to reduce. |
None
|
diagnose
|
bool
|
If True, print the progress with a timestamp: the grouping, the number of groups, and the first groups. |
False
|
**kwargs
|
Any
|
Keyword arguments passed on to the function. |
{}
|
Returns:
| Type | Description |
|---|---|
Dict[str, 'Daf']
|
A dict that maps each column to its Daf. |
Examples:
Create a small Daf of orders:
>>> daf = Daf(cols=['city', 'qty', 'amount'], lol=[['Denver', 2, 25.0], ['Austin', 6, 24.0], ['Denver', 1, 30.5]])
>>> daf
| city | qty | amount |
| -----: | --: | -----: |
| Denver | 2 | 25.0 |
| Austin | 6 | 24.0 |
| Denver | 1 | 30.5 |
%% daf rows=3; cols=3; keyfield=''; name=''
Total the amount for each city:
>>> daf.multi_groupby_reduce(['city'], Daf.sum_da, reduce_cols=['amount'])['city']
| city | qty | amount |
| -----: | --: | -----: |
| Denver | | 55.5 |
| Austin | | 24.0 |
%% daf rows=2; cols=3; keyfield='city'; name=''
reduce_dodaf_to_daf ¶
reduce_dodaf_to_daf(
colname,
func,
grouped_dodaf,
reduce_cols=None,
diagnose=False,
all_cols=None,
**kwargs,
)
Reduce each Daf in a dict to one row, and join the rows in a Daf.
This is the second half of groupby_reduce(). The dict maps the values of a
column to Daf instances. Each Daf is reduced with reduce(). The value is
stored in the column colname of the row. The result has colname as its keyfield.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
colname
|
str
|
The column that holds the value of each group. |
required |
func
|
Callable[..., Any]
|
The reduction function. See |
required |
grouped_dodaf
|
T_dodaf | T_klodaf
|
A dict or a KeyedList that maps each value to a Daf. |
required |
reduce_cols
|
T_la | None
|
The columns to reduce. All columns except |
None
|
diagnose
|
bool
|
If True, print the progress with a timestamp: the grouping, the number of groups, and the first groups. |
False
|
all_cols
|
T_ls | None
|
The columns of the result, in order. Use it when each group holds only some columns, so that the rows have the same columns as for whole groups. The columns that are not reduced are then empty. |
None
|
**kwargs
|
Any
|
Keyword arguments passed on to the function. |
{}
|
Returns:
| Type | Description |
|---|---|
'Daf'
|
The Daf with one row for each group. |
Examples:
Reduce a dict of Daf objects, one for each city, to one row each:
>>> groups = {'Denver': Daf(cols=['qty'], lol=[[2], [1]]), 'Austin': Daf(cols=['qty'], lol=[[6]])}
>>> result = Daf.reduce_dodaf_to_daf('city', Daf.sum_da, groups)
>>> result
| qty | city |
| --: | -----: |
| 3 | Denver |
| 6 | Austin |
%% daf rows=2; cols=2; keyfield='city'; name=''
Reduce only some columns, with reduce_cols. The others are empty:
>>> parts = {'Denver': Daf(cols=['qty', 'visits'], lol=[[3, 4], [7, 9]])}
>>> Daf.reduce_dodaf_to_daf('city', Daf.sum_da, parts, reduce_cols=['qty'])
| qty | visits | city |
| --: | -----: | -----: |
| 10 | | Denver |
%% daf rows=1; cols=3; keyfield='city'; name=''
Give the columns of the result with all_cols, when the groups hold only some of them:
>>> parts = {'Denver': Daf(cols=['qty'], lol=[[3], [7]]), 'Austin': Daf(cols=['qty'], lol=[[10]])}
>>> Daf.reduce_dodaf_to_daf('city', Daf.sum_da, parts, all_cols=['city', 'qty', 'note'])
| city | qty | note |
| -----: | --: | ---: |
| Denver | 10 | |
| Austin | 10 | |
%% daf rows=2; cols=3; keyfield='city'; name=''
groupsum_daf ¶
groupsum_daf(
colname, by="row", reduce_cols=None, indirect_col=""
)
Group by a column and add up the other columns of each group.
This is groupby_reduce() with sum_da(). The result has one row for each value of the
column, in the order first seen, with that column as its keyfield. Only numbers are added.
Text counts as 0, even text of digits, so convert a table read from CSV first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
colname
|
str
|
The column to group by. |
required |
by
|
str
|
|
'row'
|
reduce_cols
|
T_cs | None
|
The columns to add. |
None
|
indirect_col
|
str
|
A column that holds a dict. Each group's dicts are added into one, in that column. |
''
|
Returns:
| Type | Description |
|---|---|
'Daf'
|
The Daf with one row for each group. |
Examples:
Create a small Daf of orders:
>>> daf = Daf(cols=['city', 'qty', 'amount'], lol=[['Denver', 2, 25.0], ['Austin', 6, 24.0], ['Denver', 1, 30.5]])
>>> daf
| city | qty | amount |
| -----: | --: | -----: |
| Denver | 2 | 25.0 |
| Austin | 6 | 24.0 |
| Denver | 1 | 30.5 |
%% daf rows=3; cols=3; keyfield=''; name=''
Total the amount for each city:
>>> daf.groupsum_daf('city', reduce_cols=['amount'])
| city | qty | amount |
| -----: | --: | -----: |
| Denver | | 55.5 |
| Austin | | 24.0 |
%% daf rows=2; cols=3; keyfield='city'; name=''
multi_groupsum ¶
multi_groupsum(
colnames=None,
by="row",
reduce_cols=None,
indirect_col="",
)
Group by each of several columns, one at a time, and add up the columns.
This is multi_groupby_reduce() with sum_da(). Each column gets a Daf of its own, with one
row for each of its values, and that column as the keyfield. The other columns grouped by are
Python NULL in it. Use it to total the same columns by city, by plan and so on, in one call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
colnames
|
T_cs | None
|
The columns to group by. These are required. |
None
|
by
|
str
|
|
'row'
|
reduce_cols
|
T_cs | None
|
The columns to add. |
None
|
indirect_col
|
str
|
A column that holds a dict. Each group's dicts are added into one, in that column. |
''
|
Returns:
| Type | Description |
|---|---|
Dict[str, 'Daf']
|
A dict that maps each column to a Daf with one row for each value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
No |
Examples:
Create a small Daf of orders, with a city and a plan:
>>> daf = Daf(cols=['city', 'plan', 'qty'], lol=[['Denver', 'basic', 2], ['Denver', 'pro', 6], ['Austin', 'basic', 1]])
>>> daf
| city | plan | qty |
| -----: | ----: | --: |
| Denver | basic | 2 |
| Denver | pro | 6 |
| Austin | basic | 1 |
%% daf rows=3; cols=3; keyfield=''; name=''
Total the quantity by city, and separately by plan:
>>> sums = daf.multi_groupsum(colnames=['city', 'plan'], reduce_cols=['qty'])
>>> sums['city']
| city | plan | qty |
| -----: | ---: | --: |
| Denver | | 8 |
| Austin | | 1 |
%% daf rows=2; cols=3; keyfield='city'; name=''
>>> sums['plan']
| city | plan | qty |
| ---: | ----: | --: |
| | basic | 3 |
| | pro | 6 |
%% daf rows=2; cols=3; keyfield='plan'; name=''
The columns to group by are required:
>>> daf.multi_groupsum()
Traceback (most recent call last):
...
ValueError: multi_groupsum: colnames is required
Manifest processing¶
manifest_apply ¶
manifest_apply(
func,
load_func,
save_func,
by="row",
cols=None,
**kwargs,
)
Run a function on each chunk that a manifest lists, and save the results.
A manifest is a Daf in which each row describes one chunk of data. For each
row, load_func loads the chunk as a Daf. func is applied to it with
by='table', so it gets the loaded Daf and the keyword cols. It returns a
tuple of a dict that describes the result chunk and the new Daf. save_func
saves the new Daf. The result manifest has one row for each dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[T_da, T_la | None], Tuple[T_da, 'Daf']]
|
Gets a loaded Daf. Returns a dict that describes the result and the new Daf. |
required |
load_func
|
Callable[[T_ma], 'Daf']
|
Loads the chunk that a manifest row describes. |
required |
save_func
|
Callable[[T_da, 'Daf'], str]
|
Saves a new Daf, given the dict that describes it. |
required |
by
|
str
|
Must be |
'row'
|
cols
|
T_la | None
|
Passed to |
None
|
**kwargs
|
Any
|
Keyword arguments passed on to |
{}
|
Returns:
| Type | Description |
|---|---|
'Daf'
|
The manifest of the result chunks. |
Examples:
Keep two chunks of a table, here in a dict. In real use they would be files:
>>> chunks = {'a': Daf(cols=['qty'], lol=[[2], [6]]), 'b': Daf(cols=['qty'], lol=[[1]])}
>>> saved = []
>>> def load(spec):
... return chunks[spec['chunk']]
>>> def double(daf, cols=None):
... new = Daf(cols=['qty'], lol=[[row['qty'] * 2] for row in daf])
... return {'rows': len(new)}, new
>>> def save(spec, daf):
... saved.append(daf.to_lod())
... return 'saved'
Make a manifest, a Daf with one row for each chunk:
>>> manifest = Daf(cols=['chunk'], lol=[['a'], ['b']])
>>> manifest
| chunk |
| ----: |
| a |
| b |
%% daf rows=2; cols=1; keyfield=''; name=''
Load each chunk, double it, and save it. The result has a row of status for each chunk:
>>> result = manifest.manifest_apply(double, load, save, by='table')
>>> result
| rows |
| ---: |
| 2 |
| 1 |
%% daf rows=2; cols=1; keyfield=''; name=''
>>> saved
[[{'qty': 4}, {'qty': 12}], [{'qty': 2}]]
manifest_reduce ¶
manifest_reduce(
func, load_func=None, by="row", cols=None, **kwargs
)
Reduce the chunks that a manifest lists into one row.
Each chunk is loaded with load_func and reduced with reduce(). The
reductions are put in a Daf, and that is reduced again with the same function.
This works for functions such as sum_da() that can be combined in this way.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
The reduction function. See |
required |
load_func
|
Callable[[T_ma], 'Daf'] | None
|
Loads the chunk that a manifest row describes. It is required. |
None
|
by
|
str
|
How the function is applied. See |
'row'
|
cols
|
T_la | None
|
The columns to reduce. All columns if None. |
None
|
**kwargs
|
Any
|
Keyword arguments passed on to the function. |
{}
|
Returns:
| Type | Description |
|---|---|
T_da
|
The reduced row, as a dict. |
Raises:
| Type | Description |
|---|---|
ValueError
|
No |
Examples:
Keep two chunks of a table, here in a dict, and a manifest with a row for each:
>>> store = {'c1': Daf(cols=['qty', 'amount'], lol=[[2, 25.0], [6, 24.0]]), 'c2': Daf(cols=['qty', 'amount'], lol=[[1, 30.5]])}
>>> manifest = Daf(lol=[['c1'], ['c2']], cols=['chunk'])
>>> manifest
| chunk |
| ----: |
| c1 |
| c2 |
%% daf rows=2; cols=1; keyfield=''; name=''
Total the columns over every chunk, one chunk at a time:
>>> manifest.manifest_reduce(Daf.sum_da, load_func=lambda spec: store[spec['chunk']])
{'qty': 9, 'amount': 79.5}
manifest_process ¶
manifest_process(func, **kwargs)
Call a function once for each chunk that a manifest lists.
The function gets the manifest row as a dict, and does its own loading and saving. It returns a dict of information about what it did. These dicts are collected as the rows of the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., T_da]
|
Gets a manifest row and returns a dict of results. |
required |
**kwargs
|
Any
|
Keyword arguments passed on to the function. |
{}
|
Returns:
| Type | Description |
|---|---|
'Daf'
|
A Daf with one row for each returned dict. |
Examples:
Make a manifest with a row for each chunk:
>>> manifest = Daf(lol=[['c1'], ['c2']], cols=['chunk'])
>>> manifest
| chunk |
| ----: |
| c1 |
| c2 |
%% daf rows=2; cols=1; keyfield=''; name=''
Call a function for each row of the manifest, and collect the results:
>>> manifest.manifest_process(lambda spec: {'chunk': spec['chunk'], 'seen': True})
| chunk | seen |
| ----: | ---: |
| c1 | True |
| c2 | True |
%% daf rows=2; cols=2; keyfield=''; name=''
Regex and settings¶
set_col2_from_col1_using_regex_select ¶
set_col2_from_col1_using_regex_select(
col1, col2="", regex=""
)
Fill a column with the part of another column that a regex selects, in place.
regex must have one pair of parentheses around the part to keep. A cell that
does not match gives an empty cell. Give regex as a keyword. Without col2,
col1 is changed. If col2 is not a column, it is added at the right.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
col1
|
str
|
The column to read. |
required |
col2
|
str
|
The column to write. Defaults to |
''
|
regex
|
str
|
A regular expression with one group. |
''
|
Returns:
| Type | Description |
|---|---|
'Daf'
|
This Daf, which has been changed. |
Examples:
Create a small Daf of people, each with a zip code with a prefix:
>>> daf = Daf(cols=['id', 'code'], lol=[['p1', 'ZIP80202'], ['p2', 'ZIP73301']])
>>> daf
| id | code |
| -: | -------: |
| p1 | ZIP80202 |
| p2 | ZIP73301 |
%% daf rows=2; cols=2; keyfield=''; name=''
Keep only the digits, in place, with the group of a regular expression:
>>> daf.set_col2_from_col1_using_regex_select('code', regex=r'(\d+)')
| id | code |
| -: | ----: |
| p1 | 80202 |
| p2 | 73301 |
%% daf rows=2; cols=2; keyfield=''; name=''
Write the result to another column, here a new one:
>>> paths = Daf(cols=['id', 'path'], lol=[['a', 'dir/file1.csv'], ['b', 'dir/file22.csv']])
>>> paths.set_col2_from_col1_using_regex_select('path', 'name', r'dir/(\w+)\.csv')
| id | path | name |
| -: | -------------: | -----: |
| a | dir/file1.csv | file1 |
| b | dir/file22.csv | file22 |
%% daf rows=2; cols=3; keyfield=''; name=''
apply_replace_regex ¶
apply_replace_regex(col, col2='', replace_regex='')
Change a column with a pattern of the form /find/replace/, in place.
The pattern has three parts that are separated by /. The first is a regular
expression. The second is what replaces the part it finds. The groups it
finds can be used as \1. A pattern with an empty second part removes the text.
/find// remove
/find/replace/ replace
/pre(select)post/\1/ keep only the selected part
/pre(select)post/a\1b/ keep it, with new text around it
The result goes to col2, or to col if there is no col2. A column that is
not found does nothing. If col2 is not a column, it is added at the right.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
col
|
str
|
The column to read. |
required |
col2
|
str
|
The column to write. Defaults to |
''
|
replace_regex
|
str
|
The pattern. |
''
|
Returns:
| Type | Description |
|---|---|
'Daf'
|
This Daf, which has been changed. |
Examples:
Create a small Daf of people, with a zip code with a prefix:
>>> daf = Daf(cols=['id', 'code'], lol=[['p1', 'ZIP80202']])
>>> daf
| id | code |
| -: | -------: |
| p1 | ZIP80202 |
%% daf rows=1; cols=2; keyfield=''; name=''
Remove the prefix, with a find and replace pattern:
>>> daf.apply_replace_regex('code', replace_regex='/ZIP//').col('code')
['80202']
Write the result to another column, here a new one. The pattern has the form
/find/replace/, where the first character is the separator:
>>> paths = Daf(cols=['id', 'path'], lol=[['a', 'dir/file1.csv']])
>>> paths.apply_replace_regex('path', 'stem', r'/\.csv$//')
| id | path | stem |
| -: | ------------: | --------: |
| a | dir/file1.csv | dir/file1 |
%% daf rows=1; cols=3; keyfield=''; name=''
alter_daf_per_setting ¶
alter_daf_per_setting(
settingsdict,
setting_name,
setting_select_dict,
silent_error=False,
)
Change this Daf with the replace patterns found in a settings dict.
The setting is a dict, or a list of dicts. Each has a spec_name, a colname
and a replace_regex. The ones whose fields match setting_select_dict are
used. For each, apply_replace_regex() changes the column. Use it to give
different edits to different files, such as fixing ids in one source.
A setting that is None or empty changes nothing. A name that is not in the
settings dict raises KeyError, unless silent_error is True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settingsdict
|
T_da
|
A dict of settings. |
required |
setting_name
|
str
|
The key of the setting in the dict. |
required |
setting_select_dict
|
dict
|
Selects which specs apply, such as |
required |
silent_error
|
bool
|
If True, a missing setting changes nothing. |
False
|
Returns:
| Type | Description |
|---|---|
'Daf'
|
This Daf, which has been changed. |
Raises:
| Type | Description |
|---|---|
KeyError
|
The setting is missing and |
Examples:
Create a small Daf of order numbers, some with an old prefix:
>>> daf = Daf(lol=[['04_1'], ['05_2']], cols=['order_id'])
>>> daf
| order_id |
| -------: |
| 04_1 |
| 05_2 |
%% daf rows=2; cols=1; keyfield=''; name=''
Change the prefix by a setting that names the file it applies to:
>>> spec = {'spec_name': 'a.zip', 'colname': 'order_id', 'replace_regex': r'/^04_/14_/'}
>>> daf.alter_daf_per_setting({'fix': [spec]}, 'fix', {'spec_name': 'a.zip'}).col('order_id')
['14_1', '05_2']
A setting that is not in the dict raises KeyError, or changes nothing with silent_error=True:
>>> table = Daf(cols=['id', 'qty'], lol=[['a', '3']])
>>> table.alter_daf_per_setting({}, 'alter', {'spec_name': 'f.csv'}, silent_error=True)
| id | qty |
| -: | --: |
| a | 3 |
%% daf rows=1; cols=2; keyfield=''; name=''
alter_daf_per_alter_specs_daf ¶
alter_daf_per_alter_specs_daf(alter_specs_daf)
Change this Daf with the replace patterns listed in a Daf.
Each row of alter_specs_daf has a colname and a replace_regex. The
pattern is applied to the column with apply_replace_regex(). Give it only
the rows that apply, for example by selecting on spec_name first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alter_specs_daf
|
'Daf'
|
The specs, with the columns |
required |
Returns:
| Type | Description |
|---|---|
'Daf'
|
This Daf, which has been changed. |
Examples:
Create a small Daf of order numbers, some with an old prefix:
>>> daf = Daf(lol=[['04_1'], ['05_2']], cols=['order_id'])
>>> daf
| order_id |
| -------: |
| 04_1 |
| 05_2 |
%% daf rows=2; cols=1; keyfield=''; name=''
Make a Daf of the changes, one row for each:
>>> specs = Daf.from_lod([{'colname': 'order_id', 'replace_regex': r'/^04_/14_/'}])
>>> specs
| colname | replace_regex |
| -------: | ------------: |
| order_id | /^04_/14_/ |
%% daf rows=1; cols=2; keyfield=''; name=''
Apply them:
>>> daf.alter_daf_per_alter_specs_daf(specs).col('order_id')
['14_1', '05_2']
Other¶
cols_to_dol ¶
cols_to_dol(colname1, colname2)
Make a lookup from the values of one column to the values of another.
For each value in colname1, the result lists the different values that appear
with it in colname2, in the order first seen. Use it to see how two columns
relate. The values must be hashable. If a name is not a column, or the Daf is
empty, the result is empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
colname1
|
str
|
The column of keys. |
required |
colname2
|
str
|
The column of values. |
required |
Returns:
| Type | Description |
|---|---|
T_dola
|
A dict that maps each value of the first column to a list of values of the second. |
Examples:
Create a small Daf of people by city. One row repeats:
>>> daf = Daf(cols=['city', 'name'], lol=[['Denver', 'Ana Lopez'], ['Austin', 'Ben Okafor'], ['Denver', 'Chen Wu'], ['Austin', 'Ben Okafor']])
>>> daf
| city | name |
| -----: | ---------: |
| Denver | Ana Lopez |
| Austin | Ben Okafor |
| Denver | Chen Wu |
| Austin | Ben Okafor |
%% daf rows=4; cols=2; keyfield=''; name=''
List the people in each city. A repeated name is kept once:
>>> daf.cols_to_dol('city', 'name')
{'Denver': ['Ana Lopez', 'Chen Wu'], 'Austin': ['Ben Okafor']}
insert_dif_row ¶
insert_dif_row(
irow1, irow2=None, irow_insert=None, cols=None
)
Insert a row that holds the difference of two rows.
The difference is the first row minus the second row, for the numeric columns
you name. Other columns of the new row are empty. A cell that is empty counts
as 0. By default the second row is the one after the first, and the new row goes
between them. Columns that hold text must not be in cols.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
irow1
|
int
|
The position of the first row. |
required |
irow2
|
int | None
|
The position of the second row. If None, the row after the first. |
None
|
irow_insert
|
int | None
|
Where to insert the new row. If None, at |
None
|
cols
|
T_ls | None
|
The columns to subtract. If None, all columns. |
None
|
Returns:
| Type | Description |
|---|---|
'Daf'
|
This Daf, which has been changed. |
Examples:
Create a small Daf of sales by month:
>>> daf = Daf(cols=['month', 'sales'], lol=[[1, 1200], [2, 1450], [3, 1610]])
>>> daf
| month | sales |
| ----: | ----: |
| 1 | 1200 |
| 2 | 1450 |
| 3 | 1610 |
%% daf rows=3; cols=2; keyfield=''; name=''
Insert a row with the difference of the first two rows:
>>> daf.insert_dif_row(0)
| month | sales |
| ----: | ----: |
| 1 | 1200 |
| -1 | -250 |
| 2 | 1450 |
| 3 | 1610 |
%% daf rows=4; cols=2; keyfield=''; name=''
The difference is the first row minus the second. Choose the two rows, where the new row goes, and the columns to subtract. The other columns are empty:
>>> series = Daf(cols=['week', 'visits'], lol=[[1, 310], [2, 345], [4, 402]])
>>> series.insert_dif_row(0, 2, irow_insert=3, cols=['visits'])
| week | visits |
| ---: | -----: |
| 1 | 310 |
| 2 | 345 |
| 4 | 402 |
| | -92 |
%% daf rows=4; cols=2; keyfield=''; name=''
insert_dif_rows ¶
insert_dif_rows(irows_rli=None, cols=None, offset=0)
Insert a difference row after each of several rows.
For each position, a row is inserted that holds that row minus the next row,
as in insert_dif_row(). Do not list the last row. By default all rows are
used. With offset=1 the new row goes after the next row, not between them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
irows_rli
|
T_rli | None
|
The positions of the first rows. If None, every row but the last. |
None
|
cols
|
T_ls | None
|
The columns to subtract. If None, all columns. |
None
|
offset
|
int
|
0 inserts between the two rows. 1 inserts after the second. |
0
|
Returns:
| Type | Description |
|---|---|
'Daf'
|
This Daf, which has been changed. |
Examples:
Create a small Daf of sales by month:
>>> daf = Daf(cols=['month', 'sales'], lol=[[1, 1200], [2, 1450], [3, 1610]])
>>> daf
| month | sales |
| ----: | ----: |
| 1 | 1200 |
| 2 | 1450 |
| 3 | 1610 |
%% daf rows=3; cols=2; keyfield=''; name=''
Insert a row after each pair of rows, with their difference:
>>> daf.insert_dif_rows()
| month | sales |
| ----: | ----: |
| 1 | 1200 |
| -1 | -250 |
| 2 | 1450 |
| -1 | -160 |
| 3 | 1610 |
%% daf rows=5; cols=2; keyfield=''; name=''
Only after some rows, with irows_rli, and only some columns, with cols. With offset=1, each
difference goes after the second row of its pair:
>>> series = Daf(cols=['week', 'visits'], lol=[[1, 310], [2, 345], [4, 402]])
>>> series.insert_dif_rows(cols=['visits'], offset=1)
| week | visits |
| ---: | -----: |
| 1 | 310 |
| 2 | 345 |
| | -35 |
| 4 | 402 |
| | -57 |
%% daf rows=5; cols=2; keyfield=''; name=''
>>> Daf(cols=['week', 'visits'], lol=[[1, 310], [2, 345], [4, 402]]).insert_dif_rows([1])
| week | visits |
| ---: | -----: |
| 1 | 310 |
| 2 | 345 |
| -2 | -57 |
| 4 | 402 |
%% daf rows=4; cols=2; keyfield=''; name=''