Skip to content

BUG: long str unconvert fix #6166

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

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 9 additions & 2 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@
import pandas.tslib as tslib

from contextlib import contextmanager
from distutils.version import LooseVersion

# versioning attribute
_version = '0.10.1'

# PY3 encoding if we don't specify
_default_encoding = 'UTF-8'

_np_version_under_172 = LooseVersion(np.__version__) < '1.7.2'

def _ensure_decoded(s):
""" if we have bytes, decode them to unicde """
Expand Down Expand Up @@ -4165,7 +4167,6 @@ def _convert_string_array(data, encoding, itemsize=None):
data = np.array(data, dtype="S%d" % itemsize)
return data


def _unconvert_string_array(data, nan_rep=None, encoding=None):
""" deserialize a string array, possibly decoding """
shape = data.shape
Expand All @@ -4175,8 +4176,14 @@ def _unconvert_string_array(data, nan_rep=None, encoding=None):
# where the passed encoding is actually None)
encoding = _ensure_encoding(encoding)
if encoding is not None and len(data):
if _np_version_under_172:
itemsize = lib.max_len_string_array(data)
dtype = (str, itemsize)
else:
dtype = str

try:
data = data.astype(string_types).astype(object)
data = data.astype(dtype).astype(object)
except:
f = np.vectorize(lambda x: x.decode(encoding), otypes=[np.object])
data = f(data)
Expand Down
11 changes: 11 additions & 0 deletions pandas/io/tests/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ def roundtrip(key, obj,**kwargs):
finally:
safe_remove(self.path)

def test_long_strings(self):
df = DataFrame({'a': [tm.rands(100) for _ in range(10)]},
index=[tm.rands(100) for _ in range(10)])

with ensure_clean_store(self.path) as store:
store.append('df', df, data_columns=['a'])

result = store.select('df')
assert_frame_equal(df, result)


def test_api(self):

# GH4584
Expand Down