[Numpy-discussion] DataArray usage question + bug?

Skipper Seabold jsseabold at gmail.com
Mon Aug 23 18:28:01 EDT 2010


I have some new "typical" data that I'm trying out DataArray with, and
I'm trying to get my head around it again.  Is this the best way to
hold data organized by, say, household and time (week number).  I
guess what I'm asking is do I understand the concept of axes and ticks
correctly?  It seems to be what I want, but my earlier understanding
of how I would go about this was a bit different.

import numpy as np
from datarray.datarray import DataArray

ddd = np.empty((52*5,5))

# Household ID
ddd[:,0] = np.repeat([1111,2222,3333,4444,5555], 52)

# Week Number
ddd[:,1] = np.tile(range(1,53), 5)

# Some Categorical Variable
ddd[:,2] = np.tile(range(1,6), 52)

# Some Variable
ddd[:,3] = np.random.uniform(0,5.0, size=52*5)

# Some Other Variable
ddd[:,4] = np.random.uniform(-5,0., size=52*5)

# Create axes labels and ticks
hhold_ax = 'households', np.unique(ddd[:,0]).tolist()
time_ax = 'week', np.unique(ddd[:,1]).tolist()
var_ax = 'variables', ['some_dummy', 'some_var', 'other_var']

darr = DataArray(ddd[:,2:].reshape(5,52,-1), [hhold_ax, time_ax, var_ax])

It might be nice to have some convenience functions that will do the
reshape on the original 2d data if they don't already exist, so
end-users don't have to think about it.

As for the bug report.  If I don't tolist() the ticks above there is
an error.  I can file a bug report if it's warranted.

<snip>
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/home/skipper/school/RA/<ipython console> in <module>()

/usr/local/lib/python2.6/dist-packages/datarray/datarray.pyc in
__new__(cls, data, labels, dtype, copy)
    604             axes.append(Axis(label, i, arr, ticks=ticks))
    605
--> 606         _set_axes(arr, axes)
    607
    608         # validate the axes


/usr/local/lib/python2.6/dist-packages/datarray/datarray.pyc in
_set_axes(dest, in_axes)
    475     # Create the containers for various axis-related info

    476     for ax in in_axes:
--> 477         new_ax = ax._copy(parent_arr=dest)
    478         axes.append(new_ax)
    479         if hasattr(ax_holder, ax.name):

/usr/local/lib/python2.6/dist-packages/datarray/datarray.pyc in
_copy(self, **kwargs)
    113         ticks = kwargs.pop('ticks', copy.copy(self.ticks))
    114         ax.ticks = ticks
--> 115         if ticks and len(ticks) != len(self.ticks):
    116             ax._tick_dict = dict( zip(ticks, xrange( len(ticks) )) )
    117         else:

ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()

Skipper



More information about the NumPy-Discussion mailing list