Skip to content

BUG: proper conversion of string to bool HDFStore selection #2855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,14 @@ def __repr__(self):
values = []

for k in self.keys():
s = self.get_storer(k)
if s is not None:
keys.append(str(s.pathname or k))
values.append(str(s or 'invalid_HDFStore node'))
try:
s = self.get_storer(k)
if s is not None:
keys.append(str(s.pathname or k))
values.append(str(s or 'invalid_HDFStore node'))
except (Exception), detail:
keys.append(k)
values.append("[invalid_HDFStore node: %s]" % str(detail))

output += adjoin(12, keys, values)
else:
Expand Down Expand Up @@ -3060,7 +3064,10 @@ def convert_value(self, v):
v = float(v)
return [v, v]
elif self.kind == 'bool':
v = bool(v)
if isinstance(v, basestring):
v = not str(v).strip().lower() in ["false", "f", "no", "n", "none", "0", "[]", "{}", ""]
else:
v = bool(v)
return [v, v]
elif not isinstance(v, basestring):
return [str(v), None]
Expand Down
14 changes: 10 additions & 4 deletions pandas/io/tests/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1649,15 +1649,20 @@ def test_select_dtypes(self):
df['bool'] = df['A'] > 0
store.remove('df')
store.append('df', df, data_columns = True)
result = store.select('df', Term('bool == True'), columns = ['A','bool'])

expected = df[df.bool == True].reindex(columns=['A','bool'])
tm.assert_frame_equal(expected, result)
for v in [True,'true',1]:
result = store.select('df', Term('bool == %s' % str(v)), columns = ['A','bool'])
tm.assert_frame_equal(expected, result)

result = store.select('df', Term('bool == 1'), columns = ['A','bool'])
tm.assert_frame_equal(expected, result)
expected = df[df.bool == False ].reindex(columns=['A','bool'])
for v in [False,'false',0]:
result = store.select('df', Term('bool == %s' % str(v)), columns = ['A','bool'])
tm.assert_frame_equal(expected, result)

# integer index
df = DataFrame(dict(A=np.random.rand(20), B=np.random.rand(20)))
store.remove('df_int')
store.append('df_int', df)
result = store.select(
'df_int', [Term("index<10"), Term("columns", "=", ["A"])])
Expand All @@ -1667,6 +1672,7 @@ def test_select_dtypes(self):
# float index
df = DataFrame(dict(A=np.random.rand(
20), B=np.random.rand(20), index=np.arange(20, dtype='f8')))
store.remove('df_float')
store.append('df_float', df)
result = store.select(
'df_float', [Term("index<10.0"), Term("columns", "=", ["A"])])
Expand Down