Skip to content

BUG: (GH4708) A zero length series written to HDF cannot be read back. #4709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
BUG: (GH4708) A zero length series written to HDF cannot be read back.
  • Loading branch information
prossahl committed Oct 15, 2013
commit 45417bb6f7cd2e4242e6dae4bc73e48d9dc7e40d
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ API Changes

- ``HDFStore``

- A zero length series written to HDF cannot be read back. (:issue:`4708`)
- ``append_to_multiple`` automatically synchronizes writing rows to multiple
tables and adds a ``dropna`` kwarg (:issue:`4698`)
- handle a passed ``Series`` in table format (:issue:`4330`)
Expand Down
32 changes: 19 additions & 13 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2213,6 +2213,11 @@ def read_multi_index(self, key):

def read_index_node(self, node):
data = node[:]
# If the index was an empty array write_array_empty() will
# have written a sentinel. Here we relace it with the original.
if 'shape' in node._v_attrs \
and self._is_empty_array(getattr(node._v_attrs, 'shape')):
data = np.empty(getattr(node._v_attrs, 'shape'), dtype=getattr(node._v_attrs, 'value_type'))
kind = _ensure_decoded(node._v_attrs.kind)
name = None

Expand Down Expand Up @@ -2251,12 +2256,16 @@ def write_array_empty(self, key, value):
getattr(self.group, key)._v_attrs.value_type = str(value.dtype)
getattr(self.group, key)._v_attrs.shape = value.shape

def _is_empty_array(self, shape):
"""Returns true if any axis is zero length."""
return any(x == 0 for x in shape)

def write_array(self, key, value, items=None):
if key in self.group:
self._handle.removeNode(self.group, key)

# Transform needed to interface with pytables row/col notation
empty_array = any(x == 0 for x in value.shape)
empty_array = self._is_empty_array(value.shape)
transposed = False

if not empty_array:
Expand Down Expand Up @@ -2305,17 +2314,18 @@ def write_array(self, key, value, items=None):
vlarr = self._handle.createVLArray(self.group, key,
_tables().ObjectAtom())
vlarr.append(value)
elif value.dtype.type == np.datetime64:
self._handle.createArray(self.group, key, value.view('i8'))
getattr(self.group, key)._v_attrs.value_type = 'datetime64'
elif value.dtype.type == np.timedelta64:
self._handle.createArray(self.group, key, value.view('i8'))
getattr(self.group, key)._v_attrs.value_type = 'timedelta64'
else:
if empty_array:
self.write_array_empty(key, value)
else:
self._handle.createArray(self.group, key, value)
if value.dtype.type == np.datetime64:
self._handle.createArray(self.group, key, value.view('i8'))
getattr(self.group, key)._v_attrs.value_type = 'datetime64'
elif value.dtype.type == np.timedelta64:
self._handle.createArray(self.group, key, value.view('i8'))
getattr(self.group, key)._v_attrs.value_type = 'timedelta64'
else:
self._handle.createArray(self.group, key, value)

getattr(self.group, key)._v_attrs.transposed = transposed

Expand Down Expand Up @@ -2362,11 +2372,7 @@ def shape(self):
def read(self, **kwargs):
self.validate_read(kwargs)
index = self.read_index('index')
if len(index) > 0:
values = self.read_array('values')
else:
values = []

values = self.read_array('values')
return Series(values, index=index, name=self.name)

def write(self, obj, **kwargs):
Expand Down
5 changes: 5 additions & 0 deletions pandas/io/tests/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2358,6 +2358,11 @@ def test_empty_series_frame(self):
self._check_roundtrip(df1, tm.assert_frame_equal)
self._check_roundtrip(df2, tm.assert_frame_equal)

def test_empty_series(self):
for dtype in [np.int64, np.float64, np.object, 'm8[ns]', 'M8[ns]']:
s = Series(dtype=dtype)
self._check_roundtrip(s, tm.assert_series_equal)

def test_can_serialize_dates(self):

rng = [x.date() for x in bdate_range('1/1/2000', '1/30/2000')]
Expand Down