Skip to content

Commit 2f3672e

Browse files
committed
Merge pull request #5047 from jreback/pytables_cln
CLN: pytables cleanup added functiones (previously deleted/moved to comp...
2 parents 0e47ab6 + bd8daed commit 2f3672e

File tree

1 file changed

+0
-221
lines changed

1 file changed

+0
-221
lines changed

pandas/io/pytables.py

Lines changed: 0 additions & 221 deletions
Original file line numberDiff line numberDiff line change
@@ -3966,227 +3966,6 @@ def _need_convert(kind):
39663966
return True
39673967
return False
39683968

3969-
3970-
class Coordinates(object):
3971-
3972-
""" holds a returned coordinates list, useful to select the same rows from different tables
3973-
3974-
coordinates : holds the array of coordinates
3975-
group : the source group
3976-
where : the source where
3977-
"""
3978-
3979-
_ops = ['<=', '<', '>=', '>', '!=', '==', '=']
3980-
_search = re.compile(
3981-
"^\s*(?P<field>\w+)\s*(?P<op>%s)\s*(?P<value>.+)\s*$" % '|'.join(_ops))
3982-
_max_selectors = 31
3983-
3984-
def __init__(self, field, op=None, value=None, queryables=None, encoding=None):
3985-
self.field = None
3986-
self.op = None
3987-
self.value = None
3988-
self.q = queryables or dict()
3989-
self.filter = None
3990-
self.condition = None
3991-
self.encoding = encoding
3992-
3993-
# unpack lists/tuples in field
3994-
while(isinstance(field, (tuple, list))):
3995-
f = field
3996-
field = f[0]
3997-
if len(f) > 1:
3998-
op = f[1]
3999-
if len(f) > 2:
4000-
value = f[2]
4001-
4002-
# backwards compatible
4003-
if isinstance(field, dict):
4004-
self.field = field.get('field')
4005-
self.op = field.get('op') or '=='
4006-
self.value = field.get('value')
4007-
4008-
# passed a term
4009-
elif isinstance(field, Term):
4010-
self.field = field.field
4011-
self.op = field.op
4012-
self.value = field.value
4013-
4014-
# a string expression (or just the field)
4015-
elif isinstance(field, compat.string_types):
4016-
4017-
# is a term is passed
4018-
s = self._search.match(field)
4019-
if s is not None:
4020-
self.field = s.group('field')
4021-
self.op = s.group('op')
4022-
self.value = s.group('value')
4023-
4024-
else:
4025-
self.field = field
4026-
4027-
# is an op passed?
4028-
if isinstance(op, compat.string_types) and op in self._ops:
4029-
self.op = op
4030-
self.value = value
4031-
else:
4032-
self.op = '=='
4033-
self.value = op
4034-
4035-
else:
4036-
raise ValueError(
4037-
"Term does not understand the supplied field [%s]" % field)
4038-
4039-
# we have valid fields
4040-
if self.field is None or self.op is None or self.value is None:
4041-
raise ValueError("Could not create this term [%s]" % str(self))
4042-
4043-
# = vs ==
4044-
if self.op == '=':
4045-
self.op = '=='
4046-
4047-
# we have valid conditions
4048-
if self.op in ['>', '>=', '<', '<=']:
4049-
if hasattr(self.value, '__iter__') and len(self.value) > 1 and not isinstance(self.value, compat.string_types):
4050-
raise ValueError(
4051-
"an inequality condition cannot have multiple values [%s]" % str(self))
4052-
4053-
if not is_list_like(self.value):
4054-
self.value = [self.value]
4055-
4056-
if len(self.q):
4057-
self.eval()
4058-
4059-
def __unicode__(self):
4060-
attrs = lmap(pprint_thing, (self.field, self.op, self.value))
4061-
return "field->%s,op->%s,value->%s" % tuple(attrs)
4062-
4063-
@property
4064-
def is_valid(self):
4065-
""" return True if this is a valid field """
4066-
return self.field in self.q
4067-
4068-
@property
4069-
def is_in_table(self):
4070-
""" return True if this is a valid column name for generation (e.g. an actual column in the table) """
4071-
return self.q.get(self.field) is not None
4072-
4073-
@property
4074-
def kind(self):
4075-
""" the kind of my field """
4076-
return self.q.get(self.field)
4077-
4078-
def generate(self, v):
4079-
""" create and return the op string for this TermValue """
4080-
val = v.tostring(self.encoding)
4081-
return "(%s %s %s)" % (self.field, self.op, val)
4082-
4083-
def eval(self):
4084-
""" set the numexpr expression for this term """
4085-
4086-
if not self.is_valid:
4087-
raise ValueError("query term is not valid [{0}]\n"
4088-
" all queries terms must include a reference to\n"
4089-
" either an axis (e.g. index or column), or a data_columns\n".format(str(self)))
4090-
4091-
# convert values if we are in the table
4092-
if self.is_in_table:
4093-
values = [self.convert_value(v) for v in self.value]
4094-
else:
4095-
values = [TermValue(v, v, self.kind) for v in self.value]
4096-
4097-
# equality conditions
4098-
if self.op in ['==', '!=']:
4099-
4100-
# our filter op expression
4101-
if self.op == '!=':
4102-
filter_op = lambda axis, vals: not axis.isin(vals)
4103-
else:
4104-
filter_op = lambda axis, vals: axis.isin(vals)
4105-
4106-
if self.is_in_table:
4107-
4108-
# too many values to create the expression?
4109-
if len(values) <= self._max_selectors:
4110-
vs = [self.generate(v) for v in values]
4111-
self.condition = "(%s)" % ' | '.join(vs)
4112-
4113-
# use a filter after reading
4114-
else:
4115-
self.filter = (
4116-
self.field, filter_op, Index([v.value for v in values]))
4117-
4118-
else:
4119-
4120-
self.filter = (
4121-
self.field, filter_op, Index([v.value for v in values]))
4122-
4123-
else:
4124-
4125-
if self.is_in_table:
4126-
4127-
self.condition = self.generate(values[0])
4128-
4129-
else:
4130-
4131-
raise TypeError(
4132-
"passing a filterable condition to a Fixed format indexer [%s]" % str(self))
4133-
4134-
def convert_value(self, v):
4135-
""" convert the expression that is in the term to something that is accepted by pytables """
4136-
4137-
def stringify(value):
4138-
value = str(value)
4139-
if self.encoding is not None:
4140-
value = value.encode(self.encoding)
4141-
return value
4142-
4143-
kind = _ensure_decoded(self.kind)
4144-
if kind == u('datetime64') or kind == u('datetime'):
4145-
v = lib.Timestamp(v)
4146-
if v.tz is not None:
4147-
v = v.tz_convert('UTC')
4148-
return TermValue(v, v.value, kind)
4149-
elif kind == u('timedelta64') or kind == u('timedelta'):
4150-
v = _coerce_scalar_to_timedelta_type(v,unit='s').item()
4151-
return TermValue(int(v), v, kind)
4152-
elif (isinstance(v, datetime) or hasattr(v, 'timetuple')):
4153-
v = time.mktime(v.timetuple())
4154-
return TermValue(v, Timestamp(v), kind)
4155-
elif kind == u('date'):
4156-
v = v.toordinal()
4157-
return TermValue(v, Timestamp.fromordinal(v), kind)
4158-
elif kind == u('integer'):
4159-
v = int(float(v))
4160-
return TermValue(v, v, kind)
4161-
elif kind == u('float'):
4162-
v = float(v)
4163-
return TermValue(v, v, kind)
4164-
elif kind == u('bool'):
4165-
if isinstance(v, compat.string_types):
4166-
poss_vals = [u('false'), u('f'), u('no'),
4167-
u('n'), u('none'), u('0'),
4168-
u('[]'), u('{}'), u('')]
4169-
v = not v.strip().lower() in poss_vals
4170-
else:
4171-
v = bool(v)
4172-
return TermValue(v, v, kind)
4173-
elif not isinstance(v, compat.string_types):
4174-
v = stringify(v)
4175-
return TermValue(v, stringify(v), u('string'))
4176-
4177-
# string quoting
4178-
return TermValue(v, stringify(v), u('string'))
4179-
4180-
4181-
4182-
def __len__(self):
4183-
return len(self.values)
4184-
4185-
def __getitem__(self, key):
4186-
""" return a new coordinates object, sliced by the key """
4187-
return Coordinates(self.values[key], self.group, self.where)
4188-
4189-
41903969
class Selection(object):
41913970

41923971
"""

0 commit comments

Comments
 (0)