diff --git a/pandas/computation/pytables.py b/pandas/computation/pytables.py index 7716bc0051159..bf477cd71df62 100644 --- a/pandas/computation/pytables.py +++ b/pandas/computation/pytables.py @@ -4,8 +4,8 @@ import time import warnings from functools import partial -from datetime import datetime - +from datetime import datetime, timedelta +import numpy as np import pandas as pd from pandas.compat import u, string_types, PY3 from pandas.core.base import StringMixin @@ -540,6 +540,18 @@ def parse_back_compat(self, w, op=None, value=None): if value is not None: if isinstance(value, Expr): raise TypeError("invalid value passed, must be a string") + + # stringify with quotes these values + def convert(v): + if isinstance(v, (datetime,np.datetime64,timedelta,np.timedelta64)) or hasattr(v, 'timetuple'): + return "'{0}'".format(v) + return v + + if isinstance(value, (list,tuple)): + value = [ convert(v) for v in value ] + else: + value = convert(value) + w = "{0}{1}".format(w, value) warnings.warn("passing multiple values to Expr is deprecated, " diff --git a/pandas/io/tests/test_pytables.py b/pandas/io/tests/test_pytables.py index c9955b1ae2fb2..5fcafdc295c5c 100644 --- a/pandas/io/tests/test_pytables.py +++ b/pandas/io/tests/test_pytables.py @@ -2347,6 +2347,30 @@ def test_term_compat(self): expected = wp.loc[:,wp.major_axis<=Timestamp('20000103'),:] assert_panel_equal(result, expected) + with ensure_clean_store(self.path) as store: + + wp = Panel(np.random.randn(2, 5, 4), items=['Item1', 'Item2'], + major_axis=date_range('1/1/2000', periods=5), + minor_axis=['A', 'B', 'C', 'D']) + store.append('wp',wp) + + # stringified datetimes + result = store.select('wp', [Term('major_axis','>',datetime.datetime(2000,1,2))]) + expected = wp.loc[:,wp.major_axis>Timestamp('20000102')] + assert_panel_equal(result, expected) + + result = store.select('wp', [Term('major_axis','>',datetime.datetime(2000,1,2,0,0))]) + expected = wp.loc[:,wp.major_axis>Timestamp('20000102')] + assert_panel_equal(result, expected) + + result = store.select('wp', [Term('major_axis','=',[datetime.datetime(2000,1,2,0,0),datetime.datetime(2000,1,3,0,0)])]) + expected = wp.loc[:,[Timestamp('20000102'),Timestamp('20000103')]] + assert_panel_equal(result, expected) + + result = store.select('wp', [Term('minor_axis','=',['A','B'])]) + expected = wp.loc[:,:,['A','B']] + assert_panel_equal(result, expected) + def test_same_name_scoping(self): with ensure_clean_store(self.path) as store: