| 1 |
from __future__ import absolute_import, print_function, division |
| 2 |
|
| 3 |
|
| 4 |
from petl.compat import next |
| 5 |
|
| 6 |
|
| 7 |
from petl.util.base import Table, asindices |
| 8 |
|
| 9 |
|
| 10 |
def filldown(table, *fields, **kwargs): |
| 11 |
""" |
| 12 |
Replace missing values with non-missing values from the row above. E.g.:: |
| 13 |
|
| 14 |
>>> import petl as etl |
| 15 |
>>> table1 = [['foo', 'bar', 'baz'], |
| 16 |
... [1, 'a', None], |
| 17 |
... [1, None, .23], |
| 18 |
... [1, 'b', None], |
| 19 |
... [2, None, None], |
| 20 |
... [2, None, .56], |
| 21 |
... [2, 'c', None], |
| 22 |
... [None, 'c', .72]] |
| 23 |
>>> table2 = etl.filldown(table1) |
| 24 |
>>> table2.lookall() |
| 25 |
+-----+-----+------+ |
| 26 |
| foo | bar | baz | |
| 27 |
+=====+=====+======+ |
| 28 |
| 1 | 'a' | None | |
| 29 |
+-----+-----+------+ |
| 30 |
| 1 | 'a' | 0.23 | |
| 31 |
+-----+-----+------+ |
| 32 |
| 1 | 'b' | 0.23 | |
| 33 |
+-----+-----+------+ |
| 34 |
| 2 | 'b' | 0.23 | |
| 35 |
+-----+-----+------+ |
| 36 |
| 2 | 'b' | 0.56 | |
| 37 |
+-----+-----+------+ |
| 38 |
| 2 | 'c' | 0.56 | |
| 39 |
+-----+-----+------+ |
| 40 |
| 2 | 'c' | 0.72 | |
| 41 |
+-----+-----+------+ |
| 42 |
|
| 43 |
>>> table3 = etl.filldown(table1, 'bar') |
| 44 |
>>> table3.lookall() |
| 45 |
+------+-----+------+ |
| 46 |
| foo | bar | baz | |
| 47 |
+======+=====+======+ |
| 48 |
| 1 | 'a' | None | |
| 49 |
+------+-----+------+ |
| 50 |
| 1 | 'a' | 0.23 | |
| 51 |
+------+-----+------+ |
| 52 |
| 1 | 'b' | None | |
| 53 |
+------+-----+------+ |
| 54 |
| 2 | 'b' | None | |
| 55 |
+------+-----+------+ |
| 56 |
| 2 | 'b' | 0.56 | |
| 57 |
+------+-----+------+ |
| 58 |
| 2 | 'c' | None | |
| 59 |
+------+-----+------+ |
| 60 |
| None | 'c' | 0.72 | |
| 61 |
+------+-----+------+ |
| 62 |
|
| 63 |
>>> table4 = etl.filldown(table1, 'bar', 'baz') |
| 64 |
>>> table4.lookall() |
| 65 |
+------+-----+------+ |
| 66 |
| foo | bar | baz | |
| 67 |
+======+=====+======+ |
| 68 |
| 1 | 'a' | None | |
| 69 |
+------+-----+------+ |
| 70 |
| 1 | 'a' | 0.23 | |
| 71 |
+------+-----+------+ |
| 72 |
| 1 | 'b' | 0.23 | |
| 73 |
+------+-----+------+ |
| 74 |
| 2 | 'b' | 0.23 | |
| 75 |
+------+-----+------+ |
| 76 |
| 2 | 'b' | 0.56 | |
| 77 |
+------+-----+------+ |
| 78 |
| 2 | 'c' | 0.56 | |
| 79 |
+------+-----+------+ |
| 80 |
| None | 'c' | 0.72 | |
| 81 |
+------+-----+------+ |
| 82 |
|
| 83 |
Use the `missing` keyword argument to control which value is treated as |
| 84 |
missing (`None` by default). |
| 85 |
|
| 86 |
""" |
| 87 |
|
| 88 |
return FillDownView(table, fields, **kwargs) |
| 89 |
|
| 90 |
|
| 91 |
Table.filldown = filldown |
| 92 |
|
| 93 |
|
| 94 |
class FillDownView(Table): |
| 95 |
|
| 96 |
def __init__(self, table, fields, missing=None): |
| 97 |
self.table = table |
| 98 |
self.fields = fields |
| 99 |
self.missing = missing |
| 100 |
|
| 101 |
def __iter__(self): |
| 102 |
return iterfilldown(self.table, self.fields, self.missing) |
| 103 |
|
| 104 |
|
| 105 |
def iterfilldown(table, fillfields, missing): |
| 106 |
it = iter(table) |
| 107 |
try: |
| 108 |
hdr = next(it) |
| 109 |
except StopIteration: |
| 110 |
return |
| 111 |
yield tuple(hdr) |
| 112 |
if not fillfields: # fill down all fields |
| 113 |
fillfields = hdr |
| 114 |
fillindices = asindices(hdr, fillfields) |
| 115 |
fill = list(next(it)) # fill values |
| 116 |
yield tuple(fill) |
| 117 |
for row in it: |
| 118 |
outrow = list(row) |
| 119 |
for idx in fillindices: |
| 120 |
if row[idx] == missing: |
| 121 |
outrow[idx] = fill[idx] # fill down |
| 122 |
else: |
| 123 |
fill[idx] = row[idx] # new fill value |
| 124 |
yield tuple(outrow) |
| 125 |
|
| 126 |
|
| 127 |
def fillright(table, missing=None): |
| 128 |
""" |
| 129 |
Replace missing values with preceding non-missing values. E.g.:: |
| 130 |
|
| 131 |
>>> import petl as etl |
| 132 |
>>> table1 = [['foo', 'bar', 'baz'], |
| 133 |
... [1, 'a', None], |
| 134 |
... [1, None, .23], |
| 135 |
... [1, 'b', None], |
| 136 |
... [2, None, None], |
| 137 |
... [2, None, .56], |
| 138 |
... [2, 'c', None], |
| 139 |
... [None, 'c', .72]] |
| 140 |
>>> table2 = etl.fillright(table1) |
| 141 |
>>> table2.lookall() |
| 142 |
+------+-----+------+ |
| 143 |
| foo | bar | baz | |
| 144 |
+======+=====+======+ |
| 145 |
| 1 | 'a' | 'a' | |
| 146 |
+------+-----+------+ |
| 147 |
| 1 | 1 | 0.23 | |
| 148 |
+------+-----+------+ |
| 149 |
| 1 | 'b' | 'b' | |
| 150 |
+------+-----+------+ |
| 151 |
| 2 | 2 | 2 | |
| 152 |
+------+-----+------+ |
| 153 |
| 2 | 2 | 0.56 | |
| 154 |
+------+-----+------+ |
| 155 |
| 2 | 'c' | 'c' | |
| 156 |
+------+-----+------+ |
| 157 |
| None | 'c' | 0.72 | |
| 158 |
+------+-----+------+ |
| 159 |
|
| 160 |
Use the `missing` keyword argument to control which value is treated as |
| 161 |
missing (`None` by default). |
| 162 |
|
| 163 |
""" |
| 164 |
|
| 165 |
return FillRightView(table, missing=missing) |
| 166 |
|
| 167 |
|
| 168 |
Table.fillright = fillright |
| 169 |
|
| 170 |
|
| 171 |
class FillRightView(Table): |
| 172 |
|
| 173 |
def __init__(self, table, missing=None): |
| 174 |
self.table = table |
| 175 |
self.missing = missing |
| 176 |
|
| 177 |
def __iter__(self): |
| 178 |
return iterfillright(self.table, self.missing) |
| 179 |
|
| 180 |
|
| 181 |
def iterfillright(table, missing): |
| 182 |
it = iter(table) |
| 183 |
try: |
| 184 |
hdr = next(it) |
| 185 |
except StopIteration: |
| 186 |
return |
| 187 |
yield tuple(hdr) |
| 188 |
for row in it: |
| 189 |
outrow = list(row) |
| 190 |
for i, _ in enumerate(outrow): |
| 191 |
if i > 0 and outrow[i] == missing and outrow[i-1] != missing: |
| 192 |
outrow[i] = outrow[i-1] |
| 193 |
yield tuple(outrow) |
| 194 |
|
| 195 |
|
| 196 |
def fillleft(table, missing=None): |
| 197 |
""" |
| 198 |
Replace missing values with following non-missing values. E.g.:: |
| 199 |
|
| 200 |
>>> import petl as etl |
| 201 |
>>> table1 = [['foo', 'bar', 'baz'], |
| 202 |
... [1, 'a', None], |
| 203 |
... [1, None, .23], |
| 204 |
... [1, 'b', None], |
| 205 |
... [2, None, None], |
| 206 |
... [2, None, .56], |
| 207 |
... [2, 'c', None], |
| 208 |
... [None, 'c', .72]] |
| 209 |
>>> table2 = etl.fillleft(table1) |
| 210 |
>>> table2.lookall() |
| 211 |
+-----+------+------+ |
| 212 |
| foo | bar | baz | |
| 213 |
+=====+======+======+ |
| 214 |
| 1 | 'a' | None | |
| 215 |
+-----+------+------+ |
| 216 |
| 1 | 0.23 | 0.23 | |
| 217 |
+-----+------+------+ |
| 218 |
| 1 | 'b' | None | |
| 219 |
+-----+------+------+ |
| 220 |
| 2 | None | None | |
| 221 |
+-----+------+------+ |
| 222 |
| 2 | 0.56 | 0.56 | |
| 223 |
+-----+------+------+ |
| 224 |
| 2 | 'c' | None | |
| 225 |
+-----+------+------+ |
| 226 |
| 'c' | 'c' | 0.72 | |
| 227 |
+-----+------+------+ |
| 228 |
|
| 229 |
Use the `missing` keyword argument to control which value is treated as |
| 230 |
missing (`None` by default). |
| 231 |
|
| 232 |
""" |
| 233 |
|
| 234 |
return FillLeftView(table, missing=missing) |
| 235 |
|
| 236 |
|
| 237 |
Table.fillleft = fillleft |
| 238 |
|
| 239 |
|
| 240 |
class FillLeftView(Table): |
| 241 |
|
| 242 |
def __init__(self, table, missing=None): |
| 243 |
self.table = table |
| 244 |
self.missing = missing |
| 245 |
|
| 246 |
def __iter__(self): |
| 247 |
return iterfillleft(self.table, self.missing) |
| 248 |
|
| 249 |
|
| 250 |
def iterfillleft(table, missing): |
| 251 |
it = iter(table) |
| 252 |
try: |
| 253 |
hdr = next(it) |
| 254 |
except StopIteration: |
| 255 |
return |
| 256 |
yield tuple(hdr) |
| 257 |
for row in it: |
| 258 |
outrow = list(reversed(row)) |
| 259 |
for i, _ in enumerate(outrow): |
| 260 |
if i > 0 and outrow[i] == missing and outrow[i-1] != missing: |
| 261 |
outrow[i] = outrow[i-1] |
| 262 |
yield tuple(reversed(outrow)) |