Skip to content

Commit

Permalink
add h2o.trim() to python client and respective pyunit
Browse files Browse the repository at this point in the history
  • Loading branch information
ericeckstrand committed Jun 22, 2015
1 parent 1944d80 commit 5496d10
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
21 changes: 21 additions & 0 deletions h2o-py/h2o/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,18 @@ def transpose(self):
expr = "(= !{} (t %{}))".format(tmp_key,frame_keys[0])
return H2OFrame._get_frame_from_rapids_string(expr, tmp_key, frame_keys)

def trim(self):
"""
Trim the edge-spaces in a column of strings (only operates on frame with one column)
:return: H2OFrame
"""
if self._vecs is None or self._vecs == []:
raise ValueError("Frame Removed")
frame_keys = [self.send_frame()]
tmp_key = H2OFrame.py_tmp_key()
expr = "(= !{} (trim %{}))".format(tmp_key,frame_keys[0])
return H2OFrame._get_frame_from_rapids_string(expr, tmp_key, frame_keys)

def table(self, data2=None):
"""
:return: a frame of the counts at each combination of factor levels
Expand Down Expand Up @@ -1744,6 +1756,15 @@ def transpose(self):
expr = "(= !{} (t %{}))".format(tmp_key,self.key())
return H2OFrame._get_frame_from_rapids_string(expr, tmp_key, [])

def trim(self):
"""
Trim the edge-spaces in a column of strings
:return: H2OVec
"""
tmp_key = H2OFrame.py_tmp_key()
expr = "(= !{} (trim %{}))".format(tmp_key,self.key())
return H2OVec._get_vec_from_rapids_string(self, expr, tmp_key)

def table(self, data2=None):
"""
:return: a frame of the counts at each combination of factor levels
Expand Down
1 change: 1 addition & 0 deletions h2o-py/h2o/h2o.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,7 @@ def var(data) : return data.var()
def mean(data) : return data.mean()
def median(data): return data.median()

def trim(data) : return data.trim()
def asnumeric(data) : return data.asnumeric()
def transpose(data) : return data.transpose()
def anyfactor(data) : return data.anyfactor()
Expand Down
31 changes: 31 additions & 0 deletions h2o-py/tests/testdir_munging/pyunit_trim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import sys
sys.path.insert(1, "../../")
import h2o

def trim_check(ip,port):
# Connect to a pre-existing cluster
h2o.init(ip,port)

frame = h2o.import_frame(path=h2o.locate("smalldata/junit/cars_trim.csv"))

# single column (frame)
trimmed_frame = h2o.trim(frame[["name"]])
assert trimmed_frame[0,0] == "AMC Ambassador Brougham", "Expected 'AMC Ambassador Brougham', but got " \
"{0}".format(trimmed_frame[0,0])
assert trimmed_frame[1,0] == "AMC Ambassador DPL", "Expected 'AMC Ambassador DPL', but got " \
"{0}".format(trimmed_frame[1,0])
assert trimmed_frame[2,0] == "AMC Ambassador SST", "Expected 'AMC Ambassador SST', but got " \
"{0}".format(trimmed_frame[2,0])

# single column (vec)
vec = frame["name"]
trimmed_vec = h2o.trim(vec)
assert trimmed_vec[0] == "AMC Ambassador Brougham", "Expected 'AMC Ambassador Brougham', but got " \
"{0}".format(trimmed_frame[0])
assert trimmed_vec[1] == "AMC Ambassador DPL", "Expected 'AMC Ambassador DPL', but got " \
"{0}".format(trimmed_frame[1])
assert trimmed_vec[2] == "AMC Ambassador SST", "Expected 'AMC Ambassador SST', but got " \
"{0}".format(trimmed_frame[2])

if __name__ == "__main__":
h2o.run_test(sys.argv, trim_check)

0 comments on commit 5496d10

Please sign in to comment.