Skip to content

str'ified column names in DataFrame.to_hdf(); Fixes #9057 #9902

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 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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: 10 additions & 1 deletion pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1766,7 +1766,7 @@ def set_kind(self):

# set my typ if we need
if self.typ is None:
self.typ = getattr(self.description, self.cname, None)
self.typ = getattr(self.description, str(self.cname), None)

def set_atom(self, block, block_items, existing_col, min_itemsize,
nan_rep, info, encoding=None, **kwargs):
Expand Down Expand Up @@ -3018,6 +3018,8 @@ def write_metadata(self, key, values):

def read_metadata(self, key):
""" return the meta data array for this key """
if str(key) != key:
key = str(key)
if getattr(getattr(self.group,'meta',None),key,None) is not None:
return self.parent.select(self._get_metadata_path(key))
return None
Expand Down Expand Up @@ -3157,6 +3159,8 @@ def create_index(self, columns=None, optlevel=None, kind=None):

table = self.table
for c in columns:
if str(c) != c:
c = str(c)
v = getattr(table.cols, c, None)
if v is not None:

Expand Down Expand Up @@ -3519,6 +3523,11 @@ def create_description(self, complib=None, complevel=None,
# description from the axes & values
d['description'] = dict([(a.cname, a.typ) for a in self.axes])

# keys to pytables columns must be strings
for a in self.axes:
if str(a.cname) != a.cname:
d['description'][str(a.cname)] = d['description'].pop(a.cname)

if complib:
if complevel is None:
complevel = self._complevel or 9
Expand Down
31 changes: 31 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from copy import deepcopy
from datetime import datetime, timedelta, time
import sys
import os
import operator
import re
import csv
Expand All @@ -13,6 +14,7 @@
import itertools
from itertools import product, permutations
from distutils.version import LooseVersion
from tempfile import NamedTemporaryFile

from pandas.compat import(
map, zip, range, long, lrange, lmap, lzip,
Expand Down Expand Up @@ -14204,6 +14206,35 @@ def _constructor(self):
# GH9776
self.assertEqual(df.iloc[0:1, :].testattr, 'XXX')

def skip_if_no_pytables():
try:
import tables
except ImportError:
raise nose.SkipTest("cannot check test results without pytables")


class TestToHdfWithIntegerColumnNames(tm.TestCase):
# GH9057
def setUp(self):
N = 2
array = np.random.randint(0,8, size=N*N).astype('uint8').reshape(N,-1)
df = DataFrame(array, index=pd.date_range('20130206',
periods=N,freq='ms'))

self.filename = NamedTemporaryFile(suffix='.h5', delete=False).name
df.to_hdf(self.filename,'df', mode='w', format='table',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these all need to go in io/tests/test_pytables.py

data_columns=True)

def test_file_is_openable(self):
skip_if_no_pytables()
import tables

with tables.open_file(self.filename, 'r') as myfile:
stuff = myfile.root.df
assert stuff

def tearDown(self):
os.remove(self.filename)

def skip_if_no_ne(engine='numexpr'):
if engine == 'numexpr':
Expand Down