Skip to content

Commit fb25ac1

Browse files
committed
BUG: fix numpy 1.6.1 issues; remove need for condvars and use literals in
the numexpr expressions
1 parent 2f7f9bd commit fb25ac1

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

pandas/io/pytables.py

+21-26
Original file line numberDiff line numberDiff line change
@@ -3314,8 +3314,8 @@ def _unconvert_index_legacy(data, kind, legacy=False, encoding=None):
33143314
def _convert_string_array(data, encoding, itemsize=None):
33153315

33163316
# encode if needed
3317-
if encoding is not None:
3318-
f = np.vectorize(lambda x: x.encode(encoding))
3317+
if encoding is not None and len(data):
3318+
f = np.vectorize(lambda x: x.encode(encoding), otypes=[np.object])
33193319
data = f(data)
33203320

33213321
# create the sized dtype
@@ -3333,7 +3333,7 @@ def _unconvert_string_array(data, nan_rep=None, encoding=None):
33333333
# guard against a None encoding in PY3 (because of a legacy
33343334
# where the passed encoding is actually None)
33353335
encoding = _ensure_encoding(encoding)
3336-
if encoding is not None:
3336+
if encoding is not None and len(data):
33373337
f = np.vectorize(lambda x: x.decode(encoding),otypes=[np.object])
33383338
data = f(data)
33393339

@@ -3378,7 +3378,6 @@ class Term(object):
33783378
value : a value or list of values (required)
33793379
queryables : a kinds map (dict of column name -> kind), or None i column is non-indexable
33803380
encoding : an encoding that will encode the query terms
3381-
i : my term id number
33823381
33833382
Returns
33843383
-------
@@ -3399,18 +3398,13 @@ class Term(object):
33993398
_search = re.compile("^\s*(?P<field>\w+)\s*(?P<op>%s)\s*(?P<value>.+)\s*$" % '|'.join(_ops))
34003399
_max_selectors = 31
34013400

3402-
def __init__(self, field, op=None, value=None, queryables=None, i=None, encoding=None):
3401+
def __init__(self, field, op=None, value=None, queryables=None, encoding=None):
34033402
self.field = None
34043403
self.op = None
34053404
self.value = None
34063405
self.q = queryables or dict()
34073406
self.filter = None
3408-
3409-
if i is None:
3410-
i = 0
3411-
self.i = i
34123407
self.condition = None
3413-
self.condvars = dict()
34143408
self.encoding = encoding
34153409

34163410
# unpack lists/tuples in field
@@ -3498,15 +3492,10 @@ def kind(self):
34983492
""" the kind of my field """
34993493
return self.q.get(self.field)
35003494

3501-
def generate(self, v, i=None):
3502-
""" create and return the op string for this TermValue
3503-
add the variable to condvars """
3504-
if i is None:
3505-
i = 0
3506-
3507-
cv = "_%s_%s_%s" % (self.field,self.i,i)
3508-
self.condvars[cv] = v.converted
3509-
return "(%s %s %s)" % (self.field, self.op, cv)
3495+
def generate(self, v):
3496+
""" create and return the op string for this TermValue """
3497+
val = v.tostring(self.encoding)
3498+
return "(%s %s %s)" % (self.field, self.op, val)
35103499

35113500
def eval(self):
35123501
""" set the numexpr expression for this term """
@@ -3534,7 +3523,7 @@ def eval(self):
35343523

35353524
# too many values to create the expression?
35363525
if len(values) <= self._max_selectors:
3537-
vs = [ self.generate(v, i) for i, v in enumerate(values) ]
3526+
vs = [ self.generate(v) for v in values ]
35383527
self.condition = "(%s)" % ' | '.join(vs)
35393528

35403529
# use a filter after reading
@@ -3600,6 +3589,15 @@ def __init__(self, value, converted, kind):
36003589
self.converted = converted
36013590
self.kind = kind
36023591

3592+
def tostring(self, encoding):
3593+
""" quote the string if not encoded
3594+
else encode and return """
3595+
if self.kind == u'string':
3596+
if encoding is not None:
3597+
return self.converted
3598+
return '"%s"' % self.converted
3599+
return self.converted
3600+
36033601
class Coordinates(object):
36043602
""" holds a returned coordinates list, useful to select the same rows from different tables
36053603
@@ -3637,7 +3635,6 @@ def __init__(self, table, where=None, start=None, stop=None, **kwargs):
36373635
self.start = start
36383636
self.stop = stop
36393637
self.condition = None
3640-
self.condvars = dict()
36413638
self.filter = None
36423639
self.terms = None
36433640
self.coordinates = None
@@ -3652,8 +3649,6 @@ def __init__(self, table, where=None, start=None, stop=None, **kwargs):
36523649
terms = [ t for t in self.terms if t.condition is not None ]
36533650
if len(terms):
36543651
self.condition = "(%s)" % ' & '.join([ t.condition for t in terms ])
3655-
for t in terms:
3656-
self.condvars.update(t.condvars)
36573652
self.filter = []
36583653
for t in self.terms:
36593654
if t.filter is not None:
@@ -3676,14 +3671,14 @@ def generate(self, where):
36763671
where = [where]
36773672

36783673
queryables = self.table.queryables()
3679-
return [Term(c, queryables=queryables, i=i, encoding=self.table.encoding) for i, c in enumerate(where)]
3674+
return [Term(c, queryables=queryables, encoding=self.table.encoding) for c in where]
36803675

36813676
def select(self):
36823677
"""
36833678
generate the selection
36843679
"""
36853680
if self.condition is not None:
3686-
return self.table.table.readWhere(self.condition, condvars=self.condvars, start=self.start, stop=self.stop)
3681+
return self.table.table.readWhere(self.condition, start=self.start, stop=self.stop)
36873682
elif self.coordinates is not None:
36883683
return self.table.table.readCoordinates(self.coordinates)
36893684
return self.table.table.read(start=self.start, stop=self.stop)
@@ -3695,7 +3690,7 @@ def select_coords(self):
36953690
if self.condition is None:
36963691
return np.arange(self.table.nrows)
36973692

3698-
return self.table.table.getWhereList(self.condition, condvars=self.condvars, start=self.start, stop=self.stop, sort=True)
3693+
return self.table.table.getWhereList(self.condition, start=self.start, stop=self.stop, sort=True)
36993694

37003695

37013696
### utilities ###

0 commit comments

Comments
 (0)