Skip to content

Commit fa73cbb

Browse files
committed
DataFrameWrapper masquerades a dataframe as an ndarray just well enough that we can run our test suite
not very interesting yet because the DF is still assumed to be dense and we pass it through asarray before indexing it
1 parent 36d9fcb commit fa73cbb

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

larray/core.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@ def opmethod(self, other):
13651365
elif not np.isscalar(other):
13661366
raise TypeError("unsupported operand type(s) for %s: '%s' "
13671367
"and '%s'" % (opname, type(self), type(other)))
1368-
return LArray(super_method(self.data, other), self.axes)
1368+
return LArray(super_method(np.asarray(self), other), self.axes)
13691369
opmethod.__name__ = fullname
13701370
return opmethod
13711371

@@ -1411,7 +1411,7 @@ def _unaryop(opname):
14111411
super_method = getattr(np.ndarray, fullname)
14121412

14131413
def opmethod(self):
1414-
return LArray(super_method(self.data), self.axes)
1414+
return LArray(super_method(np.asarray(self)), self.axes)
14151415
opmethod.__name__ = fullname
14161416
return opmethod
14171417

@@ -1541,6 +1541,15 @@ def to_clipboard(self, *args, **kwargs):
15411541
def plot(self, *args, **kwargs):
15421542
self.df.plot(*args, **kwargs)
15431543

1544+
#XXX: one less indirection as we have all the info at this level?
1545+
# @property
1546+
# def shape(self):
1547+
# return tuple(len(a) for a in self.axes)
1548+
#
1549+
# @property
1550+
# def ndim(self):
1551+
# return len(self.axes)
1552+
15441553
@property
15451554
def shape(self):
15461555
return self.data.shape
@@ -1565,7 +1574,7 @@ def __len__(self):
15651574
return len(self.data)
15661575

15671576
def __array__(self, dtype=None):
1568-
return self.data
1577+
return np.asarray(self.data)
15691578

15701579
__array_priority__ = 100
15711580

@@ -1658,6 +1667,31 @@ def __getitem__(self, key):
16581667
def __getattr__(self, key):
16591668
return getattr(self.df, key)
16601669

1670+
@property
1671+
def dtype(self):
1672+
# assumes df is homogeneous !
1673+
return self.df.dtypes[0]
1674+
1675+
@property
1676+
def ndim(self):
1677+
return self.df.index.nlevels + 1
1678+
1679+
@property
1680+
def shape(self):
1681+
shape = [len(level) for level in self.df.index.levels]
1682+
shape.append(len(self.df.columns))
1683+
return tuple(shape)
1684+
1685+
def copy(self):
1686+
return DataFrameWrapper(self.df.copy())
1687+
1688+
# not caught by __getattr__?
1689+
def __len__(self):
1690+
return self.shape[0]
1691+
1692+
def __array__(self, dtype=None):
1693+
return self.df.__array__(dtype).reshape(self.shape)
1694+
16611695

16621696
#TODO: implement sort_columns
16631697
def df_aslarray2(df, sort_rows=True, sort_columns=True, **kwargs):

larray/tests/test_la.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import larray
1111
from larray import (LArray, Axis, ValueGroup, union, to_ticks, to_key,
1212
srange, larray_equal, read_csv, read_hdf, df_aslarray,
13-
zeros, zeros_like, AxisCollection)
13+
zeros, zeros_like, AxisCollection, DataFrameWrapper)
1414
from larray.utils import array_equal, array_nan_equal
1515

1616

@@ -509,11 +509,23 @@ def setUp(self):
509509

510510
self.array = np.arange(116 * 44 * 2 * 15).reshape(116, 44, 2, 15) \
511511
.astype(float)
512-
self.larray = LArray(self.array,
513-
axes=(self.age, self.geo, self.sex, self.lipro))
512+
idx = pd.MultiIndex.from_product([self.age.labels, self.geo.labels,
513+
self.sex.labels])
514+
dfarray = self.array.reshape(116 * 44 * 2, 15)
515+
df = pd.DataFrame(dfarray, idx, columns=self.lipro.labels)
516+
wrapped = DataFrameWrapper(df)
517+
self.larray = LArray(wrapped, (self.age, self.geo, self.sex,
518+
self.lipro))
519+
# self.larray = LArray(self.array,
520+
# axes=(self.age, self.geo, self.sex, self.lipro))
521+
# self.larray = read_hdf('c:/tmp/y.h5', 'y', sort_rows=False)
514522

515523
self.small_data = np.arange(30).reshape(2, 15)
516-
self.small = LArray(self.small_data, axes=(self.sex, self.lipro))
524+
df = pd.DataFrame(self.small_data, self.sex.labels,
525+
columns=self.lipro.labels)
526+
self.small = LArray(DataFrameWrapper(df), (self.sex, self.lipro))
527+
# self.small = LArray(self.small_data, axes=(self.sex, self.lipro))
528+
# self.small = read_hdf('c:/tmp/x.h5', 'x', sort_rows=False)
517529

518530
def test_zeros(self):
519531
la = zeros((self.geo, self.age))

0 commit comments

Comments
 (0)