Skip to content

Commit 5c7e849

Browse files
committed
BUG: support multiple data columns that are in the same block (e.g. the same type)
e.g. self.store.select('df', [ Term('string', '=', 'foo'), Term('string2=foo'), Term('A>0'), Term('B<0') ])
1 parent 9408d59 commit 5c7e849

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

pandas/io/pytables.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,19 +1568,19 @@ def create_axes(self, axes, obj, validate = True, nan_rep = None, columns = None
15681568

15691569
# get out blocks
15701570
block_obj = self.get_object(obj)
1571+
blocks = None
15711572

1572-
data_obj = None
15731573
if columns is not None and len(self.non_index_axes):
15741574
axis = self.non_index_axes[0][0]
15751575
axis_labels = self.non_index_axes[0][1]
15761576
columns = [ c for c in columns if c in axis_labels ]
15771577
if len(columns):
1578-
data_obj = block_obj.reindex_axis(Index(columns), axis = axis, copy = False)
1579-
block_obj = block_obj.reindex_axis(Index(axis_labels)-Index(columns), axis = axis, copy = False)
1578+
blocks = block_obj.reindex_axis(Index(axis_labels)-Index(columns), axis = axis, copy = False)._data.blocks
1579+
for c in columns:
1580+
blocks.extend(block_obj.reindex_axis([ c ], axis = axis, copy = False)._data.blocks)
15801581

1581-
blocks = list(block_obj._data.blocks)
1582-
if data_obj is not None:
1583-
blocks.extend(data_obj._data.blocks)
1582+
if blocks is None:
1583+
blocks = block_obj._data.blocks
15841584

15851585
# add my values
15861586
self.values_axes = []

pandas/io/tests/test_pytables.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,25 @@ def test_append_with_data_columns(self):
460460
expected = df_new[df_new.string == 'foo']
461461
tm.assert_frame_equal(result, expected)
462462

463+
# multiple data columns
464+
df_new = df.copy()
465+
df_new['string'] = 'foo'
466+
df_new['string'][1:4] = np.nan
467+
df_new['string'][5:6] = 'bar'
468+
df_new['string2'] = 'foo'
469+
df_new['string2'][2:5] = np.nan
470+
df_new['string2'][7:8] = 'bar'
471+
self.store.remove('df')
472+
self.store.append('df', df_new, columns = ['A','B','string','string2'])
473+
result = self.store.select('df', [ Term('string', '=', 'foo'), Term('string2=foo'), Term('A>0'), Term('B<0') ])
474+
expected = df_new[(df_new.string == 'foo') & (df_new.string2 == 'foo') & (df_new.A > 0) & (df_new.B < 0)]
475+
tm.assert_frame_equal(result, expected)
476+
477+
# yield an empty frame
478+
result = self.store.select('df', [ Term('string', '=', 'foo'), Term('string2=bar'), Term('A>0'), Term('B<0') ])
479+
expected = df_new[(df_new.string == 'foo') & (df_new.string2 == 'bar') & (df_new.A > 0) & (df_new.B < 0)]
480+
tm.assert_frame_equal(result, expected)
481+
463482
def test_create_table_index(self):
464483
wp = tm.makePanel()
465484
self.store.append('p5', wp)

0 commit comments

Comments
 (0)