improvements to Table class
- string comparison should be '!=' not 'is not', latter compares object, not value - functions for common operations: append (vstack, growTable) and join (hstack, addTable)
This commit is contained in:
parent
c3c08b5b53
commit
2cb3213e37
|
@ -248,17 +248,17 @@ class Table():
|
||||||
'' if info is None else ': {}'.format(info),
|
'' if info is None else ': {}'.format(info),
|
||||||
))
|
))
|
||||||
|
|
||||||
self.shapes = {(label if label is not label_old else label_new):self.shapes[label] for label in self.shapes}
|
self.shapes = {(label if label != label_old else label_new):self.shapes[label] for label in self.shapes}
|
||||||
|
|
||||||
|
|
||||||
def sort_by(self,labels,ascending=True):
|
def sort_by(self,labels,ascending=True):
|
||||||
"""
|
"""
|
||||||
Get column data.
|
Sort table by values of given labels.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
label : str or list
|
label : str or list
|
||||||
Column labels.
|
Column labels for sorting.
|
||||||
ascending : bool or list, optional
|
ascending : bool or list, optional
|
||||||
Set sort order.
|
Set sort order.
|
||||||
|
|
||||||
|
@ -269,6 +269,41 @@ class Table():
|
||||||
self.comments.append('sorted by [{}]'.format(', '.join(labels)))
|
self.comments.append('sorted by [{}]'.format(', '.join(labels)))
|
||||||
|
|
||||||
|
|
||||||
|
def append(self,other):
|
||||||
|
"""
|
||||||
|
Append other table vertically (similar to numpy.vstack). Requires matching shapes and order.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
other : Table
|
||||||
|
Table to append
|
||||||
|
|
||||||
|
"""
|
||||||
|
if self.shapes != other.shapes or not self.data.columns.equals(other.data.columns):
|
||||||
|
raise KeyError('Labels or shapes or order do not match')
|
||||||
|
else:
|
||||||
|
self.data = self.data.append(other.data,ignore_index=True)
|
||||||
|
|
||||||
|
|
||||||
|
def join(self,other):
|
||||||
|
"""
|
||||||
|
Append other table horizontally (similar to numpy.hstack). Requires matching number of rows
|
||||||
|
and no common lables
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
other : Table
|
||||||
|
Table to join
|
||||||
|
|
||||||
|
"""
|
||||||
|
if set(self.shapes) & set(other.shapes) or self.data.shape[0] != other.data.shape[0]:
|
||||||
|
raise KeyError('Dublicated keys or row count mismatch')
|
||||||
|
else:
|
||||||
|
self.data = self.data.join(other.data)
|
||||||
|
for key in other.shapes:
|
||||||
|
self.shapes[key] = other.shapes[key]
|
||||||
|
|
||||||
|
|
||||||
def to_ASCII(self,fname):
|
def to_ASCII(self,fname):
|
||||||
"""
|
"""
|
||||||
Store as plain text file.
|
Store as plain text file.
|
||||||
|
|
|
@ -86,14 +86,42 @@ class TestTable:
|
||||||
|
|
||||||
def test_rename_gone(self,default):
|
def test_rename_gone(self,default):
|
||||||
default.rename('v','V')
|
default.rename('v','V')
|
||||||
|
assert 'v' not in default.shapes and 'v' not in default.data.columns
|
||||||
with pytest.raises(KeyError):
|
with pytest.raises(KeyError):
|
||||||
default.get('v')
|
default.get('v')
|
||||||
|
|
||||||
def test_delete(self,default):
|
def test_delete(self,default):
|
||||||
default.delete('v')
|
default.delete('v')
|
||||||
|
assert 'v' not in default.shapes and 'v' not in default.data.columns
|
||||||
with pytest.raises(KeyError):
|
with pytest.raises(KeyError):
|
||||||
default.get('v')
|
default.get('v')
|
||||||
|
|
||||||
|
def test_join(self):
|
||||||
|
x = np.random.random((5,13))
|
||||||
|
a = Table(x,{'F':(3,3),'v':(3,),'s':(1,)},['random test data'])
|
||||||
|
y = np.random.random((5,3))
|
||||||
|
b = Table(y,{'u':(3,)},['random test data'])
|
||||||
|
a.join(b)
|
||||||
|
assert np.array_equal(a.get('u'), b.get('u'))
|
||||||
|
|
||||||
|
def test_join_invalid(self):
|
||||||
|
x = np.random.random((5,13))
|
||||||
|
a = Table(x,{'F':(3,3),'v':(3,),'s':(1,)},['random test data'])
|
||||||
|
with pytest.raises(KeyError):
|
||||||
|
a.join(a)
|
||||||
|
|
||||||
|
def test_append(self):
|
||||||
|
x = np.random.random((5,13))
|
||||||
|
a = Table(x,{'F':(3,3),'v':(3,),'s':(1,)},['random test data'])
|
||||||
|
a.append(a)
|
||||||
|
assert np.array_equal(a.data[:5].to_numpy(),a.data[5:].to_numpy())
|
||||||
|
|
||||||
|
def test_append_invalid(self):
|
||||||
|
x = np.random.random((5,13))
|
||||||
|
a = Table(x,{'F':(3,3),'v':(3,),'s':(1,)},['random test data'])
|
||||||
|
b = Table(x,{'F':(3,3),'u':(3,),'s':(1,)},['random test data'])
|
||||||
|
with pytest.raises(KeyError):
|
||||||
|
a.append(b)
|
||||||
|
|
||||||
def test_invalid_initialization(self):
|
def test_invalid_initialization(self):
|
||||||
x = np.random.random((5,10))
|
x = np.random.random((5,10))
|
||||||
|
|
Loading…
Reference in New Issue