|
91 | 91 | :meth:`rec_join` |
92 | 92 | join two record arrays on sequence of fields |
93 | 93 |
|
| 94 | +:meth:`recs_join` |
| 95 | + a simple join of multiple recarrays using a single column as a key |
| 96 | +
|
94 | 97 | :meth:`rec_groupby` |
95 | 98 | summarize data by groups (similar to SQL GROUP BY) |
96 | 99 |
|
|
139 | 142 | """ |
140 | 143 |
|
141 | 144 | from __future__ import division |
142 | | -import csv, warnings, copy, os |
| 145 | +import csv, warnings, copy, os, operator |
143 | 146 |
|
144 | 147 | import numpy as np |
145 | 148 | ma = np.ma |
@@ -1880,6 +1883,28 @@ def mapped_r2field(name): |
1880 | 1883 |
|
1881 | 1884 | return newrec |
1882 | 1885 |
|
| 1886 | +def recs_join(key, name, recs,missing=0.): |
| 1887 | + """ |
| 1888 | + *key* is the column name that acts as a key |
| 1889 | + *name* is the name that we want to join |
| 1890 | + *missing" is what the missing fields are replaced by |
| 1891 | + *recarrays* is a list of record arrays to join |
| 1892 | +
|
| 1893 | + returns a record array with columns [rowkey, name1, name2, ... namen] |
| 1894 | +
|
| 1895 | + >>> r = recs_join("date", "close", recs=[r0, r1], missing=0.) |
| 1896 | +
|
| 1897 | + """ |
| 1898 | + results = [] |
| 1899 | + def extract(r): |
| 1900 | + if r is None: return missing |
| 1901 | + else: return r[name] |
| 1902 | + |
| 1903 | + for rowkey, row in cbook.align_iterators(operator.attrgetter(key), *[iter(r) for r in recs]): |
| 1904 | + results.append([rowkey] + map(extract, row)) |
| 1905 | + names = ",".join([key] + ["%s%d" % (name, d) for d in range(len(recs))]) |
| 1906 | + return np.rec.fromrecords(results, names=names) |
| 1907 | + |
1883 | 1908 |
|
1884 | 1909 | def csv2rec(fname, comments='#', skiprows=0, checkrows=0, delimiter=',', |
1885 | 1910 | converterd=None, names=None, missing='', missingd=None, |
|
0 commit comments