Skip to content

Commit 5299eee

Browse files
committed
LArray does not inherit from ndarray, just stores one
1 parent 90dca30 commit 5299eee

1 file changed

Lines changed: 53 additions & 18 deletions

File tree

‎larray/core.py‎

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -795,27 +795,26 @@ def without(self, axes):
795795
return res
796796

797797

798-
class LArray(np.ndarray):
798+
class LArray(object):
799799
"""
800800
LArray class
801801
"""
802-
def __new__(cls, data, axes=None):
803-
obj = np.asarray(data).view(cls)
804-
ndim = obj.ndim
802+
def __init__(self, data, axes=None):
803+
ndim = data.ndim
805804
if axes is not None:
806805
if len(axes) != ndim:
807806
raise ValueError("number of axes (%d) does not match "
808807
"number of dimensions of data (%d)"
809808
% (len(axes), ndim))
810809
shape = tuple(len(axis) for axis in axes)
811-
if shape != obj.shape:
810+
if shape != data.shape:
812811
raise ValueError("length of axes %s does not match "
813-
"data shape %s" % (shape, obj.shape))
812+
"data shape %s" % (shape, data.shape))
814813

815814
if axes is not None and not isinstance(axes, AxisCollection):
816815
axes = AxisCollection(axes)
817-
obj.axes = axes
818-
return obj
816+
self.data = data
817+
self.axes = axes
819818

820819
@property
821820
def df(self):
@@ -974,8 +973,9 @@ def translated_key(self, key):
974973
def __getitem__(self, key, collapse_slices=False):
975974
data = np.asarray(self)
976975

977-
if isinstance(key, np.ndarray) and np.issubdtype(key.dtype, bool):
978-
return data[key]
976+
if (isinstance(key, np.ndarray) or isinstance(key, LArray)) and \
977+
np.issubdtype(key.dtype, bool):
978+
return data[np.asarray(key)]
979979

980980
translated_key = self.translated_key(self.full_key(key))
981981

@@ -987,15 +987,20 @@ def __getitem__(self, key, collapse_slices=False):
987987
data = data[cross_key]
988988
# drop length 1 dimensions created by scalar keys
989989
data = data.reshape(tuple(len(axis) for axis in axes))
990-
return LArray(data, axes)
990+
if not axes:
991+
# scalars do not need to be wrapped in LArray
992+
return data
993+
else:
994+
return LArray(data, axes)
991995

992996
def __setitem__(self, key, value, collapse_slices=True):
993997
data = np.asarray(self)
994998

995-
if isinstance(key, np.ndarray) and np.issubdtype(key.dtype, bool):
999+
if (isinstance(key, np.ndarray) or isinstance(key, LArray)) and \
1000+
np.issubdtype(key.dtype, bool):
9961001
if isinstance(key, LArray):
9971002
key = key.broadcast_with(self.axes)
998-
data[key] = value
1003+
data[np.asarray(key)] = value
9991004
return
10001005

10011006
translated_key = self.translated_key(self.full_key(key))
@@ -1162,15 +1167,18 @@ def _axis_aggregate(self, op, axes=()):
11621167
"""
11631168
src_data = np.asarray(self)
11641169
if not axes:
1165-
# scalars don't need to be wrapped in LArray
1166-
return op(src_data)
1170+
axes = self.axes
11671171

11681172
axes_indices = tuple(self.get_axis_idx(a) for a in axes)
11691173
res_data = op(src_data, axis=axes_indices)
11701174
axes_tokill = set(axes_indices)
11711175
res_axes = [axis for axis_num, axis in enumerate(self.axes)
11721176
if axis_num not in axes_tokill]
1173-
return LArray(res_data, res_axes)
1177+
if not res_axes:
1178+
# scalars don't need to be wrapped in LArray
1179+
return res_data
1180+
else:
1181+
return LArray(res_data, res_axes)
11741182

11751183
def get_axis_idx(self, axis):
11761184
"""
@@ -1294,7 +1302,7 @@ def isaxis(a):
12941302
return res
12951303

12961304
def copy(self):
1297-
return LArray(np.ndarray.copy(self), axes=self.axes[:])
1305+
return LArray(self.data.copy(), axes=self.axes[:])
12981306

12991307
@property
13001308
def info(self):
@@ -1349,7 +1357,7 @@ def opmethod(self, other):
13491357
elif not np.isscalar(other):
13501358
raise TypeError("unsupported operand type(s) for %s: '%s' "
13511359
"and '%s'" % (opname, type(self), type(other)))
1352-
return super_method(self, other)
1360+
return LArray(super_method(self.data, other), self.axes)
13531361
opmethod.__name__ = fullname
13541362
return opmethod
13551363

@@ -1515,6 +1523,33 @@ def to_clipboard(self, *args, **kwargs):
15151523
def plot(self, *args, **kwargs):
15161524
self.df.plot(*args, **kwargs)
15171525

1526+
@property
1527+
def shape(self):
1528+
return self.data.shape
1529+
1530+
@property
1531+
def ndim(self):
1532+
return self.data.ndim
1533+
1534+
@property
1535+
def size(self):
1536+
return self.data.size
1537+
1538+
@property
1539+
def dtype(self):
1540+
return self.data.dtype
1541+
1542+
@property
1543+
def item(self):
1544+
return self.data.item
1545+
1546+
def __len__(self):
1547+
return len(self.data)
1548+
1549+
def __array__(self, dtype=None):
1550+
return self.data
1551+
1552+
15181553

15191554
def parse(s):
15201555
"""

0 commit comments

Comments
 (0)