[SciPy-User] [SciPy-user] Matching up arrays?
Keith Goodman
kwgoodman@gmail....
Fri Nov 19 09:40:37 CST 2010
On Thu, Nov 18, 2010 at 9:58 PM, mdekauwe <mdekauwe@gmail.com> wrote:
>
> Hi,
>
> So I have 2 arrays which hold some data and for each array I have an array
> with the associated date stamp. The dates vary between arrays, so what I
> would like to be able to do is just subset the data so I end up with two
> arrays if there is a matching time stamp between arrays (with the idea being
> I would do some comparative stats on these arrays). How I solved it seems a
> bit ugly and I wondered if anyone had a better idea?
>
> e.g.
>
> m_date = np.array(['1998-01-01 00:00:00', '1999-01-01 00:00:00', '2000-01-01
> 00:00:00',
> '2005-01-01 00:00:00'])
> o_date = np.array(['1998-01-01 00:00:00', '1999-01-01 00:00:00', '2000-01-01
> 00:00:00'])
>
> mm = np.array([ 3.5732, 4.5761, 4.0994, 3.9031])
> oo = np.array([ 5.84, 5.66, 5.83])
>
> x, y = [], []
> o = np.vstack((o_date, oo))
> m = np.vstack((m_date, mm))
> for i in xrange(o.shape[1]):
> for j in xrange(m.shape[1]):
> if m[0,j] == o[0,i]:
> x.append(m[1,j])
> y.append(o[1,i])
You could try using a labeled-array (http://pypi.python.org/pypi/la).
Create larrys:
>> mlar = la.larry(mm, [m_date.tolist()])
>> olar = la.larry(oo, [o_date.tolist()])
Alignment is automatic for binary operations (the default is an inner join):
>> mlar + olar
label_0
1998-01-01 00:00:00
1999-01-01 00:00:00
2000-01-01 00:00:00
x
array([ 9.4132, 10.2361, 9.9294])
Our you can prealign the data using various join methods:
>> m, o = la.align(mlar, olar, join='outer')
>> o
label_0
1998-01-01 00:00:00
1999-01-01 00:00:00
2000-01-01 00:00:00
2005-01-01 00:00:00
x
array([ 5.84, 5.66, 5.83, NaN])
If all you want are the underlying arrays, then:
>> m.A
array([ 3.5732, 4.5761, 4.0994, 3.9031])
>> o.A
array([ 5.84, 5.66, 5.83, NaN])
More information about the SciPy-User
mailing list