From 80b5082a2c2a39ac9613e30168a18706a4d3088c Mon Sep 17 00:00:00 2001 From: John-Colvin Date: Mon, 3 Sep 2012 20:53:50 +0200 Subject: [PATCH 1/2] fixed row/col orientation for 2D arrays Pytables createArray function uses the opposite convention to pandas for row/col. Using the numpy transpose .T corrects for this without copying the data and keeping the orientation of 1D arrays intact. --- pandas/io/pytables.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index d116337c80e4d..b7dce3d5c758a 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -686,7 +686,10 @@ def _read_index_node(self, node): def _write_array(self, group, key, value): if key in group: self.handle.removeNode(group, key) - + + #Transform needed to interface with pytables row/col notation + value = value.T + if self.filters is not None: atom = None try: From 4ff42dbcd4cc430f504cd8e53067581575fd949c Mon Sep 17 00:00:00 2001 From: John-Colvin Date: Mon, 3 Sep 2012 22:18:20 +0200 Subject: [PATCH 2/2] added tranposed attribute, used in _read_array MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit the transposed attribute keeps track of whether the data has beenĀ  transposed, allowing it to be correctly read by _read_array. Some minor rearrangement of _read_array was necessary to reduce duplicate code. --- pandas/io/pytables.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index b7dce3d5c758a..5820e447e5df5 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -704,6 +704,7 @@ def _write_array(self, group, key, value): value.shape, filters=self.filters) ca[:] = value + getattr(group, key)._v_attrs.transposed = True return if value.dtype.type == np.object_: @@ -722,6 +723,8 @@ def _write_array(self, group, key, value): getattr(group, key)._v_attrs.shape = value.shape else: self.handle.createArray(group, key, value) + + getattr(group, key)._v_attrs.transposed = True def _write_table(self, group, items=None, index=None, columns=None, values=None, append=False, compression=None): @@ -961,23 +964,29 @@ def _read_array(group, key): import tables node = getattr(group, key) data = node[:] + attrs = node._v_attrs + + transposed = getattr(attrs, 'transposed', False) if isinstance(node, tables.VLArray): - return data[0] + ret = data[0] else: - attrs = node._v_attrs - dtype = getattr(attrs, 'value_type', None) shape = getattr(attrs, 'shape', None) if shape is not None: # length 0 axis - return np.empty(shape, dtype=dtype) + ret = np.empty(shape, dtype=dtype) if dtype == 'datetime64': - return np.array(data, dtype='M8[ns]') - return data - + ret = np.array(data, dtype='M8[ns]') + ret = data + + if transposed == True: + return ret.T + else: + return ret + def _unconvert_index(data, kind): if kind == 'datetime64': index = DatetimeIndex(data)