Skip to content

Commit b64e81e

Browse files
committed
BUG: GH3493 Fix cannot append DataFrames with uint dtypes to HDFStore
1 parent 77618f0 commit b64e81e

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

pandas/io/pytables.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -1284,8 +1284,17 @@ def set_atom_string(self, block, existing_col, min_itemsize, nan_rep):
12841284
def convert_string_data(self, data, itemsize):
12851285
return data.astype('S%s' % itemsize)
12861286

1287+
def get_atom_coltype(self):
1288+
""" return the PyTables column class for this column """
1289+
if self.kind.startswith('uint'):
1290+
col_name = "UInt%sCol" % self.kind[4:]
1291+
else:
1292+
col_name = "%sCol" % self.kind.capitalize()
1293+
1294+
return getattr(_tables(), col_name)
1295+
12871296
def get_atom_data(self, block):
1288-
return getattr(_tables(), "%sCol" % self.kind.capitalize())(shape=block.shape[0])
1297+
return self.get_atom_coltype()(shape=block.shape[0])
12891298

12901299
def set_atom_data(self, block):
12911300
self.kind = block.dtype.name
@@ -1383,7 +1392,7 @@ def get_atom_string(self, block, itemsize):
13831392
return _tables().StringCol(itemsize=itemsize)
13841393

13851394
def get_atom_data(self, block):
1386-
return getattr(_tables(), "%sCol" % self.kind.capitalize())()
1395+
return self.get_atom_coltype()()
13871396

13881397
def get_atom_datetime64(self, block):
13891398
return _tables().Int64Col()

pandas/io/tests/test_pytables.py

+15
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,21 @@ def test_append(self):
458458
store.append('df', df)
459459
tm.assert_frame_equal(store['df'], df)
460460

461+
# uints - test storage of uints
462+
uint_data = DataFrame({'u08' : Series(np.random.random_integers(0, high=255, size=5), dtype=np.uint8),
463+
'u16' : Series(np.random.random_integers(0, high=65535, size=5), dtype=np.uint16),
464+
'u32' : Series(np.random.random_integers(0, high=2**30, size=5), dtype=np.uint32),
465+
'u64' : Series([2**58, 2**59, 2**60, 2**61, 2**62], dtype=np.uint64)},
466+
index=np.arange(5))
467+
_maybe_remove(store, 'uints')
468+
store.append('uints', uint_data)
469+
tm.assert_frame_equal(store['uints'], uint_data)
470+
471+
# uints - test storage of uints in indexable columns
472+
_maybe_remove(store, 'uints')
473+
store.append('uints', uint_data, data_columns=['u08','u16','u32']) # 64-bit indices not yet supported
474+
tm.assert_frame_equal(store['uints'], uint_data)
475+
461476
def test_append_some_nans(self):
462477

463478
with ensure_clean(self.path) as store:

0 commit comments

Comments
 (0)