@@ -5080,12 +5080,13 @@ def test_query_long_float_literal(self):
5080
5080
expected = df .loc [[1 ], :]
5081
5081
tm .assert_frame_equal (expected , result )
5082
5082
5083
- def test_query_compare_string_column (self ):
5083
+ def test_query_compare_column_type (self ):
5084
5084
# GH 15492
5085
5085
df = pd .DataFrame ({'date' : ['2014-01-01' , '2014-01-02' ],
5086
5086
'real_date' : date_range ('2014-01-01' , periods = 2 ),
5087
- 'values' : [1 , 2 ]},
5088
- columns = ['date' , 'real_date' , 'values' ])
5087
+ 'float' : [1.1 , 1.2 ],
5088
+ 'int' : [1 , 2 ]},
5089
+ columns = ['date' , 'real_date' , 'float' , 'int' ])
5089
5090
5090
5091
with ensure_clean_store (self .path ) as store :
5091
5092
store .append ('test' , df , format = 'table' , data_columns = True )
@@ -5095,12 +5096,34 @@ def test_query_compare_string_column(self):
5095
5096
expected = df .loc [[1 ], :]
5096
5097
tm .assert_frame_equal (expected , result )
5097
5098
5098
- for v in [2.1 , True , pd .Timestamp ('2014-01-01' )]:
5099
- for op in ['<' , '>' , '==' ]:
5099
+ for op in ['<' , '>' , '==' ]:
5100
+ # non strings to string column always fail
5101
+ for v in [2.1 , True , pd .Timestamp ('2014-01-01' ),
5102
+ pd .Timedelta (1 , 's' )]:
5100
5103
query = 'date {op} v' .format (op = op )
5101
5104
with tm .assertRaises (TypeError ):
5102
5105
result = store .select ('test' , where = query )
5103
5106
5107
+ # strings to other columns must be convertible to type
5108
+ v = 'a'
5109
+ for col in ['int' , 'float' , 'real_date' ]:
5110
+ query = '{col} {op} v' .format (op = op , col = col )
5111
+ with tm .assertRaises (ValueError ):
5112
+ result = store .select ('test' , where = query )
5113
+
5114
+ for v , col in zip (['1' , '1.1' , '2014-01-01' ],
5115
+ ['int' , 'float' , 'real_date' ]):
5116
+ query = '{col} {op} v' .format (op = op , col = col )
5117
+ result = store .select ('test' , where = query )
5118
+
5119
+ if op == '==' :
5120
+ expected = df .loc [[0 ], :]
5121
+ elif op == '>' :
5122
+ expected = df .loc [[1 ], :]
5123
+ else :
5124
+ expected = df .loc [[], :]
5125
+ tm .assert_frame_equal (expected , result )
5126
+
5104
5127
5105
5128
class TestHDFComplexValues (Base ):
5106
5129
# GH10447
0 commit comments