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 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: 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
22 changes: 22 additions & 0 deletions pandas/io/tests/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import warnings
import tempfile
from contextlib import contextmanager
from tempfile import NamedTemporaryFile

import datetime
import numpy as np
Expand Down Expand Up @@ -4623,6 +4624,27 @@ def _test_sort(obj):
else:
raise ValueError('type not supported here')

class TestToHdfWithIntegerColumnNames(tm.TestCase):
# GH9057
def setUp(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

pls test the way everything else is done
eg use ensure_clean for a file

Copy link
Contributor

Choose a reason for hiding this comment

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

pls give a test with Unicode column names

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',
data_columns=True)

def test_file_is_openable(self):

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

Choose a reason for hiding this comment

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

test that the read back works with regular selection (look at other tests for how to do this)
further need a test with a query, which uses a stringified column

assert stuff

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


if __name__ == '__main__':
import nose
Expand Down
2 changes: 1 addition & 1 deletion 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 Down Expand Up @@ -14204,7 +14205,6 @@ def _constructor(self):
# GH9776
self.assertEqual(df.iloc[0:1, :].testattr, 'XXX')


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