Skip to content

Commit bd726d3

Browse files
committed
update v0.10.0 whatsnew docs for PyTables Table features
added additional test cases for Terms in test_pytables.py subtle change to parsing order in Terms (e.g. '<=' would only cause '<' to be picked up)
1 parent 64c2eb6 commit bd726d3

File tree

3 files changed

+99
-18
lines changed

3 files changed

+99
-18
lines changed

doc/source/v0.10.0.txt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,68 @@ enhancements along with a large number of bug fixes.
99
New features
1010
~~~~~~~~~~~~
1111

12+
- Docs for PyTables ``Table`` format & several enhancements to the api. Here is a taste of what to expect. The
13+
14+
`full docs for tables
15+
<https://github.com/pydata/pandas/blob/master/io.html#hdf5-pytables>`__
16+
17+
18+
.. ipython:: python
19+
:suppress:
20+
:okexcept:
21+
22+
os.remove('store.h5')
23+
24+
.. ipython:: python
25+
26+
store = HDFStore('store.h5')
27+
df = DataFrame(randn(8, 3), index=date_range('1/1/2000', periods=8),
28+
columns=['A', 'B', 'C'])
29+
df
30+
31+
# appending data frames
32+
df1 = df[0:4]
33+
df2 = df[4:]
34+
store.append('df', df1)
35+
store.append('df', df2)
36+
store
37+
38+
# selecting the entire store
39+
store.select('df')
40+
41+
.. ipython:: python
42+
:suppress:
43+
44+
store.close()
45+
import os
46+
os.remove('store.h5')
47+
48+
.. ipython:: python
49+
50+
from pandas.io.pytables import Term
51+
store = HDFStore('store.h5')
52+
wp = Panel(randn(2, 5, 4), items=['Item1', 'Item2'],
53+
major_axis=date_range('1/1/2000', periods=5),
54+
minor_axis=['A', 'B', 'C', 'D'])
55+
wp
56+
57+
# storing a panel
58+
store.append('wp',wp)
59+
60+
# selecting via A QUERY
61+
store.select('wp',
62+
[ Term('major_axis>20000102'), Term('minor_axis', '=', ['A','B']) ])
63+
64+
# removing data from tables
65+
store.remove('wp', [ 'major_axis', '>', wp.major_axis[3] ])
66+
store.select('wp')
67+
68+
.. ipython:: python
69+
:suppress:
70+
71+
store.close()
72+
import os
73+
os.remove('store.h5')
1274

1375
API changes
1476
~~~~~~~~~~~

pandas/io/pytables.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,7 +1751,7 @@ class Term(object):
17511751
17521752
"""
17531753

1754-
_ops = ['<','<=','>','>=','=','!=']
1754+
_ops = ['<=','<','>=','>','!=','=']
17551755
_search = re.compile("^(?P<field>\w+)(?P<op>%s)(?P<value>.+)$" % '|'.join(_ops))
17561756
_index = ['index','major_axis','major']
17571757
_column = ['column','minor_axis','minor']
@@ -1765,7 +1765,7 @@ def __init__(self, field, op = None, value = None, kinds = None):
17651765
self.condition = None
17661766

17671767
# unpack lists/tuples in field
1768-
if isinstance(field,(tuple,list)):
1768+
while(isinstance(field,(tuple,list))):
17691769
f = field
17701770
field = f[0]
17711771
if len(f) > 1:
@@ -1923,8 +1923,8 @@ def generate(self, where):
19231923
if not isinstance(where, (list,tuple)):
19241924
where = [ where ]
19251925
else:
1926-
# do we have a single list/tuple
1927-
if not isinstance(where[0], (list,tuple)):
1926+
# do we have all list/tuple
1927+
if not any([ isinstance(w, (list,tuple,Term)) for w in where ]):
19281928
where = [ where ]
19291929

19301930
return [ Term(c, kinds = self.table.kinds_map()) for c in where ]

pandas/io/tests/test_pytables.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -350,29 +350,44 @@ def test_remove_crit(self):
350350

351351
def test_terms(self):
352352

353+
wp = tm.makePanel()
354+
self.store.put('wp', wp, table=True)
355+
356+
# some invalid terms
357+
terms = [
358+
[ 'minor', ['A','B'] ],
359+
[ 'index', ['20121114'] ],
360+
[ 'index', ['20121114', '20121114'] ],
361+
]
362+
for t in terms:
363+
self.assertRaises(Exception, self.store.select, 'wp', t)
364+
365+
self.assertRaises(Exception, Term.__init__)
366+
self.assertRaises(Exception, Term.__init__, 'blah')
367+
self.assertRaises(Exception, Term.__init__, 'index')
368+
self.assertRaises(Exception, Term.__init__, 'index', '==')
369+
self.assertRaises(Exception, Term.__init__, 'index', '>', 5)
370+
371+
result = self.store.select('wp',[ Term('major_axis<20000108'), Term('minor_axis', '=', ['A','B']) ])
372+
expected = wp.truncate(after='20000108').reindex(minor=['A', 'B'])
373+
tm.assert_panel_equal(result, expected)
374+
375+
# valid terms
353376
terms = [
354377
dict(field = 'index', op = '>', value = '20121114'),
355378
('index', '20121114'),
356379
('index', '>', '20121114'),
357-
('index', ['20121114','20121114']),
380+
(('index', ['20121114','20121114']),),
358381
('index', datetime(2012,11,14)),
359382
'index>20121114',
360383
'major>20121114',
361384
'major_axis>20121114',
362-
('minor', ['A','B']),
363-
('minor_axis', ['A','B']),
364-
('column', ['A','B']),
385+
(('minor', ['A','B']),),
386+
(('minor_axis', ['A','B']),),
387+
((('minor_axis', ['A','B']),),),
388+
(('column', ['A','B']),),
365389
]
366390

367-
self.assertRaises(Exception, Term.__init__)
368-
self.assertRaises(Exception, Term.__init__, 'blah')
369-
self.assertRaises(Exception, Term.__init__, 'index')
370-
self.assertRaises(Exception, Term.__init__, 'index', '==')
371-
self.assertRaises(Exception, Term.__init__, 'index', '>', 5)
372-
373-
# test em
374-
wp = tm.makePanel()
375-
self.store.put('wp', wp, table=True)
376391
for t in terms:
377392
self.store.select('wp', t)
378393

@@ -712,12 +727,16 @@ def test_panel_select(self):
712727
date = wp.major_axis[len(wp.major_axis) // 2]
713728

714729
crit1 = ('index','>=',date)
715-
crit2 = ('column', ['A', 'D'])
730+
crit2 = ('column', '=', ['A', 'D'])
716731

717732
result = self.store.select('wp', [crit1, crit2])
718733
expected = wp.truncate(before=date).reindex(minor=['A', 'D'])
719734
tm.assert_panel_equal(result, expected)
720735

736+
result = self.store.select('wp', [ 'major_axis>=20000124', ('minor_axis', '=', ['A','B']) ])
737+
expected = wp.truncate(before='20000124').reindex(minor=['A', 'B'])
738+
tm.assert_panel_equal(result, expected)
739+
721740
def test_frame_select(self):
722741
df = tm.makeTimeDataFrame()
723742
self.store.put('frame', df, table=True)

0 commit comments

Comments
 (0)