Skip to content

Commit 7c7100d

Browse files
committed
expand test cases
1 parent 946a48e commit 7c7100d

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

doc/source/whatsnew/v0.20.0.txt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -461,13 +461,20 @@ New Behavior:
461461

462462
.. code-block:: ipython
463463

464-
df = pd.DataFrame({'unparsed_date': ['2014-01-01', '2014-01-01']})
465-
df.to_hdf('store.h5', 'key', format='table', data_columns=True)
466-
ts = pd.Timestamp('2014-01-01')
467-
try:
468-
pd.read_hdf('store.h5', 'key', where='unparsed_date > ts')
469-
except TypeError:
470-
print("TypeError raised")
464+
In [15]: df = pd.DataFrame({'unparsed_date': ['2014-01-01', '2014-01-01']})
465+
466+
In [16]: df.dtypes
467+
Out[16]:
468+
unparsed_date object
469+
dtype: object
470+
471+
In [17]: df.to_hdf('store.h5', 'key', format='table', data_columns=True)
472+
473+
In [18]: ts = pd.Timestamp('2014-01-01')
474+
475+
In [19]: pd.read_hdf('store.h5', 'key', where='unparsed_date > ts')
476+
TypeError: Cannot compare 2014-01-01 00:00:00 of
477+
type <class 'pandas.tslib.Timestamp'> to string column
471478

472479

473480
.. _whatsnew_0200.api:

pandas/tests/io/test_pytables.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5080,12 +5080,13 @@ def test_query_long_float_literal(self):
50805080
expected = df.loc[[1], :]
50815081
tm.assert_frame_equal(expected, result)
50825082

5083-
def test_query_compare_string_column(self):
5083+
def test_query_compare_column_type(self):
50845084
# GH 15492
50855085
df = pd.DataFrame({'date': ['2014-01-01', '2014-01-02'],
50865086
'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'])
50895090

50905091
with ensure_clean_store(self.path) as store:
50915092
store.append('test', df, format='table', data_columns=True)
@@ -5095,12 +5096,34 @@ def test_query_compare_string_column(self):
50955096
expected = df.loc[[1], :]
50965097
tm.assert_frame_equal(expected, result)
50975098

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')]:
51005103
query = 'date {op} v'.format(op=op)
51015104
with tm.assertRaises(TypeError):
51025105
result = store.select('test', where=query)
51035106

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+
51045127

51055128
class TestHDFComplexValues(Base):
51065129
# GH10447

0 commit comments

Comments
 (0)