Skip to content

BUG: fixes issue in HDFStore w.r.t. compressed empty sparse series (GH #2931) #2933

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
Feb 26, 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
33 changes: 22 additions & 11 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,16 @@ def read_index_node(self, node):

return name, index


def write_array_empty(self, key, value):
""" write a 0-len array """

# ugly hack for length 0 axes
arr = np.empty((1,) * value.ndim)
self._handle.createArray(self.group, key, arr)
getattr(self.group, key)._v_attrs.value_type = str(value.dtype)
getattr(self.group, key)._v_attrs.shape = value.shape

def write_array(self, key, value):
if key in self.group:
self._handle.removeNode(self.group, key)
Expand All @@ -1618,11 +1628,16 @@ def write_array(self, key, value):

if atom is not None:
# create an empty chunked array and fill it from value
ca = self._handle.createCArray(self.group, key, atom,
value.shape,
filters=self._filters)
ca[:] = value
getattr(self.group, key)._v_attrs.transposed = transposed
if not empty_array:
ca = self._handle.createCArray(self.group, key, atom,
value.shape,
filters=self._filters)
ca[:] = value
getattr(self.group, key)._v_attrs.transposed = transposed

else:
self.write_array_empty(key, value)

return

if value.dtype.type == np.object_:
Expand All @@ -1645,11 +1660,7 @@ def write_array(self, key, value):
getattr(self.group, key)._v_attrs.value_type = 'datetime64'
else:
if empty_array:
# ugly hack for length 0 axes
arr = np.empty((1,) * value.ndim)
self._handle.createArray(self.group, key, arr)
getattr(self.group, key)._v_attrs.value_type = str(value.dtype)
getattr(self.group, key)._v_attrs.shape = value.shape
self.write_array_empty(key, value)
else:
self._handle.createArray(self.group, key, value)

Expand Down Expand Up @@ -1720,7 +1731,7 @@ def write(self, obj, **kwargs):
self.write_index('sp_index', obj.sp_index)
self.write_array('sp_values', obj.sp_values)
self.attrs.name = obj.name
self.attrs.dill_value = obj.fill_value
self.attrs.fill_value = obj.fill_value
self.attrs.kind = obj.kind

class SparseFrameStorer(GenericStorer):
Expand Down
33 changes: 32 additions & 1 deletion pandas/io/tests/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,7 @@ def test_sparse_frame(self):
s.ix[3:5, 1:3] = np.nan
s.ix[8:10, -2] = np.nan
ss = s.to_sparse()

self._check_double_roundtrip(ss, tm.assert_frame_equal,
check_frame_type=True)

Expand Down Expand Up @@ -1565,6 +1566,36 @@ def test_overwrite_node(self):

tm.assert_series_equal(store['a'], ts)

def test_sparse_with_compression(self):

# GH 2931

# make sparse dataframe
df = DataFrame(np.random.binomial(n=1, p=.01, size=(1e3, 10))).to_sparse(fill_value=0)

# case 1: store uncompressed
self._check_double_roundtrip(df, tm.assert_frame_equal,
compression = False,
check_frame_type=True)

# case 2: store compressed (works)
self._check_double_roundtrip(df, tm.assert_frame_equal,
compression = 'zlib',
check_frame_type=True)

# set one series to be completely sparse
df[0] = np.zeros(1e3)

# case 3: store df with completely sparse series uncompressed
self._check_double_roundtrip(df, tm.assert_frame_equal,
compression = False,
check_frame_type=True)

# case 4: try storing df with completely sparse series compressed (fails)
self._check_double_roundtrip(df, tm.assert_frame_equal,
compression = 'zlib',
check_frame_type=True)

def test_select(self):
wp = tm.makePanel()

Expand Down Expand Up @@ -1967,7 +1998,7 @@ def _check_double_roundtrip(self, obj, comparator, compression=False,
**kwargs):
options = {}
if compression:
options['complib'] = _default_compressor
options['complib'] = compression or _default_compressor

with ensure_clean(self.path, 'w', **options) as store:
store['obj'] = obj
Expand Down