File: homogenize.rst

package info (click to toggle)
python-agate 1.13.0-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,008 kB
  • sloc: python: 8,578; makefile: 126
file content (91 lines) | stat: -rw-r--r-- 2,938 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
===============
Homogenize rows
===============

Fill in missing rows in a series. This can be used, for instance, to add rows for missing years in a time series.

Create rows for missing values
==============================

We can insert a default row for each value that is missing in a table from a given sequence of values.

Starting with a table like this, we can fill in rows for all missing years:

+-------+--------------+------------+
|  year | female_count | male_count |
+=======+==============+============+
|  1997 |           2  |         1  |
+-------+--------------+------------+
|  2000 |           4  |         3  |
+-------+--------------+------------+
|  2002 |           4  |         5  |
+-------+--------------+------------+
|  2003 |           1  |         2  |
+-------+--------------+------------+

.. code-block:: python

    key = 'year'
    expected_values = (1997, 1998, 1999, 2000, 2001, 2002, 2003)

    # Your default row should specify column values not in `key`
    default_row = (0, 0)

    new_table = table.homogenize(key, expected_values, default_row)

The result will be:

+-------+--------------+------------+
|  year | female_count | male_count |
+=======+==============+============+
|  1997 |           2  |         1  |
+-------+--------------+------------+
|  1998 |           0  |         0  |
+-------+--------------+------------+
|  1999 |           0  |         0  |
+-------+--------------+------------+
|  2000 |           4  |         3  |
+-------+--------------+------------+
|  2001 |           0  |         0  |
+-------+--------------+------------+
|  2002 |           4  |         5  |
+-------+--------------+------------+
|  2003 |           1  |         2  |
+-------+--------------+------------+


Create dynamic rows based on missing values
===========================================

We can also specify new row values with a value-generating function:

.. code-block:: python

    key = 'year'
    expected_values = (1997, 1998, 1999, 2000, 2001, 2002, 2003)

    # If default row is a function, it should return a full row
    def default_row(missing_value):
      return (missing_value, missing_value-1997, missing_value-1997)

    new_table = table.homogenize(key, expected_values, default_row)

The new table will be:

+-------+--------------+------------+
|  year | female_count | male_count |
+=======+==============+============+
|  1997 |           2  |         1  |
+-------+--------------+------------+
|  1998 |           1  |         1  |
+-------+--------------+------------+
|  1999 |           2  |         2  |
+-------+--------------+------------+
|  2000 |           4  |         3  |
+-------+--------------+------------+
|  2001 |           4  |         4  |
+-------+--------------+------------+
|  2002 |           4  |         5  |
+-------+--------------+------------+
|  2003 |           1  |         2  |
+-------+--------------+------------+