repository_name
stringlengths
5
67
func_path_in_repository
stringlengths
4
234
func_name
stringlengths
0
153
whole_func_string
stringlengths
52
3.87M
language
stringclasses
6 values
func_code_string
stringlengths
52
3.87M
func_code_tokens
sequence
func_documentation_string
stringlengths
1
46.9k
func_documentation_tokens
sequence
split_name
stringclasses
1 value
func_code_url
stringlengths
85
339
apache/spark
python/pyspark/mllib/classification.py
SVMModel.predict
def predict(self, x): """ Predict values for a single data point or an RDD of points using the model trained. """ if isinstance(x, RDD): return x.map(lambda v: self.predict(v)) x = _convert_to_vector(x) margin = self.weights.dot(x) + self.intercept if self._threshold is None: return margin else: return 1 if margin > self._threshold else 0
python
def predict(self, x): """ Predict values for a single data point or an RDD of points using the model trained. """ if isinstance(x, RDD): return x.map(lambda v: self.predict(v)) x = _convert_to_vector(x) margin = self.weights.dot(x) + self.intercept if self._threshold is None: return margin else: return 1 if margin > self._threshold else 0
[ "def", "predict", "(", "self", ",", "x", ")", ":", "if", "isinstance", "(", "x", ",", "RDD", ")", ":", "return", "x", ".", "map", "(", "lambda", "v", ":", "self", ".", "predict", "(", "v", ")", ")", "x", "=", "_convert_to_vector", "(", "x", ")", "margin", "=", "self", ".", "weights", ".", "dot", "(", "x", ")", "+", "self", ".", "intercept", "if", "self", ".", "_threshold", "is", "None", ":", "return", "margin", "else", ":", "return", "1", "if", "margin", ">", "self", ".", "_threshold", "else", "0" ]
Predict values for a single data point or an RDD of points using the model trained.
[ "Predict", "values", "for", "a", "single", "data", "point", "or", "an", "RDD", "of", "points", "using", "the", "model", "trained", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/mllib/classification.py#L460-L473
apache/spark
python/pyspark/mllib/classification.py
SVMModel.save
def save(self, sc, path): """ Save this model to the given path. """ java_model = sc._jvm.org.apache.spark.mllib.classification.SVMModel( _py2java(sc, self._coeff), self.intercept) java_model.save(sc._jsc.sc(), path)
python
def save(self, sc, path): """ Save this model to the given path. """ java_model = sc._jvm.org.apache.spark.mllib.classification.SVMModel( _py2java(sc, self._coeff), self.intercept) java_model.save(sc._jsc.sc(), path)
[ "def", "save", "(", "self", ",", "sc", ",", "path", ")", ":", "java_model", "=", "sc", ".", "_jvm", ".", "org", ".", "apache", ".", "spark", ".", "mllib", ".", "classification", ".", "SVMModel", "(", "_py2java", "(", "sc", ",", "self", ".", "_coeff", ")", ",", "self", ".", "intercept", ")", "java_model", ".", "save", "(", "sc", ".", "_jsc", ".", "sc", "(", ")", ",", "path", ")" ]
Save this model to the given path.
[ "Save", "this", "model", "to", "the", "given", "path", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/mllib/classification.py#L476-L482
apache/spark
python/pyspark/mllib/classification.py
SVMModel.load
def load(cls, sc, path): """ Load a model from the given path. """ java_model = sc._jvm.org.apache.spark.mllib.classification.SVMModel.load( sc._jsc.sc(), path) weights = _java2py(sc, java_model.weights()) intercept = java_model.intercept() threshold = java_model.getThreshold().get() model = SVMModel(weights, intercept) model.setThreshold(threshold) return model
python
def load(cls, sc, path): """ Load a model from the given path. """ java_model = sc._jvm.org.apache.spark.mllib.classification.SVMModel.load( sc._jsc.sc(), path) weights = _java2py(sc, java_model.weights()) intercept = java_model.intercept() threshold = java_model.getThreshold().get() model = SVMModel(weights, intercept) model.setThreshold(threshold) return model
[ "def", "load", "(", "cls", ",", "sc", ",", "path", ")", ":", "java_model", "=", "sc", ".", "_jvm", ".", "org", ".", "apache", ".", "spark", ".", "mllib", ".", "classification", ".", "SVMModel", ".", "load", "(", "sc", ".", "_jsc", ".", "sc", "(", ")", ",", "path", ")", "weights", "=", "_java2py", "(", "sc", ",", "java_model", ".", "weights", "(", ")", ")", "intercept", "=", "java_model", ".", "intercept", "(", ")", "threshold", "=", "java_model", ".", "getThreshold", "(", ")", ".", "get", "(", ")", "model", "=", "SVMModel", "(", "weights", ",", "intercept", ")", "model", ".", "setThreshold", "(", "threshold", ")", "return", "model" ]
Load a model from the given path.
[ "Load", "a", "model", "from", "the", "given", "path", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/mllib/classification.py#L486-L497
apache/spark
python/pyspark/mllib/classification.py
NaiveBayes.train
def train(cls, data, lambda_=1.0): """ Train a Naive Bayes model given an RDD of (label, features) vectors. This is the Multinomial NB (U{http://tinyurl.com/lsdw6p}) which can handle all kinds of discrete data. For example, by converting documents into TF-IDF vectors, it can be used for document classification. By making every vector a 0-1 vector, it can also be used as Bernoulli NB (U{http://tinyurl.com/p7c96j6}). The input feature values must be nonnegative. :param data: RDD of LabeledPoint. :param lambda_: The smoothing parameter. (default: 1.0) """ first = data.first() if not isinstance(first, LabeledPoint): raise ValueError("`data` should be an RDD of LabeledPoint") labels, pi, theta = callMLlibFunc("trainNaiveBayesModel", data, lambda_) return NaiveBayesModel(labels.toArray(), pi.toArray(), numpy.array(theta))
python
def train(cls, data, lambda_=1.0): """ Train a Naive Bayes model given an RDD of (label, features) vectors. This is the Multinomial NB (U{http://tinyurl.com/lsdw6p}) which can handle all kinds of discrete data. For example, by converting documents into TF-IDF vectors, it can be used for document classification. By making every vector a 0-1 vector, it can also be used as Bernoulli NB (U{http://tinyurl.com/p7c96j6}). The input feature values must be nonnegative. :param data: RDD of LabeledPoint. :param lambda_: The smoothing parameter. (default: 1.0) """ first = data.first() if not isinstance(first, LabeledPoint): raise ValueError("`data` should be an RDD of LabeledPoint") labels, pi, theta = callMLlibFunc("trainNaiveBayesModel", data, lambda_) return NaiveBayesModel(labels.toArray(), pi.toArray(), numpy.array(theta))
[ "def", "train", "(", "cls", ",", "data", ",", "lambda_", "=", "1.0", ")", ":", "first", "=", "data", ".", "first", "(", ")", "if", "not", "isinstance", "(", "first", ",", "LabeledPoint", ")", ":", "raise", "ValueError", "(", "\"`data` should be an RDD of LabeledPoint\"", ")", "labels", ",", "pi", ",", "theta", "=", "callMLlibFunc", "(", "\"trainNaiveBayesModel\"", ",", "data", ",", "lambda_", ")", "return", "NaiveBayesModel", "(", "labels", ".", "toArray", "(", ")", ",", "pi", ".", "toArray", "(", ")", ",", "numpy", ".", "array", "(", "theta", ")", ")" ]
Train a Naive Bayes model given an RDD of (label, features) vectors. This is the Multinomial NB (U{http://tinyurl.com/lsdw6p}) which can handle all kinds of discrete data. For example, by converting documents into TF-IDF vectors, it can be used for document classification. By making every vector a 0-1 vector, it can also be used as Bernoulli NB (U{http://tinyurl.com/p7c96j6}). The input feature values must be nonnegative. :param data: RDD of LabeledPoint. :param lambda_: The smoothing parameter. (default: 1.0)
[ "Train", "a", "Naive", "Bayes", "model", "given", "an", "RDD", "of", "(", "label", "features", ")", "vectors", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/mllib/classification.py#L657-L679
apache/spark
python/pyspark/heapq3.py
heappush
def heappush(heap, item): """Push item onto heap, maintaining the heap invariant.""" heap.append(item) _siftdown(heap, 0, len(heap)-1)
python
def heappush(heap, item): """Push item onto heap, maintaining the heap invariant.""" heap.append(item) _siftdown(heap, 0, len(heap)-1)
[ "def", "heappush", "(", "heap", ",", "item", ")", ":", "heap", ".", "append", "(", "item", ")", "_siftdown", "(", "heap", ",", "0", ",", "len", "(", "heap", ")", "-", "1", ")" ]
Push item onto heap, maintaining the heap invariant.
[ "Push", "item", "onto", "heap", "maintaining", "the", "heap", "invariant", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/heapq3.py#L411-L414
apache/spark
python/pyspark/heapq3.py
heappop
def heappop(heap): """Pop the smallest item off the heap, maintaining the heap invariant.""" lastelt = heap.pop() # raises appropriate IndexError if heap is empty if heap: returnitem = heap[0] heap[0] = lastelt _siftup(heap, 0) return returnitem return lastelt
python
def heappop(heap): """Pop the smallest item off the heap, maintaining the heap invariant.""" lastelt = heap.pop() # raises appropriate IndexError if heap is empty if heap: returnitem = heap[0] heap[0] = lastelt _siftup(heap, 0) return returnitem return lastelt
[ "def", "heappop", "(", "heap", ")", ":", "lastelt", "=", "heap", ".", "pop", "(", ")", "# raises appropriate IndexError if heap is empty", "if", "heap", ":", "returnitem", "=", "heap", "[", "0", "]", "heap", "[", "0", "]", "=", "lastelt", "_siftup", "(", "heap", ",", "0", ")", "return", "returnitem", "return", "lastelt" ]
Pop the smallest item off the heap, maintaining the heap invariant.
[ "Pop", "the", "smallest", "item", "off", "the", "heap", "maintaining", "the", "heap", "invariant", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/heapq3.py#L416-L424
apache/spark
python/pyspark/heapq3.py
heapreplace
def heapreplace(heap, item): """Pop and return the current smallest value, and add the new item. This is more efficient than heappop() followed by heappush(), and can be more appropriate when using a fixed-size heap. Note that the value returned may be larger than item! That constrains reasonable uses of this routine unless written as part of a conditional replacement: if item > heap[0]: item = heapreplace(heap, item) """ returnitem = heap[0] # raises appropriate IndexError if heap is empty heap[0] = item _siftup(heap, 0) return returnitem
python
def heapreplace(heap, item): """Pop and return the current smallest value, and add the new item. This is more efficient than heappop() followed by heappush(), and can be more appropriate when using a fixed-size heap. Note that the value returned may be larger than item! That constrains reasonable uses of this routine unless written as part of a conditional replacement: if item > heap[0]: item = heapreplace(heap, item) """ returnitem = heap[0] # raises appropriate IndexError if heap is empty heap[0] = item _siftup(heap, 0) return returnitem
[ "def", "heapreplace", "(", "heap", ",", "item", ")", ":", "returnitem", "=", "heap", "[", "0", "]", "# raises appropriate IndexError if heap is empty", "heap", "[", "0", "]", "=", "item", "_siftup", "(", "heap", ",", "0", ")", "return", "returnitem" ]
Pop and return the current smallest value, and add the new item. This is more efficient than heappop() followed by heappush(), and can be more appropriate when using a fixed-size heap. Note that the value returned may be larger than item! That constrains reasonable uses of this routine unless written as part of a conditional replacement: if item > heap[0]: item = heapreplace(heap, item)
[ "Pop", "and", "return", "the", "current", "smallest", "value", "and", "add", "the", "new", "item", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/heapq3.py#L426-L440
apache/spark
python/pyspark/heapq3.py
heappushpop
def heappushpop(heap, item): """Fast version of a heappush followed by a heappop.""" if heap and heap[0] < item: item, heap[0] = heap[0], item _siftup(heap, 0) return item
python
def heappushpop(heap, item): """Fast version of a heappush followed by a heappop.""" if heap and heap[0] < item: item, heap[0] = heap[0], item _siftup(heap, 0) return item
[ "def", "heappushpop", "(", "heap", ",", "item", ")", ":", "if", "heap", "and", "heap", "[", "0", "]", "<", "item", ":", "item", ",", "heap", "[", "0", "]", "=", "heap", "[", "0", "]", ",", "item", "_siftup", "(", "heap", ",", "0", ")", "return", "item" ]
Fast version of a heappush followed by a heappop.
[ "Fast", "version", "of", "a", "heappush", "followed", "by", "a", "heappop", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/heapq3.py#L442-L447
apache/spark
python/pyspark/heapq3.py
heapify
def heapify(x): """Transform list into a heap, in-place, in O(len(x)) time.""" n = len(x) # Transform bottom-up. The largest index there's any point to looking at # is the largest with a child index in-range, so must have 2*i + 1 < n, # or i < (n-1)/2. If n is even = 2*j, this is (2*j-1)/2 = j-1/2 so # j-1 is the largest, which is n//2 - 1. If n is odd = 2*j+1, this is # (2*j+1-1)/2 = j so j-1 is the largest, and that's again n//2-1. for i in reversed(range(n//2)): _siftup(x, i)
python
def heapify(x): """Transform list into a heap, in-place, in O(len(x)) time.""" n = len(x) # Transform bottom-up. The largest index there's any point to looking at # is the largest with a child index in-range, so must have 2*i + 1 < n, # or i < (n-1)/2. If n is even = 2*j, this is (2*j-1)/2 = j-1/2 so # j-1 is the largest, which is n//2 - 1. If n is odd = 2*j+1, this is # (2*j+1-1)/2 = j so j-1 is the largest, and that's again n//2-1. for i in reversed(range(n//2)): _siftup(x, i)
[ "def", "heapify", "(", "x", ")", ":", "n", "=", "len", "(", "x", ")", "# Transform bottom-up. The largest index there's any point to looking at", "# is the largest with a child index in-range, so must have 2*i + 1 < n,", "# or i < (n-1)/2. If n is even = 2*j, this is (2*j-1)/2 = j-1/2 so", "# j-1 is the largest, which is n//2 - 1. If n is odd = 2*j+1, this is", "# (2*j+1-1)/2 = j so j-1 is the largest, and that's again n//2-1.", "for", "i", "in", "reversed", "(", "range", "(", "n", "//", "2", ")", ")", ":", "_siftup", "(", "x", ",", "i", ")" ]
Transform list into a heap, in-place, in O(len(x)) time.
[ "Transform", "list", "into", "a", "heap", "in", "-", "place", "in", "O", "(", "len", "(", "x", "))", "time", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/heapq3.py#L449-L458
apache/spark
python/pyspark/heapq3.py
_heappop_max
def _heappop_max(heap): """Maxheap version of a heappop.""" lastelt = heap.pop() # raises appropriate IndexError if heap is empty if heap: returnitem = heap[0] heap[0] = lastelt _siftup_max(heap, 0) return returnitem return lastelt
python
def _heappop_max(heap): """Maxheap version of a heappop.""" lastelt = heap.pop() # raises appropriate IndexError if heap is empty if heap: returnitem = heap[0] heap[0] = lastelt _siftup_max(heap, 0) return returnitem return lastelt
[ "def", "_heappop_max", "(", "heap", ")", ":", "lastelt", "=", "heap", ".", "pop", "(", ")", "# raises appropriate IndexError if heap is empty", "if", "heap", ":", "returnitem", "=", "heap", "[", "0", "]", "heap", "[", "0", "]", "=", "lastelt", "_siftup_max", "(", "heap", ",", "0", ")", "return", "returnitem", "return", "lastelt" ]
Maxheap version of a heappop.
[ "Maxheap", "version", "of", "a", "heappop", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/heapq3.py#L460-L468
apache/spark
python/pyspark/heapq3.py
_heapreplace_max
def _heapreplace_max(heap, item): """Maxheap version of a heappop followed by a heappush.""" returnitem = heap[0] # raises appropriate IndexError if heap is empty heap[0] = item _siftup_max(heap, 0) return returnitem
python
def _heapreplace_max(heap, item): """Maxheap version of a heappop followed by a heappush.""" returnitem = heap[0] # raises appropriate IndexError if heap is empty heap[0] = item _siftup_max(heap, 0) return returnitem
[ "def", "_heapreplace_max", "(", "heap", ",", "item", ")", ":", "returnitem", "=", "heap", "[", "0", "]", "# raises appropriate IndexError if heap is empty", "heap", "[", "0", "]", "=", "item", "_siftup_max", "(", "heap", ",", "0", ")", "return", "returnitem" ]
Maxheap version of a heappop followed by a heappush.
[ "Maxheap", "version", "of", "a", "heappop", "followed", "by", "a", "heappush", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/heapq3.py#L470-L475
apache/spark
python/pyspark/heapq3.py
_heapify_max
def _heapify_max(x): """Transform list into a maxheap, in-place, in O(len(x)) time.""" n = len(x) for i in reversed(range(n//2)): _siftup_max(x, i)
python
def _heapify_max(x): """Transform list into a maxheap, in-place, in O(len(x)) time.""" n = len(x) for i in reversed(range(n//2)): _siftup_max(x, i)
[ "def", "_heapify_max", "(", "x", ")", ":", "n", "=", "len", "(", "x", ")", "for", "i", "in", "reversed", "(", "range", "(", "n", "//", "2", ")", ")", ":", "_siftup_max", "(", "x", ",", "i", ")" ]
Transform list into a maxheap, in-place, in O(len(x)) time.
[ "Transform", "list", "into", "a", "maxheap", "in", "-", "place", "in", "O", "(", "len", "(", "x", "))", "time", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/heapq3.py#L477-L481
apache/spark
python/pyspark/heapq3.py
_siftdown_max
def _siftdown_max(heap, startpos, pos): 'Maxheap variant of _siftdown' newitem = heap[pos] # Follow the path to the root, moving parents down until finding a place # newitem fits. while pos > startpos: parentpos = (pos - 1) >> 1 parent = heap[parentpos] if parent < newitem: heap[pos] = parent pos = parentpos continue break heap[pos] = newitem
python
def _siftdown_max(heap, startpos, pos): 'Maxheap variant of _siftdown' newitem = heap[pos] # Follow the path to the root, moving parents down until finding a place # newitem fits. while pos > startpos: parentpos = (pos - 1) >> 1 parent = heap[parentpos] if parent < newitem: heap[pos] = parent pos = parentpos continue break heap[pos] = newitem
[ "def", "_siftdown_max", "(", "heap", ",", "startpos", ",", "pos", ")", ":", "newitem", "=", "heap", "[", "pos", "]", "# Follow the path to the root, moving parents down until finding a place", "# newitem fits.", "while", "pos", ">", "startpos", ":", "parentpos", "=", "(", "pos", "-", "1", ")", ">>", "1", "parent", "=", "heap", "[", "parentpos", "]", "if", "parent", "<", "newitem", ":", "heap", "[", "pos", "]", "=", "parent", "pos", "=", "parentpos", "continue", "break", "heap", "[", "pos", "]", "=", "newitem" ]
Maxheap variant of _siftdown
[ "Maxheap", "variant", "of", "_siftdown" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/heapq3.py#L559-L572
apache/spark
python/pyspark/heapq3.py
_siftup_max
def _siftup_max(heap, pos): 'Maxheap variant of _siftup' endpos = len(heap) startpos = pos newitem = heap[pos] # Bubble up the larger child until hitting a leaf. childpos = 2*pos + 1 # leftmost child position while childpos < endpos: # Set childpos to index of larger child. rightpos = childpos + 1 if rightpos < endpos and not heap[rightpos] < heap[childpos]: childpos = rightpos # Move the larger child up. heap[pos] = heap[childpos] pos = childpos childpos = 2*pos + 1 # The leaf at pos is empty now. Put newitem there, and bubble it up # to its final resting place (by sifting its parents down). heap[pos] = newitem _siftdown_max(heap, startpos, pos)
python
def _siftup_max(heap, pos): 'Maxheap variant of _siftup' endpos = len(heap) startpos = pos newitem = heap[pos] # Bubble up the larger child until hitting a leaf. childpos = 2*pos + 1 # leftmost child position while childpos < endpos: # Set childpos to index of larger child. rightpos = childpos + 1 if rightpos < endpos and not heap[rightpos] < heap[childpos]: childpos = rightpos # Move the larger child up. heap[pos] = heap[childpos] pos = childpos childpos = 2*pos + 1 # The leaf at pos is empty now. Put newitem there, and bubble it up # to its final resting place (by sifting its parents down). heap[pos] = newitem _siftdown_max(heap, startpos, pos)
[ "def", "_siftup_max", "(", "heap", ",", "pos", ")", ":", "endpos", "=", "len", "(", "heap", ")", "startpos", "=", "pos", "newitem", "=", "heap", "[", "pos", "]", "# Bubble up the larger child until hitting a leaf.", "childpos", "=", "2", "*", "pos", "+", "1", "# leftmost child position", "while", "childpos", "<", "endpos", ":", "# Set childpos to index of larger child.", "rightpos", "=", "childpos", "+", "1", "if", "rightpos", "<", "endpos", "and", "not", "heap", "[", "rightpos", "]", "<", "heap", "[", "childpos", "]", ":", "childpos", "=", "rightpos", "# Move the larger child up.", "heap", "[", "pos", "]", "=", "heap", "[", "childpos", "]", "pos", "=", "childpos", "childpos", "=", "2", "*", "pos", "+", "1", "# The leaf at pos is empty now. Put newitem there, and bubble it up", "# to its final resting place (by sifting its parents down).", "heap", "[", "pos", "]", "=", "newitem", "_siftdown_max", "(", "heap", ",", "startpos", ",", "pos", ")" ]
Maxheap variant of _siftup
[ "Maxheap", "variant", "of", "_siftup" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/heapq3.py#L574-L593
apache/spark
python/pyspark/heapq3.py
merge
def merge(iterables, key=None, reverse=False): '''Merge multiple sorted inputs into a single sorted output. Similar to sorted(itertools.chain(*iterables)) but returns a generator, does not pull the data into memory all at once, and assumes that each of the input streams is already sorted (smallest to largest). >>> list(merge([1,3,5,7], [0,2,4,8], [5,10,15,20], [], [25])) [0, 1, 2, 3, 4, 5, 5, 7, 8, 10, 15, 20, 25] If *key* is not None, applies a key function to each element to determine its sort order. >>> list(merge(['dog', 'horse'], ['cat', 'fish', 'kangaroo'], key=len)) ['dog', 'cat', 'fish', 'horse', 'kangaroo'] ''' h = [] h_append = h.append if reverse: _heapify = _heapify_max _heappop = _heappop_max _heapreplace = _heapreplace_max direction = -1 else: _heapify = heapify _heappop = heappop _heapreplace = heapreplace direction = 1 if key is None: for order, it in enumerate(map(iter, iterables)): try: h_append([next(it), order * direction, it]) except StopIteration: pass _heapify(h) while len(h) > 1: try: while True: value, order, it = s = h[0] yield value s[0] = next(it) # raises StopIteration when exhausted _heapreplace(h, s) # restore heap condition except StopIteration: _heappop(h) # remove empty iterator if h: # fast case when only a single iterator remains value, order, it = h[0] yield value for value in it: yield value return for order, it in enumerate(map(iter, iterables)): try: value = next(it) h_append([key(value), order * direction, value, it]) except StopIteration: pass _heapify(h) while len(h) > 1: try: while True: key_value, order, value, it = s = h[0] yield value value = next(it) s[0] = key(value) s[2] = value _heapreplace(h, s) except StopIteration: _heappop(h) if h: key_value, order, value, it = h[0] yield value for value in it: yield value
python
def merge(iterables, key=None, reverse=False): '''Merge multiple sorted inputs into a single sorted output. Similar to sorted(itertools.chain(*iterables)) but returns a generator, does not pull the data into memory all at once, and assumes that each of the input streams is already sorted (smallest to largest). >>> list(merge([1,3,5,7], [0,2,4,8], [5,10,15,20], [], [25])) [0, 1, 2, 3, 4, 5, 5, 7, 8, 10, 15, 20, 25] If *key* is not None, applies a key function to each element to determine its sort order. >>> list(merge(['dog', 'horse'], ['cat', 'fish', 'kangaroo'], key=len)) ['dog', 'cat', 'fish', 'horse', 'kangaroo'] ''' h = [] h_append = h.append if reverse: _heapify = _heapify_max _heappop = _heappop_max _heapreplace = _heapreplace_max direction = -1 else: _heapify = heapify _heappop = heappop _heapreplace = heapreplace direction = 1 if key is None: for order, it in enumerate(map(iter, iterables)): try: h_append([next(it), order * direction, it]) except StopIteration: pass _heapify(h) while len(h) > 1: try: while True: value, order, it = s = h[0] yield value s[0] = next(it) # raises StopIteration when exhausted _heapreplace(h, s) # restore heap condition except StopIteration: _heappop(h) # remove empty iterator if h: # fast case when only a single iterator remains value, order, it = h[0] yield value for value in it: yield value return for order, it in enumerate(map(iter, iterables)): try: value = next(it) h_append([key(value), order * direction, value, it]) except StopIteration: pass _heapify(h) while len(h) > 1: try: while True: key_value, order, value, it = s = h[0] yield value value = next(it) s[0] = key(value) s[2] = value _heapreplace(h, s) except StopIteration: _heappop(h) if h: key_value, order, value, it = h[0] yield value for value in it: yield value
[ "def", "merge", "(", "iterables", ",", "key", "=", "None", ",", "reverse", "=", "False", ")", ":", "h", "=", "[", "]", "h_append", "=", "h", ".", "append", "if", "reverse", ":", "_heapify", "=", "_heapify_max", "_heappop", "=", "_heappop_max", "_heapreplace", "=", "_heapreplace_max", "direction", "=", "-", "1", "else", ":", "_heapify", "=", "heapify", "_heappop", "=", "heappop", "_heapreplace", "=", "heapreplace", "direction", "=", "1", "if", "key", "is", "None", ":", "for", "order", ",", "it", "in", "enumerate", "(", "map", "(", "iter", ",", "iterables", ")", ")", ":", "try", ":", "h_append", "(", "[", "next", "(", "it", ")", ",", "order", "*", "direction", ",", "it", "]", ")", "except", "StopIteration", ":", "pass", "_heapify", "(", "h", ")", "while", "len", "(", "h", ")", ">", "1", ":", "try", ":", "while", "True", ":", "value", ",", "order", ",", "it", "=", "s", "=", "h", "[", "0", "]", "yield", "value", "s", "[", "0", "]", "=", "next", "(", "it", ")", "# raises StopIteration when exhausted", "_heapreplace", "(", "h", ",", "s", ")", "# restore heap condition", "except", "StopIteration", ":", "_heappop", "(", "h", ")", "# remove empty iterator", "if", "h", ":", "# fast case when only a single iterator remains", "value", ",", "order", ",", "it", "=", "h", "[", "0", "]", "yield", "value", "for", "value", "in", "it", ":", "yield", "value", "return", "for", "order", ",", "it", "in", "enumerate", "(", "map", "(", "iter", ",", "iterables", ")", ")", ":", "try", ":", "value", "=", "next", "(", "it", ")", "h_append", "(", "[", "key", "(", "value", ")", ",", "order", "*", "direction", ",", "value", ",", "it", "]", ")", "except", "StopIteration", ":", "pass", "_heapify", "(", "h", ")", "while", "len", "(", "h", ")", ">", "1", ":", "try", ":", "while", "True", ":", "key_value", ",", "order", ",", "value", ",", "it", "=", "s", "=", "h", "[", "0", "]", "yield", "value", "value", "=", "next", "(", "it", ")", "s", "[", "0", "]", "=", "key", "(", "value", ")", "s", "[", "2", "]", "=", "value", "_heapreplace", "(", "h", ",", "s", ")", "except", "StopIteration", ":", "_heappop", "(", "h", ")", "if", "h", ":", "key_value", ",", "order", ",", "value", ",", "it", "=", "h", "[", "0", "]", "yield", "value", "for", "value", "in", "it", ":", "yield", "value" ]
Merge multiple sorted inputs into a single sorted output. Similar to sorted(itertools.chain(*iterables)) but returns a generator, does not pull the data into memory all at once, and assumes that each of the input streams is already sorted (smallest to largest). >>> list(merge([1,3,5,7], [0,2,4,8], [5,10,15,20], [], [25])) [0, 1, 2, 3, 4, 5, 5, 7, 8, 10, 15, 20, 25] If *key* is not None, applies a key function to each element to determine its sort order. >>> list(merge(['dog', 'horse'], ['cat', 'fish', 'kangaroo'], key=len)) ['dog', 'cat', 'fish', 'horse', 'kangaroo']
[ "Merge", "multiple", "sorted", "inputs", "into", "a", "single", "sorted", "output", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/heapq3.py#L595-L673
apache/spark
python/pyspark/heapq3.py
nsmallest
def nsmallest(n, iterable, key=None): """Find the n smallest elements in a dataset. Equivalent to: sorted(iterable, key=key)[:n] """ # Short-cut for n==1 is to use min() if n == 1: it = iter(iterable) sentinel = object() if key is None: result = min(it, default=sentinel) else: result = min(it, default=sentinel, key=key) return [] if result is sentinel else [result] # When n>=size, it's faster to use sorted() try: size = len(iterable) except (TypeError, AttributeError): pass else: if n >= size: return sorted(iterable, key=key)[:n] # When key is none, use simpler decoration if key is None: it = iter(iterable) # put the range(n) first so that zip() doesn't # consume one too many elements from the iterator result = [(elem, i) for i, elem in zip(range(n), it)] if not result: return result _heapify_max(result) top = result[0][0] order = n _heapreplace = _heapreplace_max for elem in it: if elem < top: _heapreplace(result, (elem, order)) top = result[0][0] order += 1 result.sort() return [r[0] for r in result] # General case, slowest method it = iter(iterable) result = [(key(elem), i, elem) for i, elem in zip(range(n), it)] if not result: return result _heapify_max(result) top = result[0][0] order = n _heapreplace = _heapreplace_max for elem in it: k = key(elem) if k < top: _heapreplace(result, (k, order, elem)) top = result[0][0] order += 1 result.sort() return [r[2] for r in result]
python
def nsmallest(n, iterable, key=None): """Find the n smallest elements in a dataset. Equivalent to: sorted(iterable, key=key)[:n] """ # Short-cut for n==1 is to use min() if n == 1: it = iter(iterable) sentinel = object() if key is None: result = min(it, default=sentinel) else: result = min(it, default=sentinel, key=key) return [] if result is sentinel else [result] # When n>=size, it's faster to use sorted() try: size = len(iterable) except (TypeError, AttributeError): pass else: if n >= size: return sorted(iterable, key=key)[:n] # When key is none, use simpler decoration if key is None: it = iter(iterable) # put the range(n) first so that zip() doesn't # consume one too many elements from the iterator result = [(elem, i) for i, elem in zip(range(n), it)] if not result: return result _heapify_max(result) top = result[0][0] order = n _heapreplace = _heapreplace_max for elem in it: if elem < top: _heapreplace(result, (elem, order)) top = result[0][0] order += 1 result.sort() return [r[0] for r in result] # General case, slowest method it = iter(iterable) result = [(key(elem), i, elem) for i, elem in zip(range(n), it)] if not result: return result _heapify_max(result) top = result[0][0] order = n _heapreplace = _heapreplace_max for elem in it: k = key(elem) if k < top: _heapreplace(result, (k, order, elem)) top = result[0][0] order += 1 result.sort() return [r[2] for r in result]
[ "def", "nsmallest", "(", "n", ",", "iterable", ",", "key", "=", "None", ")", ":", "# Short-cut for n==1 is to use min()", "if", "n", "==", "1", ":", "it", "=", "iter", "(", "iterable", ")", "sentinel", "=", "object", "(", ")", "if", "key", "is", "None", ":", "result", "=", "min", "(", "it", ",", "default", "=", "sentinel", ")", "else", ":", "result", "=", "min", "(", "it", ",", "default", "=", "sentinel", ",", "key", "=", "key", ")", "return", "[", "]", "if", "result", "is", "sentinel", "else", "[", "result", "]", "# When n>=size, it's faster to use sorted()", "try", ":", "size", "=", "len", "(", "iterable", ")", "except", "(", "TypeError", ",", "AttributeError", ")", ":", "pass", "else", ":", "if", "n", ">=", "size", ":", "return", "sorted", "(", "iterable", ",", "key", "=", "key", ")", "[", ":", "n", "]", "# When key is none, use simpler decoration", "if", "key", "is", "None", ":", "it", "=", "iter", "(", "iterable", ")", "# put the range(n) first so that zip() doesn't", "# consume one too many elements from the iterator", "result", "=", "[", "(", "elem", ",", "i", ")", "for", "i", ",", "elem", "in", "zip", "(", "range", "(", "n", ")", ",", "it", ")", "]", "if", "not", "result", ":", "return", "result", "_heapify_max", "(", "result", ")", "top", "=", "result", "[", "0", "]", "[", "0", "]", "order", "=", "n", "_heapreplace", "=", "_heapreplace_max", "for", "elem", "in", "it", ":", "if", "elem", "<", "top", ":", "_heapreplace", "(", "result", ",", "(", "elem", ",", "order", ")", ")", "top", "=", "result", "[", "0", "]", "[", "0", "]", "order", "+=", "1", "result", ".", "sort", "(", ")", "return", "[", "r", "[", "0", "]", "for", "r", "in", "result", "]", "# General case, slowest method", "it", "=", "iter", "(", "iterable", ")", "result", "=", "[", "(", "key", "(", "elem", ")", ",", "i", ",", "elem", ")", "for", "i", ",", "elem", "in", "zip", "(", "range", "(", "n", ")", ",", "it", ")", "]", "if", "not", "result", ":", "return", "result", "_heapify_max", "(", "result", ")", "top", "=", "result", "[", "0", "]", "[", "0", "]", "order", "=", "n", "_heapreplace", "=", "_heapreplace_max", "for", "elem", "in", "it", ":", "k", "=", "key", "(", "elem", ")", "if", "k", "<", "top", ":", "_heapreplace", "(", "result", ",", "(", "k", ",", "order", ",", "elem", ")", ")", "top", "=", "result", "[", "0", "]", "[", "0", "]", "order", "+=", "1", "result", ".", "sort", "(", ")", "return", "[", "r", "[", "2", "]", "for", "r", "in", "result", "]" ]
Find the n smallest elements in a dataset. Equivalent to: sorted(iterable, key=key)[:n]
[ "Find", "the", "n", "smallest", "elements", "in", "a", "dataset", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/heapq3.py#L742-L803
apache/spark
python/pyspark/heapq3.py
nlargest
def nlargest(n, iterable, key=None): """Find the n largest elements in a dataset. Equivalent to: sorted(iterable, key=key, reverse=True)[:n] """ # Short-cut for n==1 is to use max() if n == 1: it = iter(iterable) sentinel = object() if key is None: result = max(it, default=sentinel) else: result = max(it, default=sentinel, key=key) return [] if result is sentinel else [result] # When n>=size, it's faster to use sorted() try: size = len(iterable) except (TypeError, AttributeError): pass else: if n >= size: return sorted(iterable, key=key, reverse=True)[:n] # When key is none, use simpler decoration if key is None: it = iter(iterable) result = [(elem, i) for i, elem in zip(range(0, -n, -1), it)] if not result: return result heapify(result) top = result[0][0] order = -n _heapreplace = heapreplace for elem in it: if top < elem: _heapreplace(result, (elem, order)) top = result[0][0] order -= 1 result.sort(reverse=True) return [r[0] for r in result] # General case, slowest method it = iter(iterable) result = [(key(elem), i, elem) for i, elem in zip(range(0, -n, -1), it)] if not result: return result heapify(result) top = result[0][0] order = -n _heapreplace = heapreplace for elem in it: k = key(elem) if top < k: _heapreplace(result, (k, order, elem)) top = result[0][0] order -= 1 result.sort(reverse=True) return [r[2] for r in result]
python
def nlargest(n, iterable, key=None): """Find the n largest elements in a dataset. Equivalent to: sorted(iterable, key=key, reverse=True)[:n] """ # Short-cut for n==1 is to use max() if n == 1: it = iter(iterable) sentinel = object() if key is None: result = max(it, default=sentinel) else: result = max(it, default=sentinel, key=key) return [] if result is sentinel else [result] # When n>=size, it's faster to use sorted() try: size = len(iterable) except (TypeError, AttributeError): pass else: if n >= size: return sorted(iterable, key=key, reverse=True)[:n] # When key is none, use simpler decoration if key is None: it = iter(iterable) result = [(elem, i) for i, elem in zip(range(0, -n, -1), it)] if not result: return result heapify(result) top = result[0][0] order = -n _heapreplace = heapreplace for elem in it: if top < elem: _heapreplace(result, (elem, order)) top = result[0][0] order -= 1 result.sort(reverse=True) return [r[0] for r in result] # General case, slowest method it = iter(iterable) result = [(key(elem), i, elem) for i, elem in zip(range(0, -n, -1), it)] if not result: return result heapify(result) top = result[0][0] order = -n _heapreplace = heapreplace for elem in it: k = key(elem) if top < k: _heapreplace(result, (k, order, elem)) top = result[0][0] order -= 1 result.sort(reverse=True) return [r[2] for r in result]
[ "def", "nlargest", "(", "n", ",", "iterable", ",", "key", "=", "None", ")", ":", "# Short-cut for n==1 is to use max()", "if", "n", "==", "1", ":", "it", "=", "iter", "(", "iterable", ")", "sentinel", "=", "object", "(", ")", "if", "key", "is", "None", ":", "result", "=", "max", "(", "it", ",", "default", "=", "sentinel", ")", "else", ":", "result", "=", "max", "(", "it", ",", "default", "=", "sentinel", ",", "key", "=", "key", ")", "return", "[", "]", "if", "result", "is", "sentinel", "else", "[", "result", "]", "# When n>=size, it's faster to use sorted()", "try", ":", "size", "=", "len", "(", "iterable", ")", "except", "(", "TypeError", ",", "AttributeError", ")", ":", "pass", "else", ":", "if", "n", ">=", "size", ":", "return", "sorted", "(", "iterable", ",", "key", "=", "key", ",", "reverse", "=", "True", ")", "[", ":", "n", "]", "# When key is none, use simpler decoration", "if", "key", "is", "None", ":", "it", "=", "iter", "(", "iterable", ")", "result", "=", "[", "(", "elem", ",", "i", ")", "for", "i", ",", "elem", "in", "zip", "(", "range", "(", "0", ",", "-", "n", ",", "-", "1", ")", ",", "it", ")", "]", "if", "not", "result", ":", "return", "result", "heapify", "(", "result", ")", "top", "=", "result", "[", "0", "]", "[", "0", "]", "order", "=", "-", "n", "_heapreplace", "=", "heapreplace", "for", "elem", "in", "it", ":", "if", "top", "<", "elem", ":", "_heapreplace", "(", "result", ",", "(", "elem", ",", "order", ")", ")", "top", "=", "result", "[", "0", "]", "[", "0", "]", "order", "-=", "1", "result", ".", "sort", "(", "reverse", "=", "True", ")", "return", "[", "r", "[", "0", "]", "for", "r", "in", "result", "]", "# General case, slowest method", "it", "=", "iter", "(", "iterable", ")", "result", "=", "[", "(", "key", "(", "elem", ")", ",", "i", ",", "elem", ")", "for", "i", ",", "elem", "in", "zip", "(", "range", "(", "0", ",", "-", "n", ",", "-", "1", ")", ",", "it", ")", "]", "if", "not", "result", ":", "return", "result", "heapify", "(", "result", ")", "top", "=", "result", "[", "0", "]", "[", "0", "]", "order", "=", "-", "n", "_heapreplace", "=", "heapreplace", "for", "elem", "in", "it", ":", "k", "=", "key", "(", "elem", ")", "if", "top", "<", "k", ":", "_heapreplace", "(", "result", ",", "(", "k", ",", "order", ",", "elem", ")", ")", "top", "=", "result", "[", "0", "]", "[", "0", "]", "order", "-=", "1", "result", ".", "sort", "(", "reverse", "=", "True", ")", "return", "[", "r", "[", "2", "]", "for", "r", "in", "result", "]" ]
Find the n largest elements in a dataset. Equivalent to: sorted(iterable, key=key, reverse=True)[:n]
[ "Find", "the", "n", "largest", "elements", "in", "a", "dataset", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/heapq3.py#L805-L864
apache/spark
python/pyspark/ml/stat.py
Correlation.corr
def corr(dataset, column, method="pearson"): """ Compute the correlation matrix with specified method using dataset. :param dataset: A Dataset or a DataFrame. :param column: The name of the column of vectors for which the correlation coefficient needs to be computed. This must be a column of the dataset, and it must contain Vector objects. :param method: String specifying the method to use for computing correlation. Supported: `pearson` (default), `spearman`. :return: A DataFrame that contains the correlation matrix of the column of vectors. This DataFrame contains a single row and a single column of name '$METHODNAME($COLUMN)'. >>> from pyspark.ml.linalg import Vectors >>> from pyspark.ml.stat import Correlation >>> dataset = [[Vectors.dense([1, 0, 0, -2])], ... [Vectors.dense([4, 5, 0, 3])], ... [Vectors.dense([6, 7, 0, 8])], ... [Vectors.dense([9, 0, 0, 1])]] >>> dataset = spark.createDataFrame(dataset, ['features']) >>> pearsonCorr = Correlation.corr(dataset, 'features', 'pearson').collect()[0][0] >>> print(str(pearsonCorr).replace('nan', 'NaN')) DenseMatrix([[ 1. , 0.0556..., NaN, 0.4004...], [ 0.0556..., 1. , NaN, 0.9135...], [ NaN, NaN, 1. , NaN], [ 0.4004..., 0.9135..., NaN, 1. ]]) >>> spearmanCorr = Correlation.corr(dataset, 'features', method='spearman').collect()[0][0] >>> print(str(spearmanCorr).replace('nan', 'NaN')) DenseMatrix([[ 1. , 0.1054..., NaN, 0.4 ], [ 0.1054..., 1. , NaN, 0.9486... ], [ NaN, NaN, 1. , NaN], [ 0.4 , 0.9486... , NaN, 1. ]]) """ sc = SparkContext._active_spark_context javaCorrObj = _jvm().org.apache.spark.ml.stat.Correlation args = [_py2java(sc, arg) for arg in (dataset, column, method)] return _java2py(sc, javaCorrObj.corr(*args))
python
def corr(dataset, column, method="pearson"): """ Compute the correlation matrix with specified method using dataset. :param dataset: A Dataset or a DataFrame. :param column: The name of the column of vectors for which the correlation coefficient needs to be computed. This must be a column of the dataset, and it must contain Vector objects. :param method: String specifying the method to use for computing correlation. Supported: `pearson` (default), `spearman`. :return: A DataFrame that contains the correlation matrix of the column of vectors. This DataFrame contains a single row and a single column of name '$METHODNAME($COLUMN)'. >>> from pyspark.ml.linalg import Vectors >>> from pyspark.ml.stat import Correlation >>> dataset = [[Vectors.dense([1, 0, 0, -2])], ... [Vectors.dense([4, 5, 0, 3])], ... [Vectors.dense([6, 7, 0, 8])], ... [Vectors.dense([9, 0, 0, 1])]] >>> dataset = spark.createDataFrame(dataset, ['features']) >>> pearsonCorr = Correlation.corr(dataset, 'features', 'pearson').collect()[0][0] >>> print(str(pearsonCorr).replace('nan', 'NaN')) DenseMatrix([[ 1. , 0.0556..., NaN, 0.4004...], [ 0.0556..., 1. , NaN, 0.9135...], [ NaN, NaN, 1. , NaN], [ 0.4004..., 0.9135..., NaN, 1. ]]) >>> spearmanCorr = Correlation.corr(dataset, 'features', method='spearman').collect()[0][0] >>> print(str(spearmanCorr).replace('nan', 'NaN')) DenseMatrix([[ 1. , 0.1054..., NaN, 0.4 ], [ 0.1054..., 1. , NaN, 0.9486... ], [ NaN, NaN, 1. , NaN], [ 0.4 , 0.9486... , NaN, 1. ]]) """ sc = SparkContext._active_spark_context javaCorrObj = _jvm().org.apache.spark.ml.stat.Correlation args = [_py2java(sc, arg) for arg in (dataset, column, method)] return _java2py(sc, javaCorrObj.corr(*args))
[ "def", "corr", "(", "dataset", ",", "column", ",", "method", "=", "\"pearson\"", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "javaCorrObj", "=", "_jvm", "(", ")", ".", "org", ".", "apache", ".", "spark", ".", "ml", ".", "stat", ".", "Correlation", "args", "=", "[", "_py2java", "(", "sc", ",", "arg", ")", "for", "arg", "in", "(", "dataset", ",", "column", ",", "method", ")", "]", "return", "_java2py", "(", "sc", ",", "javaCorrObj", ".", "corr", "(", "*", "args", ")", ")" ]
Compute the correlation matrix with specified method using dataset. :param dataset: A Dataset or a DataFrame. :param column: The name of the column of vectors for which the correlation coefficient needs to be computed. This must be a column of the dataset, and it must contain Vector objects. :param method: String specifying the method to use for computing correlation. Supported: `pearson` (default), `spearman`. :return: A DataFrame that contains the correlation matrix of the column of vectors. This DataFrame contains a single row and a single column of name '$METHODNAME($COLUMN)'. >>> from pyspark.ml.linalg import Vectors >>> from pyspark.ml.stat import Correlation >>> dataset = [[Vectors.dense([1, 0, 0, -2])], ... [Vectors.dense([4, 5, 0, 3])], ... [Vectors.dense([6, 7, 0, 8])], ... [Vectors.dense([9, 0, 0, 1])]] >>> dataset = spark.createDataFrame(dataset, ['features']) >>> pearsonCorr = Correlation.corr(dataset, 'features', 'pearson').collect()[0][0] >>> print(str(pearsonCorr).replace('nan', 'NaN')) DenseMatrix([[ 1. , 0.0556..., NaN, 0.4004...], [ 0.0556..., 1. , NaN, 0.9135...], [ NaN, NaN, 1. , NaN], [ 0.4004..., 0.9135..., NaN, 1. ]]) >>> spearmanCorr = Correlation.corr(dataset, 'features', method='spearman').collect()[0][0] >>> print(str(spearmanCorr).replace('nan', 'NaN')) DenseMatrix([[ 1. , 0.1054..., NaN, 0.4 ], [ 0.1054..., 1. , NaN, 0.9486... ], [ NaN, NaN, 1. , NaN], [ 0.4 , 0.9486... , NaN, 1. ]])
[ "Compute", "the", "correlation", "matrix", "with", "specified", "method", "using", "dataset", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/stat.py#L95-L136
apache/spark
python/pyspark/ml/stat.py
Summarizer.metrics
def metrics(*metrics): """ Given a list of metrics, provides a builder that it turns computes metrics from a column. See the documentation of [[Summarizer]] for an example. The following metrics are accepted (case sensitive): - mean: a vector that contains the coefficient-wise mean. - variance: a vector tha contains the coefficient-wise variance. - count: the count of all vectors seen. - numNonzeros: a vector with the number of non-zeros for each coefficients - max: the maximum for each coefficient. - min: the minimum for each coefficient. - normL2: the Euclidean norm for each coefficient. - normL1: the L1 norm of each coefficient (sum of the absolute values). :param metrics: metrics that can be provided. :return: an object of :py:class:`pyspark.ml.stat.SummaryBuilder` Note: Currently, the performance of this interface is about 2x~3x slower then using the RDD interface. """ sc = SparkContext._active_spark_context js = JavaWrapper._new_java_obj("org.apache.spark.ml.stat.Summarizer.metrics", _to_seq(sc, metrics)) return SummaryBuilder(js)
python
def metrics(*metrics): """ Given a list of metrics, provides a builder that it turns computes metrics from a column. See the documentation of [[Summarizer]] for an example. The following metrics are accepted (case sensitive): - mean: a vector that contains the coefficient-wise mean. - variance: a vector tha contains the coefficient-wise variance. - count: the count of all vectors seen. - numNonzeros: a vector with the number of non-zeros for each coefficients - max: the maximum for each coefficient. - min: the minimum for each coefficient. - normL2: the Euclidean norm for each coefficient. - normL1: the L1 norm of each coefficient (sum of the absolute values). :param metrics: metrics that can be provided. :return: an object of :py:class:`pyspark.ml.stat.SummaryBuilder` Note: Currently, the performance of this interface is about 2x~3x slower then using the RDD interface. """ sc = SparkContext._active_spark_context js = JavaWrapper._new_java_obj("org.apache.spark.ml.stat.Summarizer.metrics", _to_seq(sc, metrics)) return SummaryBuilder(js)
[ "def", "metrics", "(", "*", "metrics", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "js", "=", "JavaWrapper", ".", "_new_java_obj", "(", "\"org.apache.spark.ml.stat.Summarizer.metrics\"", ",", "_to_seq", "(", "sc", ",", "metrics", ")", ")", "return", "SummaryBuilder", "(", "js", ")" ]
Given a list of metrics, provides a builder that it turns computes metrics from a column. See the documentation of [[Summarizer]] for an example. The following metrics are accepted (case sensitive): - mean: a vector that contains the coefficient-wise mean. - variance: a vector tha contains the coefficient-wise variance. - count: the count of all vectors seen. - numNonzeros: a vector with the number of non-zeros for each coefficients - max: the maximum for each coefficient. - min: the minimum for each coefficient. - normL2: the Euclidean norm for each coefficient. - normL1: the L1 norm of each coefficient (sum of the absolute values). :param metrics: metrics that can be provided. :return: an object of :py:class:`pyspark.ml.stat.SummaryBuilder` Note: Currently, the performance of this interface is about 2x~3x slower then using the RDD interface.
[ "Given", "a", "list", "of", "metrics", "provides", "a", "builder", "that", "it", "turns", "computes", "metrics", "from", "a", "column", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/stat.py#L326-L353
apache/spark
python/pyspark/ml/stat.py
SummaryBuilder.summary
def summary(self, featuresCol, weightCol=None): """ Returns an aggregate object that contains the summary of the column with the requested metrics. :param featuresCol: a column that contains features Vector object. :param weightCol: a column that contains weight value. Default weight is 1.0. :return: an aggregate column that contains the statistics. The exact content of this structure is determined during the creation of the builder. """ featuresCol, weightCol = Summarizer._check_param(featuresCol, weightCol) return Column(self._java_obj.summary(featuresCol._jc, weightCol._jc))
python
def summary(self, featuresCol, weightCol=None): """ Returns an aggregate object that contains the summary of the column with the requested metrics. :param featuresCol: a column that contains features Vector object. :param weightCol: a column that contains weight value. Default weight is 1.0. :return: an aggregate column that contains the statistics. The exact content of this structure is determined during the creation of the builder. """ featuresCol, weightCol = Summarizer._check_param(featuresCol, weightCol) return Column(self._java_obj.summary(featuresCol._jc, weightCol._jc))
[ "def", "summary", "(", "self", ",", "featuresCol", ",", "weightCol", "=", "None", ")", ":", "featuresCol", ",", "weightCol", "=", "Summarizer", ".", "_check_param", "(", "featuresCol", ",", "weightCol", ")", "return", "Column", "(", "self", ".", "_java_obj", ".", "summary", "(", "featuresCol", ".", "_jc", ",", "weightCol", ".", "_jc", ")", ")" ]
Returns an aggregate object that contains the summary of the column with the requested metrics. :param featuresCol: a column that contains features Vector object. :param weightCol: a column that contains weight value. Default weight is 1.0. :return: an aggregate column that contains the statistics. The exact content of this structure is determined during the creation of the builder.
[ "Returns", "an", "aggregate", "object", "that", "contains", "the", "summary", "of", "the", "column", "with", "the", "requested", "metrics", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/stat.py#L372-L386
apache/spark
python/pyspark/mllib/stat/_statistics.py
Statistics.corr
def corr(x, y=None, method=None): """ Compute the correlation (matrix) for the input RDD(s) using the specified method. Methods currently supported: I{pearson (default), spearman}. If a single RDD of Vectors is passed in, a correlation matrix comparing the columns in the input RDD is returned. Use C{method=} to specify the method to be used for single RDD inout. If two RDDs of floats are passed in, a single float is returned. :param x: an RDD of vector for which the correlation matrix is to be computed, or an RDD of float of the same cardinality as y when y is specified. :param y: an RDD of float of the same cardinality as x. :param method: String specifying the method to use for computing correlation. Supported: `pearson` (default), `spearman` :return: Correlation matrix comparing columns in x. >>> x = sc.parallelize([1.0, 0.0, -2.0], 2) >>> y = sc.parallelize([4.0, 5.0, 3.0], 2) >>> zeros = sc.parallelize([0.0, 0.0, 0.0], 2) >>> abs(Statistics.corr(x, y) - 0.6546537) < 1e-7 True >>> Statistics.corr(x, y) == Statistics.corr(x, y, "pearson") True >>> Statistics.corr(x, y, "spearman") 0.5 >>> from math import isnan >>> isnan(Statistics.corr(x, zeros)) True >>> from pyspark.mllib.linalg import Vectors >>> rdd = sc.parallelize([Vectors.dense([1, 0, 0, -2]), Vectors.dense([4, 5, 0, 3]), ... Vectors.dense([6, 7, 0, 8]), Vectors.dense([9, 0, 0, 1])]) >>> pearsonCorr = Statistics.corr(rdd) >>> print(str(pearsonCorr).replace('nan', 'NaN')) [[ 1. 0.05564149 NaN 0.40047142] [ 0.05564149 1. NaN 0.91359586] [ NaN NaN 1. NaN] [ 0.40047142 0.91359586 NaN 1. ]] >>> spearmanCorr = Statistics.corr(rdd, method="spearman") >>> print(str(spearmanCorr).replace('nan', 'NaN')) [[ 1. 0.10540926 NaN 0.4 ] [ 0.10540926 1. NaN 0.9486833 ] [ NaN NaN 1. NaN] [ 0.4 0.9486833 NaN 1. ]] >>> try: ... Statistics.corr(rdd, "spearman") ... print("Method name as second argument without 'method=' shouldn't be allowed.") ... except TypeError: ... pass """ # Check inputs to determine whether a single value or a matrix is needed for output. # Since it's legal for users to use the method name as the second argument, we need to # check if y is used to specify the method name instead. if type(y) == str: raise TypeError("Use 'method=' to specify method name.") if not y: return callMLlibFunc("corr", x.map(_convert_to_vector), method).toArray() else: return callMLlibFunc("corr", x.map(float), y.map(float), method)
python
def corr(x, y=None, method=None): """ Compute the correlation (matrix) for the input RDD(s) using the specified method. Methods currently supported: I{pearson (default), spearman}. If a single RDD of Vectors is passed in, a correlation matrix comparing the columns in the input RDD is returned. Use C{method=} to specify the method to be used for single RDD inout. If two RDDs of floats are passed in, a single float is returned. :param x: an RDD of vector for which the correlation matrix is to be computed, or an RDD of float of the same cardinality as y when y is specified. :param y: an RDD of float of the same cardinality as x. :param method: String specifying the method to use for computing correlation. Supported: `pearson` (default), `spearman` :return: Correlation matrix comparing columns in x. >>> x = sc.parallelize([1.0, 0.0, -2.0], 2) >>> y = sc.parallelize([4.0, 5.0, 3.0], 2) >>> zeros = sc.parallelize([0.0, 0.0, 0.0], 2) >>> abs(Statistics.corr(x, y) - 0.6546537) < 1e-7 True >>> Statistics.corr(x, y) == Statistics.corr(x, y, "pearson") True >>> Statistics.corr(x, y, "spearman") 0.5 >>> from math import isnan >>> isnan(Statistics.corr(x, zeros)) True >>> from pyspark.mllib.linalg import Vectors >>> rdd = sc.parallelize([Vectors.dense([1, 0, 0, -2]), Vectors.dense([4, 5, 0, 3]), ... Vectors.dense([6, 7, 0, 8]), Vectors.dense([9, 0, 0, 1])]) >>> pearsonCorr = Statistics.corr(rdd) >>> print(str(pearsonCorr).replace('nan', 'NaN')) [[ 1. 0.05564149 NaN 0.40047142] [ 0.05564149 1. NaN 0.91359586] [ NaN NaN 1. NaN] [ 0.40047142 0.91359586 NaN 1. ]] >>> spearmanCorr = Statistics.corr(rdd, method="spearman") >>> print(str(spearmanCorr).replace('nan', 'NaN')) [[ 1. 0.10540926 NaN 0.4 ] [ 0.10540926 1. NaN 0.9486833 ] [ NaN NaN 1. NaN] [ 0.4 0.9486833 NaN 1. ]] >>> try: ... Statistics.corr(rdd, "spearman") ... print("Method name as second argument without 'method=' shouldn't be allowed.") ... except TypeError: ... pass """ # Check inputs to determine whether a single value or a matrix is needed for output. # Since it's legal for users to use the method name as the second argument, we need to # check if y is used to specify the method name instead. if type(y) == str: raise TypeError("Use 'method=' to specify method name.") if not y: return callMLlibFunc("corr", x.map(_convert_to_vector), method).toArray() else: return callMLlibFunc("corr", x.map(float), y.map(float), method)
[ "def", "corr", "(", "x", ",", "y", "=", "None", ",", "method", "=", "None", ")", ":", "# Check inputs to determine whether a single value or a matrix is needed for output.", "# Since it's legal for users to use the method name as the second argument, we need to", "# check if y is used to specify the method name instead.", "if", "type", "(", "y", ")", "==", "str", ":", "raise", "TypeError", "(", "\"Use 'method=' to specify method name.\"", ")", "if", "not", "y", ":", "return", "callMLlibFunc", "(", "\"corr\"", ",", "x", ".", "map", "(", "_convert_to_vector", ")", ",", "method", ")", ".", "toArray", "(", ")", "else", ":", "return", "callMLlibFunc", "(", "\"corr\"", ",", "x", ".", "map", "(", "float", ")", ",", "y", ".", "map", "(", "float", ")", ",", "method", ")" ]
Compute the correlation (matrix) for the input RDD(s) using the specified method. Methods currently supported: I{pearson (default), spearman}. If a single RDD of Vectors is passed in, a correlation matrix comparing the columns in the input RDD is returned. Use C{method=} to specify the method to be used for single RDD inout. If two RDDs of floats are passed in, a single float is returned. :param x: an RDD of vector for which the correlation matrix is to be computed, or an RDD of float of the same cardinality as y when y is specified. :param y: an RDD of float of the same cardinality as x. :param method: String specifying the method to use for computing correlation. Supported: `pearson` (default), `spearman` :return: Correlation matrix comparing columns in x. >>> x = sc.parallelize([1.0, 0.0, -2.0], 2) >>> y = sc.parallelize([4.0, 5.0, 3.0], 2) >>> zeros = sc.parallelize([0.0, 0.0, 0.0], 2) >>> abs(Statistics.corr(x, y) - 0.6546537) < 1e-7 True >>> Statistics.corr(x, y) == Statistics.corr(x, y, "pearson") True >>> Statistics.corr(x, y, "spearman") 0.5 >>> from math import isnan >>> isnan(Statistics.corr(x, zeros)) True >>> from pyspark.mllib.linalg import Vectors >>> rdd = sc.parallelize([Vectors.dense([1, 0, 0, -2]), Vectors.dense([4, 5, 0, 3]), ... Vectors.dense([6, 7, 0, 8]), Vectors.dense([9, 0, 0, 1])]) >>> pearsonCorr = Statistics.corr(rdd) >>> print(str(pearsonCorr).replace('nan', 'NaN')) [[ 1. 0.05564149 NaN 0.40047142] [ 0.05564149 1. NaN 0.91359586] [ NaN NaN 1. NaN] [ 0.40047142 0.91359586 NaN 1. ]] >>> spearmanCorr = Statistics.corr(rdd, method="spearman") >>> print(str(spearmanCorr).replace('nan', 'NaN')) [[ 1. 0.10540926 NaN 0.4 ] [ 0.10540926 1. NaN 0.9486833 ] [ NaN NaN 1. NaN] [ 0.4 0.9486833 NaN 1. ]] >>> try: ... Statistics.corr(rdd, "spearman") ... print("Method name as second argument without 'method=' shouldn't be allowed.") ... except TypeError: ... pass
[ "Compute", "the", "correlation", "(", "matrix", ")", "for", "the", "input", "RDD", "(", "s", ")", "using", "the", "specified", "method", ".", "Methods", "currently", "supported", ":", "I", "{", "pearson", "(", "default", ")", "spearman", "}", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/mllib/stat/_statistics.py#L97-L157
apache/spark
python/pyspark/ml/tuning.py
_parallelFitTasks
def _parallelFitTasks(est, train, eva, validation, epm, collectSubModel): """ Creates a list of callables which can be called from different threads to fit and evaluate an estimator in parallel. Each callable returns an `(index, metric)` pair. :param est: Estimator, the estimator to be fit. :param train: DataFrame, training data set, used for fitting. :param eva: Evaluator, used to compute `metric` :param validation: DataFrame, validation data set, used for evaluation. :param epm: Sequence of ParamMap, params maps to be used during fitting & evaluation. :param collectSubModel: Whether to collect sub model. :return: (int, float, subModel), an index into `epm` and the associated metric value. """ modelIter = est.fitMultiple(train, epm) def singleTask(): index, model = next(modelIter) metric = eva.evaluate(model.transform(validation, epm[index])) return index, metric, model if collectSubModel else None return [singleTask] * len(epm)
python
def _parallelFitTasks(est, train, eva, validation, epm, collectSubModel): """ Creates a list of callables which can be called from different threads to fit and evaluate an estimator in parallel. Each callable returns an `(index, metric)` pair. :param est: Estimator, the estimator to be fit. :param train: DataFrame, training data set, used for fitting. :param eva: Evaluator, used to compute `metric` :param validation: DataFrame, validation data set, used for evaluation. :param epm: Sequence of ParamMap, params maps to be used during fitting & evaluation. :param collectSubModel: Whether to collect sub model. :return: (int, float, subModel), an index into `epm` and the associated metric value. """ modelIter = est.fitMultiple(train, epm) def singleTask(): index, model = next(modelIter) metric = eva.evaluate(model.transform(validation, epm[index])) return index, metric, model if collectSubModel else None return [singleTask] * len(epm)
[ "def", "_parallelFitTasks", "(", "est", ",", "train", ",", "eva", ",", "validation", ",", "epm", ",", "collectSubModel", ")", ":", "modelIter", "=", "est", ".", "fitMultiple", "(", "train", ",", "epm", ")", "def", "singleTask", "(", ")", ":", "index", ",", "model", "=", "next", "(", "modelIter", ")", "metric", "=", "eva", ".", "evaluate", "(", "model", ".", "transform", "(", "validation", ",", "epm", "[", "index", "]", ")", ")", "return", "index", ",", "metric", ",", "model", "if", "collectSubModel", "else", "None", "return", "[", "singleTask", "]", "*", "len", "(", "epm", ")" ]
Creates a list of callables which can be called from different threads to fit and evaluate an estimator in parallel. Each callable returns an `(index, metric)` pair. :param est: Estimator, the estimator to be fit. :param train: DataFrame, training data set, used for fitting. :param eva: Evaluator, used to compute `metric` :param validation: DataFrame, validation data set, used for evaluation. :param epm: Sequence of ParamMap, params maps to be used during fitting & evaluation. :param collectSubModel: Whether to collect sub model. :return: (int, float, subModel), an index into `epm` and the associated metric value.
[ "Creates", "a", "list", "of", "callables", "which", "can", "be", "called", "from", "different", "threads", "to", "fit", "and", "evaluate", "an", "estimator", "in", "parallel", ".", "Each", "callable", "returns", "an", "(", "index", "metric", ")", "pair", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/tuning.py#L36-L56
apache/spark
python/pyspark/ml/tuning.py
ParamGridBuilder.baseOn
def baseOn(self, *args): """ Sets the given parameters in this grid to fixed values. Accepts either a parameter dictionary or a list of (parameter, value) pairs. """ if isinstance(args[0], dict): self.baseOn(*args[0].items()) else: for (param, value) in args: self.addGrid(param, [value]) return self
python
def baseOn(self, *args): """ Sets the given parameters in this grid to fixed values. Accepts either a parameter dictionary or a list of (parameter, value) pairs. """ if isinstance(args[0], dict): self.baseOn(*args[0].items()) else: for (param, value) in args: self.addGrid(param, [value]) return self
[ "def", "baseOn", "(", "self", ",", "*", "args", ")", ":", "if", "isinstance", "(", "args", "[", "0", "]", ",", "dict", ")", ":", "self", ".", "baseOn", "(", "*", "args", "[", "0", "]", ".", "items", "(", ")", ")", "else", ":", "for", "(", "param", ",", "value", ")", "in", "args", ":", "self", ".", "addGrid", "(", "param", ",", "[", "value", "]", ")", "return", "self" ]
Sets the given parameters in this grid to fixed values. Accepts either a parameter dictionary or a list of (parameter, value) pairs.
[ "Sets", "the", "given", "parameters", "in", "this", "grid", "to", "fixed", "values", ".", "Accepts", "either", "a", "parameter", "dictionary", "or", "a", "list", "of", "(", "parameter", "value", ")", "pairs", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/tuning.py#L97-L108
apache/spark
python/pyspark/ml/tuning.py
ParamGridBuilder.build
def build(self): """ Builds and returns all combinations of parameters specified by the param grid. """ keys = self._param_grid.keys() grid_values = self._param_grid.values() def to_key_value_pairs(keys, values): return [(key, key.typeConverter(value)) for key, value in zip(keys, values)] return [dict(to_key_value_pairs(keys, prod)) for prod in itertools.product(*grid_values)]
python
def build(self): """ Builds and returns all combinations of parameters specified by the param grid. """ keys = self._param_grid.keys() grid_values = self._param_grid.values() def to_key_value_pairs(keys, values): return [(key, key.typeConverter(value)) for key, value in zip(keys, values)] return [dict(to_key_value_pairs(keys, prod)) for prod in itertools.product(*grid_values)]
[ "def", "build", "(", "self", ")", ":", "keys", "=", "self", ".", "_param_grid", ".", "keys", "(", ")", "grid_values", "=", "self", ".", "_param_grid", ".", "values", "(", ")", "def", "to_key_value_pairs", "(", "keys", ",", "values", ")", ":", "return", "[", "(", "key", ",", "key", ".", "typeConverter", "(", "value", ")", ")", "for", "key", ",", "value", "in", "zip", "(", "keys", ",", "values", ")", "]", "return", "[", "dict", "(", "to_key_value_pairs", "(", "keys", ",", "prod", ")", ")", "for", "prod", "in", "itertools", ".", "product", "(", "*", "grid_values", ")", "]" ]
Builds and returns all combinations of parameters specified by the param grid.
[ "Builds", "and", "returns", "all", "combinations", "of", "parameters", "specified", "by", "the", "param", "grid", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/tuning.py#L111-L122
apache/spark
python/pyspark/ml/tuning.py
ValidatorParams._from_java_impl
def _from_java_impl(cls, java_stage): """ Return Python estimator, estimatorParamMaps, and evaluator from a Java ValidatorParams. """ # Load information from java_stage to the instance. estimator = JavaParams._from_java(java_stage.getEstimator()) evaluator = JavaParams._from_java(java_stage.getEvaluator()) epms = [estimator._transfer_param_map_from_java(epm) for epm in java_stage.getEstimatorParamMaps()] return estimator, epms, evaluator
python
def _from_java_impl(cls, java_stage): """ Return Python estimator, estimatorParamMaps, and evaluator from a Java ValidatorParams. """ # Load information from java_stage to the instance. estimator = JavaParams._from_java(java_stage.getEstimator()) evaluator = JavaParams._from_java(java_stage.getEvaluator()) epms = [estimator._transfer_param_map_from_java(epm) for epm in java_stage.getEstimatorParamMaps()] return estimator, epms, evaluator
[ "def", "_from_java_impl", "(", "cls", ",", "java_stage", ")", ":", "# Load information from java_stage to the instance.", "estimator", "=", "JavaParams", ".", "_from_java", "(", "java_stage", ".", "getEstimator", "(", ")", ")", "evaluator", "=", "JavaParams", ".", "_from_java", "(", "java_stage", ".", "getEvaluator", "(", ")", ")", "epms", "=", "[", "estimator", ".", "_transfer_param_map_from_java", "(", "epm", ")", "for", "epm", "in", "java_stage", ".", "getEstimatorParamMaps", "(", ")", "]", "return", "estimator", ",", "epms", ",", "evaluator" ]
Return Python estimator, estimatorParamMaps, and evaluator from a Java ValidatorParams.
[ "Return", "Python", "estimator", "estimatorParamMaps", "and", "evaluator", "from", "a", "Java", "ValidatorParams", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/tuning.py#L173-L183
apache/spark
python/pyspark/ml/tuning.py
ValidatorParams._to_java_impl
def _to_java_impl(self): """ Return Java estimator, estimatorParamMaps, and evaluator from this Python instance. """ gateway = SparkContext._gateway cls = SparkContext._jvm.org.apache.spark.ml.param.ParamMap java_epms = gateway.new_array(cls, len(self.getEstimatorParamMaps())) for idx, epm in enumerate(self.getEstimatorParamMaps()): java_epms[idx] = self.getEstimator()._transfer_param_map_to_java(epm) java_estimator = self.getEstimator()._to_java() java_evaluator = self.getEvaluator()._to_java() return java_estimator, java_epms, java_evaluator
python
def _to_java_impl(self): """ Return Java estimator, estimatorParamMaps, and evaluator from this Python instance. """ gateway = SparkContext._gateway cls = SparkContext._jvm.org.apache.spark.ml.param.ParamMap java_epms = gateway.new_array(cls, len(self.getEstimatorParamMaps())) for idx, epm in enumerate(self.getEstimatorParamMaps()): java_epms[idx] = self.getEstimator()._transfer_param_map_to_java(epm) java_estimator = self.getEstimator()._to_java() java_evaluator = self.getEvaluator()._to_java() return java_estimator, java_epms, java_evaluator
[ "def", "_to_java_impl", "(", "self", ")", ":", "gateway", "=", "SparkContext", ".", "_gateway", "cls", "=", "SparkContext", ".", "_jvm", ".", "org", ".", "apache", ".", "spark", ".", "ml", ".", "param", ".", "ParamMap", "java_epms", "=", "gateway", ".", "new_array", "(", "cls", ",", "len", "(", "self", ".", "getEstimatorParamMaps", "(", ")", ")", ")", "for", "idx", ",", "epm", "in", "enumerate", "(", "self", ".", "getEstimatorParamMaps", "(", ")", ")", ":", "java_epms", "[", "idx", "]", "=", "self", ".", "getEstimator", "(", ")", ".", "_transfer_param_map_to_java", "(", "epm", ")", "java_estimator", "=", "self", ".", "getEstimator", "(", ")", ".", "_to_java", "(", ")", "java_evaluator", "=", "self", ".", "getEvaluator", "(", ")", ".", "_to_java", "(", ")", "return", "java_estimator", ",", "java_epms", ",", "java_evaluator" ]
Return Java estimator, estimatorParamMaps, and evaluator from this Python instance.
[ "Return", "Java", "estimator", "estimatorParamMaps", "and", "evaluator", "from", "this", "Python", "instance", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/tuning.py#L185-L199
apache/spark
python/pyspark/ml/tuning.py
CrossValidator._from_java
def _from_java(cls, java_stage): """ Given a Java CrossValidator, create and return a Python wrapper of it. Used for ML persistence. """ estimator, epms, evaluator = super(CrossValidator, cls)._from_java_impl(java_stage) numFolds = java_stage.getNumFolds() seed = java_stage.getSeed() parallelism = java_stage.getParallelism() collectSubModels = java_stage.getCollectSubModels() # Create a new instance of this stage. py_stage = cls(estimator=estimator, estimatorParamMaps=epms, evaluator=evaluator, numFolds=numFolds, seed=seed, parallelism=parallelism, collectSubModels=collectSubModels) py_stage._resetUid(java_stage.uid()) return py_stage
python
def _from_java(cls, java_stage): """ Given a Java CrossValidator, create and return a Python wrapper of it. Used for ML persistence. """ estimator, epms, evaluator = super(CrossValidator, cls)._from_java_impl(java_stage) numFolds = java_stage.getNumFolds() seed = java_stage.getSeed() parallelism = java_stage.getParallelism() collectSubModels = java_stage.getCollectSubModels() # Create a new instance of this stage. py_stage = cls(estimator=estimator, estimatorParamMaps=epms, evaluator=evaluator, numFolds=numFolds, seed=seed, parallelism=parallelism, collectSubModels=collectSubModels) py_stage._resetUid(java_stage.uid()) return py_stage
[ "def", "_from_java", "(", "cls", ",", "java_stage", ")", ":", "estimator", ",", "epms", ",", "evaluator", "=", "super", "(", "CrossValidator", ",", "cls", ")", ".", "_from_java_impl", "(", "java_stage", ")", "numFolds", "=", "java_stage", ".", "getNumFolds", "(", ")", "seed", "=", "java_stage", ".", "getSeed", "(", ")", "parallelism", "=", "java_stage", ".", "getParallelism", "(", ")", "collectSubModels", "=", "java_stage", ".", "getCollectSubModels", "(", ")", "# Create a new instance of this stage.", "py_stage", "=", "cls", "(", "estimator", "=", "estimator", ",", "estimatorParamMaps", "=", "epms", ",", "evaluator", "=", "evaluator", ",", "numFolds", "=", "numFolds", ",", "seed", "=", "seed", ",", "parallelism", "=", "parallelism", ",", "collectSubModels", "=", "collectSubModels", ")", "py_stage", ".", "_resetUid", "(", "java_stage", ".", "uid", "(", ")", ")", "return", "py_stage" ]
Given a Java CrossValidator, create and return a Python wrapper of it. Used for ML persistence.
[ "Given", "a", "Java", "CrossValidator", "create", "and", "return", "a", "Python", "wrapper", "of", "it", ".", "Used", "for", "ML", "persistence", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/tuning.py#L351-L367
apache/spark
python/pyspark/ml/tuning.py
CrossValidator._to_java
def _to_java(self): """ Transfer this instance to a Java CrossValidator. Used for ML persistence. :return: Java object equivalent to this instance. """ estimator, epms, evaluator = super(CrossValidator, self)._to_java_impl() _java_obj = JavaParams._new_java_obj("org.apache.spark.ml.tuning.CrossValidator", self.uid) _java_obj.setEstimatorParamMaps(epms) _java_obj.setEvaluator(evaluator) _java_obj.setEstimator(estimator) _java_obj.setSeed(self.getSeed()) _java_obj.setNumFolds(self.getNumFolds()) _java_obj.setParallelism(self.getParallelism()) _java_obj.setCollectSubModels(self.getCollectSubModels()) return _java_obj
python
def _to_java(self): """ Transfer this instance to a Java CrossValidator. Used for ML persistence. :return: Java object equivalent to this instance. """ estimator, epms, evaluator = super(CrossValidator, self)._to_java_impl() _java_obj = JavaParams._new_java_obj("org.apache.spark.ml.tuning.CrossValidator", self.uid) _java_obj.setEstimatorParamMaps(epms) _java_obj.setEvaluator(evaluator) _java_obj.setEstimator(estimator) _java_obj.setSeed(self.getSeed()) _java_obj.setNumFolds(self.getNumFolds()) _java_obj.setParallelism(self.getParallelism()) _java_obj.setCollectSubModels(self.getCollectSubModels()) return _java_obj
[ "def", "_to_java", "(", "self", ")", ":", "estimator", ",", "epms", ",", "evaluator", "=", "super", "(", "CrossValidator", ",", "self", ")", ".", "_to_java_impl", "(", ")", "_java_obj", "=", "JavaParams", ".", "_new_java_obj", "(", "\"org.apache.spark.ml.tuning.CrossValidator\"", ",", "self", ".", "uid", ")", "_java_obj", ".", "setEstimatorParamMaps", "(", "epms", ")", "_java_obj", ".", "setEvaluator", "(", "evaluator", ")", "_java_obj", ".", "setEstimator", "(", "estimator", ")", "_java_obj", ".", "setSeed", "(", "self", ".", "getSeed", "(", ")", ")", "_java_obj", ".", "setNumFolds", "(", "self", ".", "getNumFolds", "(", ")", ")", "_java_obj", ".", "setParallelism", "(", "self", ".", "getParallelism", "(", ")", ")", "_java_obj", ".", "setCollectSubModels", "(", "self", ".", "getCollectSubModels", "(", ")", ")", "return", "_java_obj" ]
Transfer this instance to a Java CrossValidator. Used for ML persistence. :return: Java object equivalent to this instance.
[ "Transfer", "this", "instance", "to", "a", "Java", "CrossValidator", ".", "Used", "for", "ML", "persistence", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/tuning.py#L369-L387
apache/spark
python/pyspark/ml/tuning.py
CrossValidatorModel.copy
def copy(self, extra=None): """ Creates a copy of this instance with a randomly generated uid and some extra params. This copies the underlying bestModel, creates a deep copy of the embedded paramMap, and copies the embedded and extra parameters over. It does not copy the extra Params into the subModels. :param extra: Extra parameters to copy to the new instance :return: Copy of this instance """ if extra is None: extra = dict() bestModel = self.bestModel.copy(extra) avgMetrics = self.avgMetrics subModels = self.subModels return CrossValidatorModel(bestModel, avgMetrics, subModels)
python
def copy(self, extra=None): """ Creates a copy of this instance with a randomly generated uid and some extra params. This copies the underlying bestModel, creates a deep copy of the embedded paramMap, and copies the embedded and extra parameters over. It does not copy the extra Params into the subModels. :param extra: Extra parameters to copy to the new instance :return: Copy of this instance """ if extra is None: extra = dict() bestModel = self.bestModel.copy(extra) avgMetrics = self.avgMetrics subModels = self.subModels return CrossValidatorModel(bestModel, avgMetrics, subModels)
[ "def", "copy", "(", "self", ",", "extra", "=", "None", ")", ":", "if", "extra", "is", "None", ":", "extra", "=", "dict", "(", ")", "bestModel", "=", "self", ".", "bestModel", ".", "copy", "(", "extra", ")", "avgMetrics", "=", "self", ".", "avgMetrics", "subModels", "=", "self", ".", "subModels", "return", "CrossValidatorModel", "(", "bestModel", ",", "avgMetrics", ",", "subModels", ")" ]
Creates a copy of this instance with a randomly generated uid and some extra params. This copies the underlying bestModel, creates a deep copy of the embedded paramMap, and copies the embedded and extra parameters over. It does not copy the extra Params into the subModels. :param extra: Extra parameters to copy to the new instance :return: Copy of this instance
[ "Creates", "a", "copy", "of", "this", "instance", "with", "a", "randomly", "generated", "uid", "and", "some", "extra", "params", ".", "This", "copies", "the", "underlying", "bestModel", "creates", "a", "deep", "copy", "of", "the", "embedded", "paramMap", "and", "copies", "the", "embedded", "and", "extra", "parameters", "over", ".", "It", "does", "not", "copy", "the", "extra", "Params", "into", "the", "subModels", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/tuning.py#L414-L430
apache/spark
python/pyspark/ml/tuning.py
TrainValidationSplit.setParams
def setParams(self, estimator=None, estimatorParamMaps=None, evaluator=None, trainRatio=0.75, parallelism=1, collectSubModels=False, seed=None): """ setParams(self, estimator=None, estimatorParamMaps=None, evaluator=None, trainRatio=0.75,\ parallelism=1, collectSubModels=False, seed=None): Sets params for the train validation split. """ kwargs = self._input_kwargs return self._set(**kwargs)
python
def setParams(self, estimator=None, estimatorParamMaps=None, evaluator=None, trainRatio=0.75, parallelism=1, collectSubModels=False, seed=None): """ setParams(self, estimator=None, estimatorParamMaps=None, evaluator=None, trainRatio=0.75,\ parallelism=1, collectSubModels=False, seed=None): Sets params for the train validation split. """ kwargs = self._input_kwargs return self._set(**kwargs)
[ "def", "setParams", "(", "self", ",", "estimator", "=", "None", ",", "estimatorParamMaps", "=", "None", ",", "evaluator", "=", "None", ",", "trainRatio", "=", "0.75", ",", "parallelism", "=", "1", ",", "collectSubModels", "=", "False", ",", "seed", "=", "None", ")", ":", "kwargs", "=", "self", ".", "_input_kwargs", "return", "self", ".", "_set", "(", "*", "*", "kwargs", ")" ]
setParams(self, estimator=None, estimatorParamMaps=None, evaluator=None, trainRatio=0.75,\ parallelism=1, collectSubModels=False, seed=None): Sets params for the train validation split.
[ "setParams", "(", "self", "estimator", "=", "None", "estimatorParamMaps", "=", "None", "evaluator", "=", "None", "trainRatio", "=", "0", ".", "75", "\\", "parallelism", "=", "1", "collectSubModels", "=", "False", "seed", "=", "None", ")", ":", "Sets", "params", "for", "the", "train", "validation", "split", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/tuning.py#L537-L545
apache/spark
python/pyspark/ml/tuning.py
TrainValidationSplit.copy
def copy(self, extra=None): """ Creates a copy of this instance with a randomly generated uid and some extra params. This copies creates a deep copy of the embedded paramMap, and copies the embedded and extra parameters over. :param extra: Extra parameters to copy to the new instance :return: Copy of this instance """ if extra is None: extra = dict() newTVS = Params.copy(self, extra) if self.isSet(self.estimator): newTVS.setEstimator(self.getEstimator().copy(extra)) # estimatorParamMaps remain the same if self.isSet(self.evaluator): newTVS.setEvaluator(self.getEvaluator().copy(extra)) return newTVS
python
def copy(self, extra=None): """ Creates a copy of this instance with a randomly generated uid and some extra params. This copies creates a deep copy of the embedded paramMap, and copies the embedded and extra parameters over. :param extra: Extra parameters to copy to the new instance :return: Copy of this instance """ if extra is None: extra = dict() newTVS = Params.copy(self, extra) if self.isSet(self.estimator): newTVS.setEstimator(self.getEstimator().copy(extra)) # estimatorParamMaps remain the same if self.isSet(self.evaluator): newTVS.setEvaluator(self.getEvaluator().copy(extra)) return newTVS
[ "def", "copy", "(", "self", ",", "extra", "=", "None", ")", ":", "if", "extra", "is", "None", ":", "extra", "=", "dict", "(", ")", "newTVS", "=", "Params", ".", "copy", "(", "self", ",", "extra", ")", "if", "self", ".", "isSet", "(", "self", ".", "estimator", ")", ":", "newTVS", ".", "setEstimator", "(", "self", ".", "getEstimator", "(", ")", ".", "copy", "(", "extra", ")", ")", "# estimatorParamMaps remain the same", "if", "self", ".", "isSet", "(", "self", ".", "evaluator", ")", ":", "newTVS", ".", "setEvaluator", "(", "self", ".", "getEvaluator", "(", ")", ".", "copy", "(", "extra", ")", ")", "return", "newTVS" ]
Creates a copy of this instance with a randomly generated uid and some extra params. This copies creates a deep copy of the embedded paramMap, and copies the embedded and extra parameters over. :param extra: Extra parameters to copy to the new instance :return: Copy of this instance
[ "Creates", "a", "copy", "of", "this", "instance", "with", "a", "randomly", "generated", "uid", "and", "some", "extra", "params", ".", "This", "copies", "creates", "a", "deep", "copy", "of", "the", "embedded", "paramMap", "and", "copies", "the", "embedded", "and", "extra", "parameters", "over", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/tuning.py#L598-L615
apache/spark
python/pyspark/ml/tuning.py
TrainValidationSplit._from_java
def _from_java(cls, java_stage): """ Given a Java TrainValidationSplit, create and return a Python wrapper of it. Used for ML persistence. """ estimator, epms, evaluator = super(TrainValidationSplit, cls)._from_java_impl(java_stage) trainRatio = java_stage.getTrainRatio() seed = java_stage.getSeed() parallelism = java_stage.getParallelism() collectSubModels = java_stage.getCollectSubModels() # Create a new instance of this stage. py_stage = cls(estimator=estimator, estimatorParamMaps=epms, evaluator=evaluator, trainRatio=trainRatio, seed=seed, parallelism=parallelism, collectSubModels=collectSubModels) py_stage._resetUid(java_stage.uid()) return py_stage
python
def _from_java(cls, java_stage): """ Given a Java TrainValidationSplit, create and return a Python wrapper of it. Used for ML persistence. """ estimator, epms, evaluator = super(TrainValidationSplit, cls)._from_java_impl(java_stage) trainRatio = java_stage.getTrainRatio() seed = java_stage.getSeed() parallelism = java_stage.getParallelism() collectSubModels = java_stage.getCollectSubModels() # Create a new instance of this stage. py_stage = cls(estimator=estimator, estimatorParamMaps=epms, evaluator=evaluator, trainRatio=trainRatio, seed=seed, parallelism=parallelism, collectSubModels=collectSubModels) py_stage._resetUid(java_stage.uid()) return py_stage
[ "def", "_from_java", "(", "cls", ",", "java_stage", ")", ":", "estimator", ",", "epms", ",", "evaluator", "=", "super", "(", "TrainValidationSplit", ",", "cls", ")", ".", "_from_java_impl", "(", "java_stage", ")", "trainRatio", "=", "java_stage", ".", "getTrainRatio", "(", ")", "seed", "=", "java_stage", ".", "getSeed", "(", ")", "parallelism", "=", "java_stage", ".", "getParallelism", "(", ")", "collectSubModels", "=", "java_stage", ".", "getCollectSubModels", "(", ")", "# Create a new instance of this stage.", "py_stage", "=", "cls", "(", "estimator", "=", "estimator", ",", "estimatorParamMaps", "=", "epms", ",", "evaluator", "=", "evaluator", ",", "trainRatio", "=", "trainRatio", ",", "seed", "=", "seed", ",", "parallelism", "=", "parallelism", ",", "collectSubModels", "=", "collectSubModels", ")", "py_stage", ".", "_resetUid", "(", "java_stage", ".", "uid", "(", ")", ")", "return", "py_stage" ]
Given a Java TrainValidationSplit, create and return a Python wrapper of it. Used for ML persistence.
[ "Given", "a", "Java", "TrainValidationSplit", "create", "and", "return", "a", "Python", "wrapper", "of", "it", ".", "Used", "for", "ML", "persistence", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/tuning.py#L629-L645
apache/spark
python/pyspark/ml/tuning.py
TrainValidationSplit._to_java
def _to_java(self): """ Transfer this instance to a Java TrainValidationSplit. Used for ML persistence. :return: Java object equivalent to this instance. """ estimator, epms, evaluator = super(TrainValidationSplit, self)._to_java_impl() _java_obj = JavaParams._new_java_obj("org.apache.spark.ml.tuning.TrainValidationSplit", self.uid) _java_obj.setEstimatorParamMaps(epms) _java_obj.setEvaluator(evaluator) _java_obj.setEstimator(estimator) _java_obj.setTrainRatio(self.getTrainRatio()) _java_obj.setSeed(self.getSeed()) _java_obj.setParallelism(self.getParallelism()) _java_obj.setCollectSubModels(self.getCollectSubModels()) return _java_obj
python
def _to_java(self): """ Transfer this instance to a Java TrainValidationSplit. Used for ML persistence. :return: Java object equivalent to this instance. """ estimator, epms, evaluator = super(TrainValidationSplit, self)._to_java_impl() _java_obj = JavaParams._new_java_obj("org.apache.spark.ml.tuning.TrainValidationSplit", self.uid) _java_obj.setEstimatorParamMaps(epms) _java_obj.setEvaluator(evaluator) _java_obj.setEstimator(estimator) _java_obj.setTrainRatio(self.getTrainRatio()) _java_obj.setSeed(self.getSeed()) _java_obj.setParallelism(self.getParallelism()) _java_obj.setCollectSubModels(self.getCollectSubModels()) return _java_obj
[ "def", "_to_java", "(", "self", ")", ":", "estimator", ",", "epms", ",", "evaluator", "=", "super", "(", "TrainValidationSplit", ",", "self", ")", ".", "_to_java_impl", "(", ")", "_java_obj", "=", "JavaParams", ".", "_new_java_obj", "(", "\"org.apache.spark.ml.tuning.TrainValidationSplit\"", ",", "self", ".", "uid", ")", "_java_obj", ".", "setEstimatorParamMaps", "(", "epms", ")", "_java_obj", ".", "setEvaluator", "(", "evaluator", ")", "_java_obj", ".", "setEstimator", "(", "estimator", ")", "_java_obj", ".", "setTrainRatio", "(", "self", ".", "getTrainRatio", "(", ")", ")", "_java_obj", ".", "setSeed", "(", "self", ".", "getSeed", "(", ")", ")", "_java_obj", ".", "setParallelism", "(", "self", ".", "getParallelism", "(", ")", ")", "_java_obj", ".", "setCollectSubModels", "(", "self", ".", "getCollectSubModels", "(", ")", ")", "return", "_java_obj" ]
Transfer this instance to a Java TrainValidationSplit. Used for ML persistence. :return: Java object equivalent to this instance.
[ "Transfer", "this", "instance", "to", "a", "Java", "TrainValidationSplit", ".", "Used", "for", "ML", "persistence", ".", ":", "return", ":", "Java", "object", "equivalent", "to", "this", "instance", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/tuning.py#L647-L664
apache/spark
python/pyspark/ml/tuning.py
TrainValidationSplitModel.copy
def copy(self, extra=None): """ Creates a copy of this instance with a randomly generated uid and some extra params. This copies the underlying bestModel, creates a deep copy of the embedded paramMap, and copies the embedded and extra parameters over. And, this creates a shallow copy of the validationMetrics. It does not copy the extra Params into the subModels. :param extra: Extra parameters to copy to the new instance :return: Copy of this instance """ if extra is None: extra = dict() bestModel = self.bestModel.copy(extra) validationMetrics = list(self.validationMetrics) subModels = self.subModels return TrainValidationSplitModel(bestModel, validationMetrics, subModels)
python
def copy(self, extra=None): """ Creates a copy of this instance with a randomly generated uid and some extra params. This copies the underlying bestModel, creates a deep copy of the embedded paramMap, and copies the embedded and extra parameters over. And, this creates a shallow copy of the validationMetrics. It does not copy the extra Params into the subModels. :param extra: Extra parameters to copy to the new instance :return: Copy of this instance """ if extra is None: extra = dict() bestModel = self.bestModel.copy(extra) validationMetrics = list(self.validationMetrics) subModels = self.subModels return TrainValidationSplitModel(bestModel, validationMetrics, subModels)
[ "def", "copy", "(", "self", ",", "extra", "=", "None", ")", ":", "if", "extra", "is", "None", ":", "extra", "=", "dict", "(", ")", "bestModel", "=", "self", ".", "bestModel", ".", "copy", "(", "extra", ")", "validationMetrics", "=", "list", "(", "self", ".", "validationMetrics", ")", "subModels", "=", "self", ".", "subModels", "return", "TrainValidationSplitModel", "(", "bestModel", ",", "validationMetrics", ",", "subModels", ")" ]
Creates a copy of this instance with a randomly generated uid and some extra params. This copies the underlying bestModel, creates a deep copy of the embedded paramMap, and copies the embedded and extra parameters over. And, this creates a shallow copy of the validationMetrics. It does not copy the extra Params into the subModels. :param extra: Extra parameters to copy to the new instance :return: Copy of this instance
[ "Creates", "a", "copy", "of", "this", "instance", "with", "a", "randomly", "generated", "uid", "and", "some", "extra", "params", ".", "This", "copies", "the", "underlying", "bestModel", "creates", "a", "deep", "copy", "of", "the", "embedded", "paramMap", "and", "copies", "the", "embedded", "and", "extra", "parameters", "over", ".", "And", "this", "creates", "a", "shallow", "copy", "of", "the", "validationMetrics", ".", "It", "does", "not", "copy", "the", "extra", "Params", "into", "the", "subModels", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/tuning.py#L689-L706
apache/spark
python/pyspark/ml/tuning.py
TrainValidationSplitModel._from_java
def _from_java(cls, java_stage): """ Given a Java TrainValidationSplitModel, create and return a Python wrapper of it. Used for ML persistence. """ # Load information from java_stage to the instance. bestModel = JavaParams._from_java(java_stage.bestModel()) estimator, epms, evaluator = super(TrainValidationSplitModel, cls)._from_java_impl(java_stage) # Create a new instance of this stage. py_stage = cls(bestModel=bestModel).setEstimator(estimator) py_stage = py_stage.setEstimatorParamMaps(epms).setEvaluator(evaluator) if java_stage.hasSubModels(): py_stage.subModels = [JavaParams._from_java(sub_model) for sub_model in java_stage.subModels()] py_stage._resetUid(java_stage.uid()) return py_stage
python
def _from_java(cls, java_stage): """ Given a Java TrainValidationSplitModel, create and return a Python wrapper of it. Used for ML persistence. """ # Load information from java_stage to the instance. bestModel = JavaParams._from_java(java_stage.bestModel()) estimator, epms, evaluator = super(TrainValidationSplitModel, cls)._from_java_impl(java_stage) # Create a new instance of this stage. py_stage = cls(bestModel=bestModel).setEstimator(estimator) py_stage = py_stage.setEstimatorParamMaps(epms).setEvaluator(evaluator) if java_stage.hasSubModels(): py_stage.subModels = [JavaParams._from_java(sub_model) for sub_model in java_stage.subModels()] py_stage._resetUid(java_stage.uid()) return py_stage
[ "def", "_from_java", "(", "cls", ",", "java_stage", ")", ":", "# Load information from java_stage to the instance.", "bestModel", "=", "JavaParams", ".", "_from_java", "(", "java_stage", ".", "bestModel", "(", ")", ")", "estimator", ",", "epms", ",", "evaluator", "=", "super", "(", "TrainValidationSplitModel", ",", "cls", ")", ".", "_from_java_impl", "(", "java_stage", ")", "# Create a new instance of this stage.", "py_stage", "=", "cls", "(", "bestModel", "=", "bestModel", ")", ".", "setEstimator", "(", "estimator", ")", "py_stage", "=", "py_stage", ".", "setEstimatorParamMaps", "(", "epms", ")", ".", "setEvaluator", "(", "evaluator", ")", "if", "java_stage", ".", "hasSubModels", "(", ")", ":", "py_stage", ".", "subModels", "=", "[", "JavaParams", ".", "_from_java", "(", "sub_model", ")", "for", "sub_model", "in", "java_stage", ".", "subModels", "(", ")", "]", "py_stage", ".", "_resetUid", "(", "java_stage", ".", "uid", "(", ")", ")", "return", "py_stage" ]
Given a Java TrainValidationSplitModel, create and return a Python wrapper of it. Used for ML persistence.
[ "Given", "a", "Java", "TrainValidationSplitModel", "create", "and", "return", "a", "Python", "wrapper", "of", "it", ".", "Used", "for", "ML", "persistence", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/tuning.py#L720-L739
apache/spark
python/pyspark/ml/tuning.py
TrainValidationSplitModel._to_java
def _to_java(self): """ Transfer this instance to a Java TrainValidationSplitModel. Used for ML persistence. :return: Java object equivalent to this instance. """ sc = SparkContext._active_spark_context # TODO: persst validation metrics as well _java_obj = JavaParams._new_java_obj( "org.apache.spark.ml.tuning.TrainValidationSplitModel", self.uid, self.bestModel._to_java(), _py2java(sc, [])) estimator, epms, evaluator = super(TrainValidationSplitModel, self)._to_java_impl() _java_obj.set("evaluator", evaluator) _java_obj.set("estimator", estimator) _java_obj.set("estimatorParamMaps", epms) if self.subModels is not None: java_sub_models = [sub_model._to_java() for sub_model in self.subModels] _java_obj.setSubModels(java_sub_models) return _java_obj
python
def _to_java(self): """ Transfer this instance to a Java TrainValidationSplitModel. Used for ML persistence. :return: Java object equivalent to this instance. """ sc = SparkContext._active_spark_context # TODO: persst validation metrics as well _java_obj = JavaParams._new_java_obj( "org.apache.spark.ml.tuning.TrainValidationSplitModel", self.uid, self.bestModel._to_java(), _py2java(sc, [])) estimator, epms, evaluator = super(TrainValidationSplitModel, self)._to_java_impl() _java_obj.set("evaluator", evaluator) _java_obj.set("estimator", estimator) _java_obj.set("estimatorParamMaps", epms) if self.subModels is not None: java_sub_models = [sub_model._to_java() for sub_model in self.subModels] _java_obj.setSubModels(java_sub_models) return _java_obj
[ "def", "_to_java", "(", "self", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "# TODO: persst validation metrics as well", "_java_obj", "=", "JavaParams", ".", "_new_java_obj", "(", "\"org.apache.spark.ml.tuning.TrainValidationSplitModel\"", ",", "self", ".", "uid", ",", "self", ".", "bestModel", ".", "_to_java", "(", ")", ",", "_py2java", "(", "sc", ",", "[", "]", ")", ")", "estimator", ",", "epms", ",", "evaluator", "=", "super", "(", "TrainValidationSplitModel", ",", "self", ")", ".", "_to_java_impl", "(", ")", "_java_obj", ".", "set", "(", "\"evaluator\"", ",", "evaluator", ")", "_java_obj", ".", "set", "(", "\"estimator\"", ",", "estimator", ")", "_java_obj", ".", "set", "(", "\"estimatorParamMaps\"", ",", "epms", ")", "if", "self", ".", "subModels", "is", "not", "None", ":", "java_sub_models", "=", "[", "sub_model", ".", "_to_java", "(", ")", "for", "sub_model", "in", "self", ".", "subModels", "]", "_java_obj", ".", "setSubModels", "(", "java_sub_models", ")", "return", "_java_obj" ]
Transfer this instance to a Java TrainValidationSplitModel. Used for ML persistence. :return: Java object equivalent to this instance.
[ "Transfer", "this", "instance", "to", "a", "Java", "TrainValidationSplitModel", ".", "Used", "for", "ML", "persistence", ".", ":", "return", ":", "Java", "object", "equivalent", "to", "this", "instance", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/tuning.py#L741-L764
apache/spark
python/pyspark/sql/conf.py
RuntimeConfig.get
def get(self, key, default=_NoValue): """Returns the value of Spark runtime configuration property for the given key, assuming it is set. """ self._checkType(key, "key") if default is _NoValue: return self._jconf.get(key) else: if default is not None: self._checkType(default, "default") return self._jconf.get(key, default)
python
def get(self, key, default=_NoValue): """Returns the value of Spark runtime configuration property for the given key, assuming it is set. """ self._checkType(key, "key") if default is _NoValue: return self._jconf.get(key) else: if default is not None: self._checkType(default, "default") return self._jconf.get(key, default)
[ "def", "get", "(", "self", ",", "key", ",", "default", "=", "_NoValue", ")", ":", "self", ".", "_checkType", "(", "key", ",", "\"key\"", ")", "if", "default", "is", "_NoValue", ":", "return", "self", ".", "_jconf", ".", "get", "(", "key", ")", "else", ":", "if", "default", "is", "not", "None", ":", "self", ".", "_checkType", "(", "default", ",", "\"default\"", ")", "return", "self", ".", "_jconf", ".", "get", "(", "key", ",", "default", ")" ]
Returns the value of Spark runtime configuration property for the given key, assuming it is set.
[ "Returns", "the", "value", "of", "Spark", "runtime", "configuration", "property", "for", "the", "given", "key", "assuming", "it", "is", "set", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/conf.py#L45-L55
apache/spark
python/pyspark/sql/conf.py
RuntimeConfig._checkType
def _checkType(self, obj, identifier): """Assert that an object is of type str.""" if not isinstance(obj, basestring): raise TypeError("expected %s '%s' to be a string (was '%s')" % (identifier, obj, type(obj).__name__))
python
def _checkType(self, obj, identifier): """Assert that an object is of type str.""" if not isinstance(obj, basestring): raise TypeError("expected %s '%s' to be a string (was '%s')" % (identifier, obj, type(obj).__name__))
[ "def", "_checkType", "(", "self", ",", "obj", ",", "identifier", ")", ":", "if", "not", "isinstance", "(", "obj", ",", "basestring", ")", ":", "raise", "TypeError", "(", "\"expected %s '%s' to be a string (was '%s')\"", "%", "(", "identifier", ",", "obj", ",", "type", "(", "obj", ")", ".", "__name__", ")", ")" ]
Assert that an object is of type str.
[ "Assert", "that", "an", "object", "is", "of", "type", "str", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/conf.py#L63-L67
apache/spark
python/pyspark/sql/functions.py
_create_function
def _create_function(name, doc=""): """Create a PySpark function by its name""" def _(col): sc = SparkContext._active_spark_context jc = getattr(sc._jvm.functions, name)(col._jc if isinstance(col, Column) else col) return Column(jc) _.__name__ = name _.__doc__ = doc return _
python
def _create_function(name, doc=""): """Create a PySpark function by its name""" def _(col): sc = SparkContext._active_spark_context jc = getattr(sc._jvm.functions, name)(col._jc if isinstance(col, Column) else col) return Column(jc) _.__name__ = name _.__doc__ = doc return _
[ "def", "_create_function", "(", "name", ",", "doc", "=", "\"\"", ")", ":", "def", "_", "(", "col", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "jc", "=", "getattr", "(", "sc", ".", "_jvm", ".", "functions", ",", "name", ")", "(", "col", ".", "_jc", "if", "isinstance", "(", "col", ",", "Column", ")", "else", "col", ")", "return", "Column", "(", "jc", ")", "_", ".", "__name__", "=", "name", "_", ".", "__doc__", "=", "doc", "return", "_" ]
Create a PySpark function by its name
[ "Create", "a", "PySpark", "function", "by", "its", "name" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L47-L55
apache/spark
python/pyspark/sql/functions.py
_create_function_over_column
def _create_function_over_column(name, doc=""): """Similar with `_create_function` but creates a PySpark function that takes a column (as string as well). This is mainly for PySpark functions to take strings as column names. """ def _(col): sc = SparkContext._active_spark_context jc = getattr(sc._jvm.functions, name)(_to_java_column(col)) return Column(jc) _.__name__ = name _.__doc__ = doc return _
python
def _create_function_over_column(name, doc=""): """Similar with `_create_function` but creates a PySpark function that takes a column (as string as well). This is mainly for PySpark functions to take strings as column names. """ def _(col): sc = SparkContext._active_spark_context jc = getattr(sc._jvm.functions, name)(_to_java_column(col)) return Column(jc) _.__name__ = name _.__doc__ = doc return _
[ "def", "_create_function_over_column", "(", "name", ",", "doc", "=", "\"\"", ")", ":", "def", "_", "(", "col", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "jc", "=", "getattr", "(", "sc", ".", "_jvm", ".", "functions", ",", "name", ")", "(", "_to_java_column", "(", "col", ")", ")", "return", "Column", "(", "jc", ")", "_", ".", "__name__", "=", "name", "_", ".", "__doc__", "=", "doc", "return", "_" ]
Similar with `_create_function` but creates a PySpark function that takes a column (as string as well). This is mainly for PySpark functions to take strings as column names.
[ "Similar", "with", "_create_function", "but", "creates", "a", "PySpark", "function", "that", "takes", "a", "column", "(", "as", "string", "as", "well", ")", ".", "This", "is", "mainly", "for", "PySpark", "functions", "to", "take", "strings", "as", "column", "names", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L58-L69
apache/spark
python/pyspark/sql/functions.py
_wrap_deprecated_function
def _wrap_deprecated_function(func, message): """ Wrap the deprecated function to print out deprecation warnings""" def _(col): warnings.warn(message, DeprecationWarning) return func(col) return functools.wraps(func)(_)
python
def _wrap_deprecated_function(func, message): """ Wrap the deprecated function to print out deprecation warnings""" def _(col): warnings.warn(message, DeprecationWarning) return func(col) return functools.wraps(func)(_)
[ "def", "_wrap_deprecated_function", "(", "func", ",", "message", ")", ":", "def", "_", "(", "col", ")", ":", "warnings", ".", "warn", "(", "message", ",", "DeprecationWarning", ")", "return", "func", "(", "col", ")", "return", "functools", ".", "wraps", "(", "func", ")", "(", "_", ")" ]
Wrap the deprecated function to print out deprecation warnings
[ "Wrap", "the", "deprecated", "function", "to", "print", "out", "deprecation", "warnings" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L72-L77
apache/spark
python/pyspark/sql/functions.py
_create_binary_mathfunction
def _create_binary_mathfunction(name, doc=""): """ Create a binary mathfunction by name""" def _(col1, col2): sc = SparkContext._active_spark_context # For legacy reasons, the arguments here can be implicitly converted into floats, # if they are not columns or strings. if isinstance(col1, Column): arg1 = col1._jc elif isinstance(col1, basestring): arg1 = _create_column_from_name(col1) else: arg1 = float(col1) if isinstance(col2, Column): arg2 = col2._jc elif isinstance(col2, basestring): arg2 = _create_column_from_name(col2) else: arg2 = float(col2) jc = getattr(sc._jvm.functions, name)(arg1, arg2) return Column(jc) _.__name__ = name _.__doc__ = doc return _
python
def _create_binary_mathfunction(name, doc=""): """ Create a binary mathfunction by name""" def _(col1, col2): sc = SparkContext._active_spark_context # For legacy reasons, the arguments here can be implicitly converted into floats, # if they are not columns or strings. if isinstance(col1, Column): arg1 = col1._jc elif isinstance(col1, basestring): arg1 = _create_column_from_name(col1) else: arg1 = float(col1) if isinstance(col2, Column): arg2 = col2._jc elif isinstance(col2, basestring): arg2 = _create_column_from_name(col2) else: arg2 = float(col2) jc = getattr(sc._jvm.functions, name)(arg1, arg2) return Column(jc) _.__name__ = name _.__doc__ = doc return _
[ "def", "_create_binary_mathfunction", "(", "name", ",", "doc", "=", "\"\"", ")", ":", "def", "_", "(", "col1", ",", "col2", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "# For legacy reasons, the arguments here can be implicitly converted into floats,", "# if they are not columns or strings.", "if", "isinstance", "(", "col1", ",", "Column", ")", ":", "arg1", "=", "col1", ".", "_jc", "elif", "isinstance", "(", "col1", ",", "basestring", ")", ":", "arg1", "=", "_create_column_from_name", "(", "col1", ")", "else", ":", "arg1", "=", "float", "(", "col1", ")", "if", "isinstance", "(", "col2", ",", "Column", ")", ":", "arg2", "=", "col2", ".", "_jc", "elif", "isinstance", "(", "col2", ",", "basestring", ")", ":", "arg2", "=", "_create_column_from_name", "(", "col2", ")", "else", ":", "arg2", "=", "float", "(", "col2", ")", "jc", "=", "getattr", "(", "sc", ".", "_jvm", ".", "functions", ",", "name", ")", "(", "arg1", ",", "arg2", ")", "return", "Column", "(", "jc", ")", "_", ".", "__name__", "=", "name", "_", ".", "__doc__", "=", "doc", "return", "_" ]
Create a binary mathfunction by name
[ "Create", "a", "binary", "mathfunction", "by", "name" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L80-L104
apache/spark
python/pyspark/sql/functions.py
_create_window_function
def _create_window_function(name, doc=''): """ Create a window function by name """ def _(): sc = SparkContext._active_spark_context jc = getattr(sc._jvm.functions, name)() return Column(jc) _.__name__ = name _.__doc__ = 'Window function: ' + doc return _
python
def _create_window_function(name, doc=''): """ Create a window function by name """ def _(): sc = SparkContext._active_spark_context jc = getattr(sc._jvm.functions, name)() return Column(jc) _.__name__ = name _.__doc__ = 'Window function: ' + doc return _
[ "def", "_create_window_function", "(", "name", ",", "doc", "=", "''", ")", ":", "def", "_", "(", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "jc", "=", "getattr", "(", "sc", ".", "_jvm", ".", "functions", ",", "name", ")", "(", ")", "return", "Column", "(", "jc", ")", "_", ".", "__name__", "=", "name", "_", ".", "__doc__", "=", "'Window function: '", "+", "doc", "return", "_" ]
Create a window function by name
[ "Create", "a", "window", "function", "by", "name" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L107-L115
apache/spark
python/pyspark/sql/functions.py
approx_count_distinct
def approx_count_distinct(col, rsd=None): """Aggregate function: returns a new :class:`Column` for approximate distinct count of column `col`. :param rsd: maximum estimation error allowed (default = 0.05). For rsd < 0.01, it is more efficient to use :func:`countDistinct` >>> df.agg(approx_count_distinct(df.age).alias('distinct_ages')).collect() [Row(distinct_ages=2)] """ sc = SparkContext._active_spark_context if rsd is None: jc = sc._jvm.functions.approx_count_distinct(_to_java_column(col)) else: jc = sc._jvm.functions.approx_count_distinct(_to_java_column(col), rsd) return Column(jc)
python
def approx_count_distinct(col, rsd=None): """Aggregate function: returns a new :class:`Column` for approximate distinct count of column `col`. :param rsd: maximum estimation error allowed (default = 0.05). For rsd < 0.01, it is more efficient to use :func:`countDistinct` >>> df.agg(approx_count_distinct(df.age).alias('distinct_ages')).collect() [Row(distinct_ages=2)] """ sc = SparkContext._active_spark_context if rsd is None: jc = sc._jvm.functions.approx_count_distinct(_to_java_column(col)) else: jc = sc._jvm.functions.approx_count_distinct(_to_java_column(col), rsd) return Column(jc)
[ "def", "approx_count_distinct", "(", "col", ",", "rsd", "=", "None", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "if", "rsd", "is", "None", ":", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "approx_count_distinct", "(", "_to_java_column", "(", "col", ")", ")", "else", ":", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "approx_count_distinct", "(", "_to_java_column", "(", "col", ")", ",", "rsd", ")", "return", "Column", "(", "jc", ")" ]
Aggregate function: returns a new :class:`Column` for approximate distinct count of column `col`. :param rsd: maximum estimation error allowed (default = 0.05). For rsd < 0.01, it is more efficient to use :func:`countDistinct` >>> df.agg(approx_count_distinct(df.age).alias('distinct_ages')).collect() [Row(distinct_ages=2)]
[ "Aggregate", "function", ":", "returns", "a", "new", ":", "class", ":", "Column", "for", "approximate", "distinct", "count", "of", "column", "col", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L314-L329
apache/spark
python/pyspark/sql/functions.py
broadcast
def broadcast(df): """Marks a DataFrame as small enough for use in broadcast joins.""" sc = SparkContext._active_spark_context return DataFrame(sc._jvm.functions.broadcast(df._jdf), df.sql_ctx)
python
def broadcast(df): """Marks a DataFrame as small enough for use in broadcast joins.""" sc = SparkContext._active_spark_context return DataFrame(sc._jvm.functions.broadcast(df._jdf), df.sql_ctx)
[ "def", "broadcast", "(", "df", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "DataFrame", "(", "sc", ".", "_jvm", ".", "functions", ".", "broadcast", "(", "df", ".", "_jdf", ")", ",", "df", ".", "sql_ctx", ")" ]
Marks a DataFrame as small enough for use in broadcast joins.
[ "Marks", "a", "DataFrame", "as", "small", "enough", "for", "use", "in", "broadcast", "joins", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L333-L337
apache/spark
python/pyspark/sql/functions.py
countDistinct
def countDistinct(col, *cols): """Returns a new :class:`Column` for distinct count of ``col`` or ``cols``. >>> df.agg(countDistinct(df.age, df.name).alias('c')).collect() [Row(c=2)] >>> df.agg(countDistinct("age", "name").alias('c')).collect() [Row(c=2)] """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.countDistinct(_to_java_column(col), _to_seq(sc, cols, _to_java_column)) return Column(jc)
python
def countDistinct(col, *cols): """Returns a new :class:`Column` for distinct count of ``col`` or ``cols``. >>> df.agg(countDistinct(df.age, df.name).alias('c')).collect() [Row(c=2)] >>> df.agg(countDistinct("age", "name").alias('c')).collect() [Row(c=2)] """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.countDistinct(_to_java_column(col), _to_seq(sc, cols, _to_java_column)) return Column(jc)
[ "def", "countDistinct", "(", "col", ",", "*", "cols", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "countDistinct", "(", "_to_java_column", "(", "col", ")", ",", "_to_seq", "(", "sc", ",", "cols", ",", "_to_java_column", ")", ")", "return", "Column", "(", "jc", ")" ]
Returns a new :class:`Column` for distinct count of ``col`` or ``cols``. >>> df.agg(countDistinct(df.age, df.name).alias('c')).collect() [Row(c=2)] >>> df.agg(countDistinct("age", "name").alias('c')).collect() [Row(c=2)]
[ "Returns", "a", "new", ":", "class", ":", "Column", "for", "distinct", "count", "of", "col", "or", "cols", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L421-L432
apache/spark
python/pyspark/sql/functions.py
last
def last(col, ignorenulls=False): """Aggregate function: returns the last value in a group. The function by default returns the last values it sees. It will return the last non-null value it sees when ignoreNulls is set to true. If all values are null, then null is returned. .. note:: The function is non-deterministic because its results depends on order of rows which may be non-deterministic after a shuffle. """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.last(_to_java_column(col), ignorenulls) return Column(jc)
python
def last(col, ignorenulls=False): """Aggregate function: returns the last value in a group. The function by default returns the last values it sees. It will return the last non-null value it sees when ignoreNulls is set to true. If all values are null, then null is returned. .. note:: The function is non-deterministic because its results depends on order of rows which may be non-deterministic after a shuffle. """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.last(_to_java_column(col), ignorenulls) return Column(jc)
[ "def", "last", "(", "col", ",", "ignorenulls", "=", "False", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "last", "(", "_to_java_column", "(", "col", ")", ",", "ignorenulls", ")", "return", "Column", "(", "jc", ")" ]
Aggregate function: returns the last value in a group. The function by default returns the last values it sees. It will return the last non-null value it sees when ignoreNulls is set to true. If all values are null, then null is returned. .. note:: The function is non-deterministic because its results depends on order of rows which may be non-deterministic after a shuffle.
[ "Aggregate", "function", ":", "returns", "the", "last", "value", "in", "a", "group", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L527-L538
apache/spark
python/pyspark/sql/functions.py
nanvl
def nanvl(col1, col2): """Returns col1 if it is not NaN, or col2 if col1 is NaN. Both inputs should be floating point columns (:class:`DoubleType` or :class:`FloatType`). >>> df = spark.createDataFrame([(1.0, float('nan')), (float('nan'), 2.0)], ("a", "b")) >>> df.select(nanvl("a", "b").alias("r1"), nanvl(df.a, df.b).alias("r2")).collect() [Row(r1=1.0, r2=1.0), Row(r1=2.0, r2=2.0)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.nanvl(_to_java_column(col1), _to_java_column(col2)))
python
def nanvl(col1, col2): """Returns col1 if it is not NaN, or col2 if col1 is NaN. Both inputs should be floating point columns (:class:`DoubleType` or :class:`FloatType`). >>> df = spark.createDataFrame([(1.0, float('nan')), (float('nan'), 2.0)], ("a", "b")) >>> df.select(nanvl("a", "b").alias("r1"), nanvl(df.a, df.b).alias("r2")).collect() [Row(r1=1.0, r2=1.0), Row(r1=2.0, r2=2.0)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.nanvl(_to_java_column(col1), _to_java_column(col2)))
[ "def", "nanvl", "(", "col1", ",", "col2", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "nanvl", "(", "_to_java_column", "(", "col1", ")", ",", "_to_java_column", "(", "col2", ")", ")", ")" ]
Returns col1 if it is not NaN, or col2 if col1 is NaN. Both inputs should be floating point columns (:class:`DoubleType` or :class:`FloatType`). >>> df = spark.createDataFrame([(1.0, float('nan')), (float('nan'), 2.0)], ("a", "b")) >>> df.select(nanvl("a", "b").alias("r1"), nanvl(df.a, df.b).alias("r2")).collect() [Row(r1=1.0, r2=1.0), Row(r1=2.0, r2=2.0)]
[ "Returns", "col1", "if", "it", "is", "not", "NaN", "or", "col2", "if", "col1", "is", "NaN", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L565-L575
apache/spark
python/pyspark/sql/functions.py
rand
def rand(seed=None): """Generates a random column with independent and identically distributed (i.i.d.) samples from U[0.0, 1.0]. .. note:: The function is non-deterministic in general case. >>> df.withColumn('rand', rand(seed=42) * 3).collect() [Row(age=2, name=u'Alice', rand=2.4052597283576684), Row(age=5, name=u'Bob', rand=2.3913904055683974)] """ sc = SparkContext._active_spark_context if seed is not None: jc = sc._jvm.functions.rand(seed) else: jc = sc._jvm.functions.rand() return Column(jc)
python
def rand(seed=None): """Generates a random column with independent and identically distributed (i.i.d.) samples from U[0.0, 1.0]. .. note:: The function is non-deterministic in general case. >>> df.withColumn('rand', rand(seed=42) * 3).collect() [Row(age=2, name=u'Alice', rand=2.4052597283576684), Row(age=5, name=u'Bob', rand=2.3913904055683974)] """ sc = SparkContext._active_spark_context if seed is not None: jc = sc._jvm.functions.rand(seed) else: jc = sc._jvm.functions.rand() return Column(jc)
[ "def", "rand", "(", "seed", "=", "None", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "if", "seed", "is", "not", "None", ":", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "rand", "(", "seed", ")", "else", ":", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "rand", "(", ")", "return", "Column", "(", "jc", ")" ]
Generates a random column with independent and identically distributed (i.i.d.) samples from U[0.0, 1.0]. .. note:: The function is non-deterministic in general case. >>> df.withColumn('rand', rand(seed=42) * 3).collect() [Row(age=2, name=u'Alice', rand=2.4052597283576684), Row(age=5, name=u'Bob', rand=2.3913904055683974)]
[ "Generates", "a", "random", "column", "with", "independent", "and", "identically", "distributed", "(", "i", ".", "i", ".", "d", ".", ")", "samples", "from", "U", "[", "0", ".", "0", "1", ".", "0", "]", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L580-L595
apache/spark
python/pyspark/sql/functions.py
round
def round(col, scale=0): """ Round the given value to `scale` decimal places using HALF_UP rounding mode if `scale` >= 0 or at integral part when `scale` < 0. >>> spark.createDataFrame([(2.5,)], ['a']).select(round('a', 0).alias('r')).collect() [Row(r=3.0)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.round(_to_java_column(col), scale))
python
def round(col, scale=0): """ Round the given value to `scale` decimal places using HALF_UP rounding mode if `scale` >= 0 or at integral part when `scale` < 0. >>> spark.createDataFrame([(2.5,)], ['a']).select(round('a', 0).alias('r')).collect() [Row(r=3.0)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.round(_to_java_column(col), scale))
[ "def", "round", "(", "col", ",", "scale", "=", "0", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "round", "(", "_to_java_column", "(", "col", ")", ",", "scale", ")", ")" ]
Round the given value to `scale` decimal places using HALF_UP rounding mode if `scale` >= 0 or at integral part when `scale` < 0. >>> spark.createDataFrame([(2.5,)], ['a']).select(round('a', 0).alias('r')).collect() [Row(r=3.0)]
[ "Round", "the", "given", "value", "to", "scale", "decimal", "places", "using", "HALF_UP", "rounding", "mode", "if", "scale", ">", "=", "0", "or", "at", "integral", "part", "when", "scale", "<", "0", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L619-L628
apache/spark
python/pyspark/sql/functions.py
shiftLeft
def shiftLeft(col, numBits): """Shift the given value numBits left. >>> spark.createDataFrame([(21,)], ['a']).select(shiftLeft('a', 1).alias('r')).collect() [Row(r=42)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.shiftLeft(_to_java_column(col), numBits))
python
def shiftLeft(col, numBits): """Shift the given value numBits left. >>> spark.createDataFrame([(21,)], ['a']).select(shiftLeft('a', 1).alias('r')).collect() [Row(r=42)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.shiftLeft(_to_java_column(col), numBits))
[ "def", "shiftLeft", "(", "col", ",", "numBits", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "shiftLeft", "(", "_to_java_column", "(", "col", ")", ",", "numBits", ")", ")" ]
Shift the given value numBits left. >>> spark.createDataFrame([(21,)], ['a']).select(shiftLeft('a', 1).alias('r')).collect() [Row(r=42)]
[ "Shift", "the", "given", "value", "numBits", "left", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L645-L652
apache/spark
python/pyspark/sql/functions.py
shiftRight
def shiftRight(col, numBits): """(Signed) shift the given value numBits right. >>> spark.createDataFrame([(42,)], ['a']).select(shiftRight('a', 1).alias('r')).collect() [Row(r=21)] """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.shiftRight(_to_java_column(col), numBits) return Column(jc)
python
def shiftRight(col, numBits): """(Signed) shift the given value numBits right. >>> spark.createDataFrame([(42,)], ['a']).select(shiftRight('a', 1).alias('r')).collect() [Row(r=21)] """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.shiftRight(_to_java_column(col), numBits) return Column(jc)
[ "def", "shiftRight", "(", "col", ",", "numBits", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "shiftRight", "(", "_to_java_column", "(", "col", ")", ",", "numBits", ")", "return", "Column", "(", "jc", ")" ]
(Signed) shift the given value numBits right. >>> spark.createDataFrame([(42,)], ['a']).select(shiftRight('a', 1).alias('r')).collect() [Row(r=21)]
[ "(", "Signed", ")", "shift", "the", "given", "value", "numBits", "right", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L656-L664
apache/spark
python/pyspark/sql/functions.py
expr
def expr(str): """Parses the expression string into the column that it represents >>> df.select(expr("length(name)")).collect() [Row(length(name)=5), Row(length(name)=3)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.expr(str))
python
def expr(str): """Parses the expression string into the column that it represents >>> df.select(expr("length(name)")).collect() [Row(length(name)=5), Row(length(name)=3)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.expr(str))
[ "def", "expr", "(", "str", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "expr", "(", "str", ")", ")" ]
Parses the expression string into the column that it represents >>> df.select(expr("length(name)")).collect() [Row(length(name)=5), Row(length(name)=3)]
[ "Parses", "the", "expression", "string", "into", "the", "column", "that", "it", "represents" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L694-L701
apache/spark
python/pyspark/sql/functions.py
when
def when(condition, value): """Evaluates a list of conditions and returns one of multiple possible result expressions. If :func:`Column.otherwise` is not invoked, None is returned for unmatched conditions. :param condition: a boolean :class:`Column` expression. :param value: a literal value, or a :class:`Column` expression. >>> df.select(when(df['age'] == 2, 3).otherwise(4).alias("age")).collect() [Row(age=3), Row(age=4)] >>> df.select(when(df.age == 2, df.age + 1).alias("age")).collect() [Row(age=3), Row(age=None)] """ sc = SparkContext._active_spark_context if not isinstance(condition, Column): raise TypeError("condition should be a Column") v = value._jc if isinstance(value, Column) else value jc = sc._jvm.functions.when(condition._jc, v) return Column(jc)
python
def when(condition, value): """Evaluates a list of conditions and returns one of multiple possible result expressions. If :func:`Column.otherwise` is not invoked, None is returned for unmatched conditions. :param condition: a boolean :class:`Column` expression. :param value: a literal value, or a :class:`Column` expression. >>> df.select(when(df['age'] == 2, 3).otherwise(4).alias("age")).collect() [Row(age=3), Row(age=4)] >>> df.select(when(df.age == 2, df.age + 1).alias("age")).collect() [Row(age=3), Row(age=None)] """ sc = SparkContext._active_spark_context if not isinstance(condition, Column): raise TypeError("condition should be a Column") v = value._jc if isinstance(value, Column) else value jc = sc._jvm.functions.when(condition._jc, v) return Column(jc)
[ "def", "when", "(", "condition", ",", "value", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "if", "not", "isinstance", "(", "condition", ",", "Column", ")", ":", "raise", "TypeError", "(", "\"condition should be a Column\"", ")", "v", "=", "value", ".", "_jc", "if", "isinstance", "(", "value", ",", "Column", ")", "else", "value", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "when", "(", "condition", ".", "_jc", ",", "v", ")", "return", "Column", "(", "jc", ")" ]
Evaluates a list of conditions and returns one of multiple possible result expressions. If :func:`Column.otherwise` is not invoked, None is returned for unmatched conditions. :param condition: a boolean :class:`Column` expression. :param value: a literal value, or a :class:`Column` expression. >>> df.select(when(df['age'] == 2, 3).otherwise(4).alias("age")).collect() [Row(age=3), Row(age=4)] >>> df.select(when(df.age == 2, df.age + 1).alias("age")).collect() [Row(age=3), Row(age=None)]
[ "Evaluates", "a", "list", "of", "conditions", "and", "returns", "one", "of", "multiple", "possible", "result", "expressions", ".", "If", ":", "func", ":", "Column", ".", "otherwise", "is", "not", "invoked", "None", "is", "returned", "for", "unmatched", "conditions", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L756-L774
apache/spark
python/pyspark/sql/functions.py
log
def log(arg1, arg2=None): """Returns the first argument-based logarithm of the second argument. If there is only one argument, then this takes the natural logarithm of the argument. >>> df.select(log(10.0, df.age).alias('ten')).rdd.map(lambda l: str(l.ten)[:7]).collect() ['0.30102', '0.69897'] >>> df.select(log(df.age).alias('e')).rdd.map(lambda l: str(l.e)[:7]).collect() ['0.69314', '1.60943'] """ sc = SparkContext._active_spark_context if arg2 is None: jc = sc._jvm.functions.log(_to_java_column(arg1)) else: jc = sc._jvm.functions.log(arg1, _to_java_column(arg2)) return Column(jc)
python
def log(arg1, arg2=None): """Returns the first argument-based logarithm of the second argument. If there is only one argument, then this takes the natural logarithm of the argument. >>> df.select(log(10.0, df.age).alias('ten')).rdd.map(lambda l: str(l.ten)[:7]).collect() ['0.30102', '0.69897'] >>> df.select(log(df.age).alias('e')).rdd.map(lambda l: str(l.e)[:7]).collect() ['0.69314', '1.60943'] """ sc = SparkContext._active_spark_context if arg2 is None: jc = sc._jvm.functions.log(_to_java_column(arg1)) else: jc = sc._jvm.functions.log(arg1, _to_java_column(arg2)) return Column(jc)
[ "def", "log", "(", "arg1", ",", "arg2", "=", "None", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "if", "arg2", "is", "None", ":", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "log", "(", "_to_java_column", "(", "arg1", ")", ")", "else", ":", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "log", "(", "arg1", ",", "_to_java_column", "(", "arg2", ")", ")", "return", "Column", "(", "jc", ")" ]
Returns the first argument-based logarithm of the second argument. If there is only one argument, then this takes the natural logarithm of the argument. >>> df.select(log(10.0, df.age).alias('ten')).rdd.map(lambda l: str(l.ten)[:7]).collect() ['0.30102', '0.69897'] >>> df.select(log(df.age).alias('e')).rdd.map(lambda l: str(l.e)[:7]).collect() ['0.69314', '1.60943']
[ "Returns", "the", "first", "argument", "-", "based", "logarithm", "of", "the", "second", "argument", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L778-L794
apache/spark
python/pyspark/sql/functions.py
conv
def conv(col, fromBase, toBase): """ Convert a number in a string column from one base to another. >>> df = spark.createDataFrame([("010101",)], ['n']) >>> df.select(conv(df.n, 2, 16).alias('hex')).collect() [Row(hex=u'15')] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.conv(_to_java_column(col), fromBase, toBase))
python
def conv(col, fromBase, toBase): """ Convert a number in a string column from one base to another. >>> df = spark.createDataFrame([("010101",)], ['n']) >>> df.select(conv(df.n, 2, 16).alias('hex')).collect() [Row(hex=u'15')] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.conv(_to_java_column(col), fromBase, toBase))
[ "def", "conv", "(", "col", ",", "fromBase", ",", "toBase", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "conv", "(", "_to_java_column", "(", "col", ")", ",", "fromBase", ",", "toBase", ")", ")" ]
Convert a number in a string column from one base to another. >>> df = spark.createDataFrame([("010101",)], ['n']) >>> df.select(conv(df.n, 2, 16).alias('hex')).collect() [Row(hex=u'15')]
[ "Convert", "a", "number", "in", "a", "string", "column", "from", "one", "base", "to", "another", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L810-L819
apache/spark
python/pyspark/sql/functions.py
lag
def lag(col, offset=1, default=None): """ Window function: returns the value that is `offset` rows before the current row, and `defaultValue` if there is less than `offset` rows before the current row. For example, an `offset` of one will return the previous row at any given point in the window partition. This is equivalent to the LAG function in SQL. :param col: name of column or expression :param offset: number of row to extend :param default: default value """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.lag(_to_java_column(col), offset, default))
python
def lag(col, offset=1, default=None): """ Window function: returns the value that is `offset` rows before the current row, and `defaultValue` if there is less than `offset` rows before the current row. For example, an `offset` of one will return the previous row at any given point in the window partition. This is equivalent to the LAG function in SQL. :param col: name of column or expression :param offset: number of row to extend :param default: default value """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.lag(_to_java_column(col), offset, default))
[ "def", "lag", "(", "col", ",", "offset", "=", "1", ",", "default", "=", "None", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "lag", "(", "_to_java_column", "(", "col", ")", ",", "offset", ",", "default", ")", ")" ]
Window function: returns the value that is `offset` rows before the current row, and `defaultValue` if there is less than `offset` rows before the current row. For example, an `offset` of one will return the previous row at any given point in the window partition. This is equivalent to the LAG function in SQL. :param col: name of column or expression :param offset: number of row to extend :param default: default value
[ "Window", "function", ":", "returns", "the", "value", "that", "is", "offset", "rows", "before", "the", "current", "row", "and", "defaultValue", "if", "there", "is", "less", "than", "offset", "rows", "before", "the", "current", "row", ".", "For", "example", "an", "offset", "of", "one", "will", "return", "the", "previous", "row", "at", "any", "given", "point", "in", "the", "window", "partition", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L838-L851
apache/spark
python/pyspark/sql/functions.py
ntile
def ntile(n): """ Window function: returns the ntile group id (from 1 to `n` inclusive) in an ordered window partition. For example, if `n` is 4, the first quarter of the rows will get value 1, the second quarter will get 2, the third quarter will get 3, and the last quarter will get 4. This is equivalent to the NTILE function in SQL. :param n: an integer """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.ntile(int(n)))
python
def ntile(n): """ Window function: returns the ntile group id (from 1 to `n` inclusive) in an ordered window partition. For example, if `n` is 4, the first quarter of the rows will get value 1, the second quarter will get 2, the third quarter will get 3, and the last quarter will get 4. This is equivalent to the NTILE function in SQL. :param n: an integer """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.ntile(int(n)))
[ "def", "ntile", "(", "n", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "ntile", "(", "int", "(", "n", ")", ")", ")" ]
Window function: returns the ntile group id (from 1 to `n` inclusive) in an ordered window partition. For example, if `n` is 4, the first quarter of the rows will get value 1, the second quarter will get 2, the third quarter will get 3, and the last quarter will get 4. This is equivalent to the NTILE function in SQL. :param n: an integer
[ "Window", "function", ":", "returns", "the", "ntile", "group", "id", "(", "from", "1", "to", "n", "inclusive", ")", "in", "an", "ordered", "window", "partition", ".", "For", "example", "if", "n", "is", "4", "the", "first", "quarter", "of", "the", "rows", "will", "get", "value", "1", "the", "second", "quarter", "will", "get", "2", "the", "third", "quarter", "will", "get", "3", "and", "the", "last", "quarter", "will", "get", "4", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L872-L884
apache/spark
python/pyspark/sql/functions.py
date_format
def date_format(date, format): """ Converts a date/timestamp/string to a value of string in the format specified by the date format given by the second argument. A pattern could be for instance `dd.MM.yyyy` and could return a string like '18.03.1993'. All pattern letters of the Java class `java.time.format.DateTimeFormatter` can be used. .. note:: Use when ever possible specialized functions like `year`. These benefit from a specialized implementation. >>> df = spark.createDataFrame([('2015-04-08',)], ['dt']) >>> df.select(date_format('dt', 'MM/dd/yyy').alias('date')).collect() [Row(date=u'04/08/2015')] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.date_format(_to_java_column(date), format))
python
def date_format(date, format): """ Converts a date/timestamp/string to a value of string in the format specified by the date format given by the second argument. A pattern could be for instance `dd.MM.yyyy` and could return a string like '18.03.1993'. All pattern letters of the Java class `java.time.format.DateTimeFormatter` can be used. .. note:: Use when ever possible specialized functions like `year`. These benefit from a specialized implementation. >>> df = spark.createDataFrame([('2015-04-08',)], ['dt']) >>> df.select(date_format('dt', 'MM/dd/yyy').alias('date')).collect() [Row(date=u'04/08/2015')] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.date_format(_to_java_column(date), format))
[ "def", "date_format", "(", "date", ",", "format", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "date_format", "(", "_to_java_column", "(", "date", ")", ",", "format", ")", ")" ]
Converts a date/timestamp/string to a value of string in the format specified by the date format given by the second argument. A pattern could be for instance `dd.MM.yyyy` and could return a string like '18.03.1993'. All pattern letters of the Java class `java.time.format.DateTimeFormatter` can be used. .. note:: Use when ever possible specialized functions like `year`. These benefit from a specialized implementation. >>> df = spark.createDataFrame([('2015-04-08',)], ['dt']) >>> df.select(date_format('dt', 'MM/dd/yyy').alias('date')).collect() [Row(date=u'04/08/2015')]
[ "Converts", "a", "date", "/", "timestamp", "/", "string", "to", "a", "value", "of", "string", "in", "the", "format", "specified", "by", "the", "date", "format", "given", "by", "the", "second", "argument", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L908-L924
apache/spark
python/pyspark/sql/functions.py
date_add
def date_add(start, days): """ Returns the date that is `days` days after `start` >>> df = spark.createDataFrame([('2015-04-08',)], ['dt']) >>> df.select(date_add(df.dt, 1).alias('next_date')).collect() [Row(next_date=datetime.date(2015, 4, 9))] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.date_add(_to_java_column(start), days))
python
def date_add(start, days): """ Returns the date that is `days` days after `start` >>> df = spark.createDataFrame([('2015-04-08',)], ['dt']) >>> df.select(date_add(df.dt, 1).alias('next_date')).collect() [Row(next_date=datetime.date(2015, 4, 9))] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.date_add(_to_java_column(start), days))
[ "def", "date_add", "(", "start", ",", "days", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "date_add", "(", "_to_java_column", "(", "start", ")", ",", "days", ")", ")" ]
Returns the date that is `days` days after `start` >>> df = spark.createDataFrame([('2015-04-08',)], ['dt']) >>> df.select(date_add(df.dt, 1).alias('next_date')).collect() [Row(next_date=datetime.date(2015, 4, 9))]
[ "Returns", "the", "date", "that", "is", "days", "days", "after", "start" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1058-L1067
apache/spark
python/pyspark/sql/functions.py
datediff
def datediff(end, start): """ Returns the number of days from `start` to `end`. >>> df = spark.createDataFrame([('2015-04-08','2015-05-10')], ['d1', 'd2']) >>> df.select(datediff(df.d2, df.d1).alias('diff')).collect() [Row(diff=32)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.datediff(_to_java_column(end), _to_java_column(start)))
python
def datediff(end, start): """ Returns the number of days from `start` to `end`. >>> df = spark.createDataFrame([('2015-04-08','2015-05-10')], ['d1', 'd2']) >>> df.select(datediff(df.d2, df.d1).alias('diff')).collect() [Row(diff=32)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.datediff(_to_java_column(end), _to_java_column(start)))
[ "def", "datediff", "(", "end", ",", "start", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "datediff", "(", "_to_java_column", "(", "end", ")", ",", "_to_java_column", "(", "start", ")", ")", ")" ]
Returns the number of days from `start` to `end`. >>> df = spark.createDataFrame([('2015-04-08','2015-05-10')], ['d1', 'd2']) >>> df.select(datediff(df.d2, df.d1).alias('diff')).collect() [Row(diff=32)]
[ "Returns", "the", "number", "of", "days", "from", "start", "to", "end", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1084-L1093
apache/spark
python/pyspark/sql/functions.py
add_months
def add_months(start, months): """ Returns the date that is `months` months after `start` >>> df = spark.createDataFrame([('2015-04-08',)], ['dt']) >>> df.select(add_months(df.dt, 1).alias('next_month')).collect() [Row(next_month=datetime.date(2015, 5, 8))] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.add_months(_to_java_column(start), months))
python
def add_months(start, months): """ Returns the date that is `months` months after `start` >>> df = spark.createDataFrame([('2015-04-08',)], ['dt']) >>> df.select(add_months(df.dt, 1).alias('next_month')).collect() [Row(next_month=datetime.date(2015, 5, 8))] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.add_months(_to_java_column(start), months))
[ "def", "add_months", "(", "start", ",", "months", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "add_months", "(", "_to_java_column", "(", "start", ")", ",", "months", ")", ")" ]
Returns the date that is `months` months after `start` >>> df = spark.createDataFrame([('2015-04-08',)], ['dt']) >>> df.select(add_months(df.dt, 1).alias('next_month')).collect() [Row(next_month=datetime.date(2015, 5, 8))]
[ "Returns", "the", "date", "that", "is", "months", "months", "after", "start" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1097-L1106
apache/spark
python/pyspark/sql/functions.py
months_between
def months_between(date1, date2, roundOff=True): """ Returns number of months between dates date1 and date2. If date1 is later than date2, then the result is positive. If date1 and date2 are on the same day of month, or both are the last day of month, returns an integer (time of day will be ignored). The result is rounded off to 8 digits unless `roundOff` is set to `False`. >>> df = spark.createDataFrame([('1997-02-28 10:30:00', '1996-10-30')], ['date1', 'date2']) >>> df.select(months_between(df.date1, df.date2).alias('months')).collect() [Row(months=3.94959677)] >>> df.select(months_between(df.date1, df.date2, False).alias('months')).collect() [Row(months=3.9495967741935485)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.months_between( _to_java_column(date1), _to_java_column(date2), roundOff))
python
def months_between(date1, date2, roundOff=True): """ Returns number of months between dates date1 and date2. If date1 is later than date2, then the result is positive. If date1 and date2 are on the same day of month, or both are the last day of month, returns an integer (time of day will be ignored). The result is rounded off to 8 digits unless `roundOff` is set to `False`. >>> df = spark.createDataFrame([('1997-02-28 10:30:00', '1996-10-30')], ['date1', 'date2']) >>> df.select(months_between(df.date1, df.date2).alias('months')).collect() [Row(months=3.94959677)] >>> df.select(months_between(df.date1, df.date2, False).alias('months')).collect() [Row(months=3.9495967741935485)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.months_between( _to_java_column(date1), _to_java_column(date2), roundOff))
[ "def", "months_between", "(", "date1", ",", "date2", ",", "roundOff", "=", "True", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "months_between", "(", "_to_java_column", "(", "date1", ")", ",", "_to_java_column", "(", "date2", ")", ",", "roundOff", ")", ")" ]
Returns number of months between dates date1 and date2. If date1 is later than date2, then the result is positive. If date1 and date2 are on the same day of month, or both are the last day of month, returns an integer (time of day will be ignored). The result is rounded off to 8 digits unless `roundOff` is set to `False`. >>> df = spark.createDataFrame([('1997-02-28 10:30:00', '1996-10-30')], ['date1', 'date2']) >>> df.select(months_between(df.date1, df.date2).alias('months')).collect() [Row(months=3.94959677)] >>> df.select(months_between(df.date1, df.date2, False).alias('months')).collect() [Row(months=3.9495967741935485)]
[ "Returns", "number", "of", "months", "between", "dates", "date1", "and", "date2", ".", "If", "date1", "is", "later", "than", "date2", "then", "the", "result", "is", "positive", ".", "If", "date1", "and", "date2", "are", "on", "the", "same", "day", "of", "month", "or", "both", "are", "the", "last", "day", "of", "month", "returns", "an", "integer", "(", "time", "of", "day", "will", "be", "ignored", ")", ".", "The", "result", "is", "rounded", "off", "to", "8", "digits", "unless", "roundOff", "is", "set", "to", "False", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1110-L1126
apache/spark
python/pyspark/sql/functions.py
to_date
def to_date(col, format=None): """Converts a :class:`Column` of :class:`pyspark.sql.types.StringType` or :class:`pyspark.sql.types.TimestampType` into :class:`pyspark.sql.types.DateType` using the optionally specified format. Specify formats according to `DateTimeFormatter <https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html>`_. # noqa By default, it follows casting rules to :class:`pyspark.sql.types.DateType` if the format is omitted (equivalent to ``col.cast("date")``). >>> df = spark.createDataFrame([('1997-02-28 10:30:00',)], ['t']) >>> df.select(to_date(df.t).alias('date')).collect() [Row(date=datetime.date(1997, 2, 28))] >>> df = spark.createDataFrame([('1997-02-28 10:30:00',)], ['t']) >>> df.select(to_date(df.t, 'yyyy-MM-dd HH:mm:ss').alias('date')).collect() [Row(date=datetime.date(1997, 2, 28))] """ sc = SparkContext._active_spark_context if format is None: jc = sc._jvm.functions.to_date(_to_java_column(col)) else: jc = sc._jvm.functions.to_date(_to_java_column(col), format) return Column(jc)
python
def to_date(col, format=None): """Converts a :class:`Column` of :class:`pyspark.sql.types.StringType` or :class:`pyspark.sql.types.TimestampType` into :class:`pyspark.sql.types.DateType` using the optionally specified format. Specify formats according to `DateTimeFormatter <https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html>`_. # noqa By default, it follows casting rules to :class:`pyspark.sql.types.DateType` if the format is omitted (equivalent to ``col.cast("date")``). >>> df = spark.createDataFrame([('1997-02-28 10:30:00',)], ['t']) >>> df.select(to_date(df.t).alias('date')).collect() [Row(date=datetime.date(1997, 2, 28))] >>> df = spark.createDataFrame([('1997-02-28 10:30:00',)], ['t']) >>> df.select(to_date(df.t, 'yyyy-MM-dd HH:mm:ss').alias('date')).collect() [Row(date=datetime.date(1997, 2, 28))] """ sc = SparkContext._active_spark_context if format is None: jc = sc._jvm.functions.to_date(_to_java_column(col)) else: jc = sc._jvm.functions.to_date(_to_java_column(col), format) return Column(jc)
[ "def", "to_date", "(", "col", ",", "format", "=", "None", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "if", "format", "is", "None", ":", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "to_date", "(", "_to_java_column", "(", "col", ")", ")", "else", ":", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "to_date", "(", "_to_java_column", "(", "col", ")", ",", "format", ")", "return", "Column", "(", "jc", ")" ]
Converts a :class:`Column` of :class:`pyspark.sql.types.StringType` or :class:`pyspark.sql.types.TimestampType` into :class:`pyspark.sql.types.DateType` using the optionally specified format. Specify formats according to `DateTimeFormatter <https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html>`_. # noqa By default, it follows casting rules to :class:`pyspark.sql.types.DateType` if the format is omitted (equivalent to ``col.cast("date")``). >>> df = spark.createDataFrame([('1997-02-28 10:30:00',)], ['t']) >>> df.select(to_date(df.t).alias('date')).collect() [Row(date=datetime.date(1997, 2, 28))] >>> df = spark.createDataFrame([('1997-02-28 10:30:00',)], ['t']) >>> df.select(to_date(df.t, 'yyyy-MM-dd HH:mm:ss').alias('date')).collect() [Row(date=datetime.date(1997, 2, 28))]
[ "Converts", "a", ":", "class", ":", "Column", "of", ":", "class", ":", "pyspark", ".", "sql", ".", "types", ".", "StringType", "or", ":", "class", ":", "pyspark", ".", "sql", ".", "types", ".", "TimestampType", "into", ":", "class", ":", "pyspark", ".", "sql", ".", "types", ".", "DateType", "using", "the", "optionally", "specified", "format", ".", "Specify", "formats", "according", "to", "DateTimeFormatter", "<https", ":", "//", "docs", ".", "oracle", ".", "com", "/", "javase", "/", "8", "/", "docs", "/", "api", "/", "java", "/", "time", "/", "format", "/", "DateTimeFormatter", ".", "html", ">", "_", ".", "#", "noqa", "By", "default", "it", "follows", "casting", "rules", "to", ":", "class", ":", "pyspark", ".", "sql", ".", "types", ".", "DateType", "if", "the", "format", "is", "omitted", "(", "equivalent", "to", "col", ".", "cast", "(", "date", ")", ")", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1130-L1151
apache/spark
python/pyspark/sql/functions.py
date_trunc
def date_trunc(format, timestamp): """ Returns timestamp truncated to the unit specified by the format. :param format: 'year', 'yyyy', 'yy', 'month', 'mon', 'mm', 'day', 'dd', 'hour', 'minute', 'second', 'week', 'quarter' >>> df = spark.createDataFrame([('1997-02-28 05:02:11',)], ['t']) >>> df.select(date_trunc('year', df.t).alias('year')).collect() [Row(year=datetime.datetime(1997, 1, 1, 0, 0))] >>> df.select(date_trunc('mon', df.t).alias('month')).collect() [Row(month=datetime.datetime(1997, 2, 1, 0, 0))] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.date_trunc(format, _to_java_column(timestamp)))
python
def date_trunc(format, timestamp): """ Returns timestamp truncated to the unit specified by the format. :param format: 'year', 'yyyy', 'yy', 'month', 'mon', 'mm', 'day', 'dd', 'hour', 'minute', 'second', 'week', 'quarter' >>> df = spark.createDataFrame([('1997-02-28 05:02:11',)], ['t']) >>> df.select(date_trunc('year', df.t).alias('year')).collect() [Row(year=datetime.datetime(1997, 1, 1, 0, 0))] >>> df.select(date_trunc('mon', df.t).alias('month')).collect() [Row(month=datetime.datetime(1997, 2, 1, 0, 0))] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.date_trunc(format, _to_java_column(timestamp)))
[ "def", "date_trunc", "(", "format", ",", "timestamp", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "date_trunc", "(", "format", ",", "_to_java_column", "(", "timestamp", ")", ")", ")" ]
Returns timestamp truncated to the unit specified by the format. :param format: 'year', 'yyyy', 'yy', 'month', 'mon', 'mm', 'day', 'dd', 'hour', 'minute', 'second', 'week', 'quarter' >>> df = spark.createDataFrame([('1997-02-28 05:02:11',)], ['t']) >>> df.select(date_trunc('year', df.t).alias('year')).collect() [Row(year=datetime.datetime(1997, 1, 1, 0, 0))] >>> df.select(date_trunc('mon', df.t).alias('month')).collect() [Row(month=datetime.datetime(1997, 2, 1, 0, 0))]
[ "Returns", "timestamp", "truncated", "to", "the", "unit", "specified", "by", "the", "format", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1197-L1211
apache/spark
python/pyspark/sql/functions.py
next_day
def next_day(date, dayOfWeek): """ Returns the first date which is later than the value of the date column. Day of the week parameter is case insensitive, and accepts: "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun". >>> df = spark.createDataFrame([('2015-07-27',)], ['d']) >>> df.select(next_day(df.d, 'Sun').alias('date')).collect() [Row(date=datetime.date(2015, 8, 2))] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.next_day(_to_java_column(date), dayOfWeek))
python
def next_day(date, dayOfWeek): """ Returns the first date which is later than the value of the date column. Day of the week parameter is case insensitive, and accepts: "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun". >>> df = spark.createDataFrame([('2015-07-27',)], ['d']) >>> df.select(next_day(df.d, 'Sun').alias('date')).collect() [Row(date=datetime.date(2015, 8, 2))] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.next_day(_to_java_column(date), dayOfWeek))
[ "def", "next_day", "(", "date", ",", "dayOfWeek", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "next_day", "(", "_to_java_column", "(", "date", ")", ",", "dayOfWeek", ")", ")" ]
Returns the first date which is later than the value of the date column. Day of the week parameter is case insensitive, and accepts: "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun". >>> df = spark.createDataFrame([('2015-07-27',)], ['d']) >>> df.select(next_day(df.d, 'Sun').alias('date')).collect() [Row(date=datetime.date(2015, 8, 2))]
[ "Returns", "the", "first", "date", "which", "is", "later", "than", "the", "value", "of", "the", "date", "column", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1215-L1227
apache/spark
python/pyspark/sql/functions.py
last_day
def last_day(date): """ Returns the last day of the month which the given date belongs to. >>> df = spark.createDataFrame([('1997-02-10',)], ['d']) >>> df.select(last_day(df.d).alias('date')).collect() [Row(date=datetime.date(1997, 2, 28))] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.last_day(_to_java_column(date)))
python
def last_day(date): """ Returns the last day of the month which the given date belongs to. >>> df = spark.createDataFrame([('1997-02-10',)], ['d']) >>> df.select(last_day(df.d).alias('date')).collect() [Row(date=datetime.date(1997, 2, 28))] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.last_day(_to_java_column(date)))
[ "def", "last_day", "(", "date", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "last_day", "(", "_to_java_column", "(", "date", ")", ")", ")" ]
Returns the last day of the month which the given date belongs to. >>> df = spark.createDataFrame([('1997-02-10',)], ['d']) >>> df.select(last_day(df.d).alias('date')).collect() [Row(date=datetime.date(1997, 2, 28))]
[ "Returns", "the", "last", "day", "of", "the", "month", "which", "the", "given", "date", "belongs", "to", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1231-L1240
apache/spark
python/pyspark/sql/functions.py
unix_timestamp
def unix_timestamp(timestamp=None, format='yyyy-MM-dd HH:mm:ss'): """ Convert time string with given pattern ('yyyy-MM-dd HH:mm:ss', by default) to Unix time stamp (in seconds), using the default timezone and the default locale, return null if fail. if `timestamp` is None, then it returns current timestamp. >>> spark.conf.set("spark.sql.session.timeZone", "America/Los_Angeles") >>> time_df = spark.createDataFrame([('2015-04-08',)], ['dt']) >>> time_df.select(unix_timestamp('dt', 'yyyy-MM-dd').alias('unix_time')).collect() [Row(unix_time=1428476400)] >>> spark.conf.unset("spark.sql.session.timeZone") """ sc = SparkContext._active_spark_context if timestamp is None: return Column(sc._jvm.functions.unix_timestamp()) return Column(sc._jvm.functions.unix_timestamp(_to_java_column(timestamp), format))
python
def unix_timestamp(timestamp=None, format='yyyy-MM-dd HH:mm:ss'): """ Convert time string with given pattern ('yyyy-MM-dd HH:mm:ss', by default) to Unix time stamp (in seconds), using the default timezone and the default locale, return null if fail. if `timestamp` is None, then it returns current timestamp. >>> spark.conf.set("spark.sql.session.timeZone", "America/Los_Angeles") >>> time_df = spark.createDataFrame([('2015-04-08',)], ['dt']) >>> time_df.select(unix_timestamp('dt', 'yyyy-MM-dd').alias('unix_time')).collect() [Row(unix_time=1428476400)] >>> spark.conf.unset("spark.sql.session.timeZone") """ sc = SparkContext._active_spark_context if timestamp is None: return Column(sc._jvm.functions.unix_timestamp()) return Column(sc._jvm.functions.unix_timestamp(_to_java_column(timestamp), format))
[ "def", "unix_timestamp", "(", "timestamp", "=", "None", ",", "format", "=", "'yyyy-MM-dd HH:mm:ss'", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "if", "timestamp", "is", "None", ":", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "unix_timestamp", "(", ")", ")", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "unix_timestamp", "(", "_to_java_column", "(", "timestamp", ")", ",", "format", ")", ")" ]
Convert time string with given pattern ('yyyy-MM-dd HH:mm:ss', by default) to Unix time stamp (in seconds), using the default timezone and the default locale, return null if fail. if `timestamp` is None, then it returns current timestamp. >>> spark.conf.set("spark.sql.session.timeZone", "America/Los_Angeles") >>> time_df = spark.createDataFrame([('2015-04-08',)], ['dt']) >>> time_df.select(unix_timestamp('dt', 'yyyy-MM-dd').alias('unix_time')).collect() [Row(unix_time=1428476400)] >>> spark.conf.unset("spark.sql.session.timeZone")
[ "Convert", "time", "string", "with", "given", "pattern", "(", "yyyy", "-", "MM", "-", "dd", "HH", ":", "mm", ":", "ss", "by", "default", ")", "to", "Unix", "time", "stamp", "(", "in", "seconds", ")", "using", "the", "default", "timezone", "and", "the", "default", "locale", "return", "null", "if", "fail", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1262-L1279
apache/spark
python/pyspark/sql/functions.py
from_utc_timestamp
def from_utc_timestamp(timestamp, tz): """ This is a common function for databases supporting TIMESTAMP WITHOUT TIMEZONE. This function takes a timestamp which is timezone-agnostic, and interprets it as a timestamp in UTC, and renders that timestamp as a timestamp in the given time zone. However, timestamp in Spark represents number of microseconds from the Unix epoch, which is not timezone-agnostic. So in Spark this function just shift the timestamp value from UTC timezone to the given timezone. This function may return confusing result if the input is a string with timezone, e.g. '2018-03-13T06:18:23+00:00'. The reason is that, Spark firstly cast the string to timestamp according to the timezone in the string, and finally display the result by converting the timestamp to string according to the session local timezone. :param timestamp: the column that contains timestamps :param tz: a string that has the ID of timezone, e.g. "GMT", "America/Los_Angeles", etc .. versionchanged:: 2.4 `tz` can take a :class:`Column` containing timezone ID strings. >>> df = spark.createDataFrame([('1997-02-28 10:30:00', 'JST')], ['ts', 'tz']) >>> df.select(from_utc_timestamp(df.ts, "PST").alias('local_time')).collect() [Row(local_time=datetime.datetime(1997, 2, 28, 2, 30))] >>> df.select(from_utc_timestamp(df.ts, df.tz).alias('local_time')).collect() [Row(local_time=datetime.datetime(1997, 2, 28, 19, 30))] .. note:: Deprecated in 3.0. See SPARK-25496 """ warnings.warn("Deprecated in 3.0. See SPARK-25496", DeprecationWarning) sc = SparkContext._active_spark_context if isinstance(tz, Column): tz = _to_java_column(tz) return Column(sc._jvm.functions.from_utc_timestamp(_to_java_column(timestamp), tz))
python
def from_utc_timestamp(timestamp, tz): """ This is a common function for databases supporting TIMESTAMP WITHOUT TIMEZONE. This function takes a timestamp which is timezone-agnostic, and interprets it as a timestamp in UTC, and renders that timestamp as a timestamp in the given time zone. However, timestamp in Spark represents number of microseconds from the Unix epoch, which is not timezone-agnostic. So in Spark this function just shift the timestamp value from UTC timezone to the given timezone. This function may return confusing result if the input is a string with timezone, e.g. '2018-03-13T06:18:23+00:00'. The reason is that, Spark firstly cast the string to timestamp according to the timezone in the string, and finally display the result by converting the timestamp to string according to the session local timezone. :param timestamp: the column that contains timestamps :param tz: a string that has the ID of timezone, e.g. "GMT", "America/Los_Angeles", etc .. versionchanged:: 2.4 `tz` can take a :class:`Column` containing timezone ID strings. >>> df = spark.createDataFrame([('1997-02-28 10:30:00', 'JST')], ['ts', 'tz']) >>> df.select(from_utc_timestamp(df.ts, "PST").alias('local_time')).collect() [Row(local_time=datetime.datetime(1997, 2, 28, 2, 30))] >>> df.select(from_utc_timestamp(df.ts, df.tz).alias('local_time')).collect() [Row(local_time=datetime.datetime(1997, 2, 28, 19, 30))] .. note:: Deprecated in 3.0. See SPARK-25496 """ warnings.warn("Deprecated in 3.0. See SPARK-25496", DeprecationWarning) sc = SparkContext._active_spark_context if isinstance(tz, Column): tz = _to_java_column(tz) return Column(sc._jvm.functions.from_utc_timestamp(_to_java_column(timestamp), tz))
[ "def", "from_utc_timestamp", "(", "timestamp", ",", "tz", ")", ":", "warnings", ".", "warn", "(", "\"Deprecated in 3.0. See SPARK-25496\"", ",", "DeprecationWarning", ")", "sc", "=", "SparkContext", ".", "_active_spark_context", "if", "isinstance", "(", "tz", ",", "Column", ")", ":", "tz", "=", "_to_java_column", "(", "tz", ")", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "from_utc_timestamp", "(", "_to_java_column", "(", "timestamp", ")", ",", "tz", ")", ")" ]
This is a common function for databases supporting TIMESTAMP WITHOUT TIMEZONE. This function takes a timestamp which is timezone-agnostic, and interprets it as a timestamp in UTC, and renders that timestamp as a timestamp in the given time zone. However, timestamp in Spark represents number of microseconds from the Unix epoch, which is not timezone-agnostic. So in Spark this function just shift the timestamp value from UTC timezone to the given timezone. This function may return confusing result if the input is a string with timezone, e.g. '2018-03-13T06:18:23+00:00'. The reason is that, Spark firstly cast the string to timestamp according to the timezone in the string, and finally display the result by converting the timestamp to string according to the session local timezone. :param timestamp: the column that contains timestamps :param tz: a string that has the ID of timezone, e.g. "GMT", "America/Los_Angeles", etc .. versionchanged:: 2.4 `tz` can take a :class:`Column` containing timezone ID strings. >>> df = spark.createDataFrame([('1997-02-28 10:30:00', 'JST')], ['ts', 'tz']) >>> df.select(from_utc_timestamp(df.ts, "PST").alias('local_time')).collect() [Row(local_time=datetime.datetime(1997, 2, 28, 2, 30))] >>> df.select(from_utc_timestamp(df.ts, df.tz).alias('local_time')).collect() [Row(local_time=datetime.datetime(1997, 2, 28, 19, 30))] .. note:: Deprecated in 3.0. See SPARK-25496
[ "This", "is", "a", "common", "function", "for", "databases", "supporting", "TIMESTAMP", "WITHOUT", "TIMEZONE", ".", "This", "function", "takes", "a", "timestamp", "which", "is", "timezone", "-", "agnostic", "and", "interprets", "it", "as", "a", "timestamp", "in", "UTC", "and", "renders", "that", "timestamp", "as", "a", "timestamp", "in", "the", "given", "time", "zone", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1283-L1316
apache/spark
python/pyspark/sql/functions.py
window
def window(timeColumn, windowDuration, slideDuration=None, startTime=None): """Bucketize rows into one or more time windows given a timestamp specifying column. Window starts are inclusive but the window ends are exclusive, e.g. 12:05 will be in the window [12:05,12:10) but not in [12:00,12:05). Windows can support microsecond precision. Windows in the order of months are not supported. The time column must be of :class:`pyspark.sql.types.TimestampType`. Durations are provided as strings, e.g. '1 second', '1 day 12 hours', '2 minutes'. Valid interval strings are 'week', 'day', 'hour', 'minute', 'second', 'millisecond', 'microsecond'. If the ``slideDuration`` is not provided, the windows will be tumbling windows. The startTime is the offset with respect to 1970-01-01 00:00:00 UTC with which to start window intervals. For example, in order to have hourly tumbling windows that start 15 minutes past the hour, e.g. 12:15-13:15, 13:15-14:15... provide `startTime` as `15 minutes`. The output column will be a struct called 'window' by default with the nested columns 'start' and 'end', where 'start' and 'end' will be of :class:`pyspark.sql.types.TimestampType`. >>> df = spark.createDataFrame([("2016-03-11 09:00:07", 1)]).toDF("date", "val") >>> w = df.groupBy(window("date", "5 seconds")).agg(sum("val").alias("sum")) >>> w.select(w.window.start.cast("string").alias("start"), ... w.window.end.cast("string").alias("end"), "sum").collect() [Row(start=u'2016-03-11 09:00:05', end=u'2016-03-11 09:00:10', sum=1)] """ def check_string_field(field, fieldName): if not field or type(field) is not str: raise TypeError("%s should be provided as a string" % fieldName) sc = SparkContext._active_spark_context time_col = _to_java_column(timeColumn) check_string_field(windowDuration, "windowDuration") if slideDuration and startTime: check_string_field(slideDuration, "slideDuration") check_string_field(startTime, "startTime") res = sc._jvm.functions.window(time_col, windowDuration, slideDuration, startTime) elif slideDuration: check_string_field(slideDuration, "slideDuration") res = sc._jvm.functions.window(time_col, windowDuration, slideDuration) elif startTime: check_string_field(startTime, "startTime") res = sc._jvm.functions.window(time_col, windowDuration, windowDuration, startTime) else: res = sc._jvm.functions.window(time_col, windowDuration) return Column(res)
python
def window(timeColumn, windowDuration, slideDuration=None, startTime=None): """Bucketize rows into one or more time windows given a timestamp specifying column. Window starts are inclusive but the window ends are exclusive, e.g. 12:05 will be in the window [12:05,12:10) but not in [12:00,12:05). Windows can support microsecond precision. Windows in the order of months are not supported. The time column must be of :class:`pyspark.sql.types.TimestampType`. Durations are provided as strings, e.g. '1 second', '1 day 12 hours', '2 minutes'. Valid interval strings are 'week', 'day', 'hour', 'minute', 'second', 'millisecond', 'microsecond'. If the ``slideDuration`` is not provided, the windows will be tumbling windows. The startTime is the offset with respect to 1970-01-01 00:00:00 UTC with which to start window intervals. For example, in order to have hourly tumbling windows that start 15 minutes past the hour, e.g. 12:15-13:15, 13:15-14:15... provide `startTime` as `15 minutes`. The output column will be a struct called 'window' by default with the nested columns 'start' and 'end', where 'start' and 'end' will be of :class:`pyspark.sql.types.TimestampType`. >>> df = spark.createDataFrame([("2016-03-11 09:00:07", 1)]).toDF("date", "val") >>> w = df.groupBy(window("date", "5 seconds")).agg(sum("val").alias("sum")) >>> w.select(w.window.start.cast("string").alias("start"), ... w.window.end.cast("string").alias("end"), "sum").collect() [Row(start=u'2016-03-11 09:00:05', end=u'2016-03-11 09:00:10', sum=1)] """ def check_string_field(field, fieldName): if not field or type(field) is not str: raise TypeError("%s should be provided as a string" % fieldName) sc = SparkContext._active_spark_context time_col = _to_java_column(timeColumn) check_string_field(windowDuration, "windowDuration") if slideDuration and startTime: check_string_field(slideDuration, "slideDuration") check_string_field(startTime, "startTime") res = sc._jvm.functions.window(time_col, windowDuration, slideDuration, startTime) elif slideDuration: check_string_field(slideDuration, "slideDuration") res = sc._jvm.functions.window(time_col, windowDuration, slideDuration) elif startTime: check_string_field(startTime, "startTime") res = sc._jvm.functions.window(time_col, windowDuration, windowDuration, startTime) else: res = sc._jvm.functions.window(time_col, windowDuration) return Column(res)
[ "def", "window", "(", "timeColumn", ",", "windowDuration", ",", "slideDuration", "=", "None", ",", "startTime", "=", "None", ")", ":", "def", "check_string_field", "(", "field", ",", "fieldName", ")", ":", "if", "not", "field", "or", "type", "(", "field", ")", "is", "not", "str", ":", "raise", "TypeError", "(", "\"%s should be provided as a string\"", "%", "fieldName", ")", "sc", "=", "SparkContext", ".", "_active_spark_context", "time_col", "=", "_to_java_column", "(", "timeColumn", ")", "check_string_field", "(", "windowDuration", ",", "\"windowDuration\"", ")", "if", "slideDuration", "and", "startTime", ":", "check_string_field", "(", "slideDuration", ",", "\"slideDuration\"", ")", "check_string_field", "(", "startTime", ",", "\"startTime\"", ")", "res", "=", "sc", ".", "_jvm", ".", "functions", ".", "window", "(", "time_col", ",", "windowDuration", ",", "slideDuration", ",", "startTime", ")", "elif", "slideDuration", ":", "check_string_field", "(", "slideDuration", ",", "\"slideDuration\"", ")", "res", "=", "sc", ".", "_jvm", ".", "functions", ".", "window", "(", "time_col", ",", "windowDuration", ",", "slideDuration", ")", "elif", "startTime", ":", "check_string_field", "(", "startTime", ",", "\"startTime\"", ")", "res", "=", "sc", ".", "_jvm", ".", "functions", ".", "window", "(", "time_col", ",", "windowDuration", ",", "windowDuration", ",", "startTime", ")", "else", ":", "res", "=", "sc", ".", "_jvm", ".", "functions", ".", "window", "(", "time_col", ",", "windowDuration", ")", "return", "Column", "(", "res", ")" ]
Bucketize rows into one or more time windows given a timestamp specifying column. Window starts are inclusive but the window ends are exclusive, e.g. 12:05 will be in the window [12:05,12:10) but not in [12:00,12:05). Windows can support microsecond precision. Windows in the order of months are not supported. The time column must be of :class:`pyspark.sql.types.TimestampType`. Durations are provided as strings, e.g. '1 second', '1 day 12 hours', '2 minutes'. Valid interval strings are 'week', 'day', 'hour', 'minute', 'second', 'millisecond', 'microsecond'. If the ``slideDuration`` is not provided, the windows will be tumbling windows. The startTime is the offset with respect to 1970-01-01 00:00:00 UTC with which to start window intervals. For example, in order to have hourly tumbling windows that start 15 minutes past the hour, e.g. 12:15-13:15, 13:15-14:15... provide `startTime` as `15 minutes`. The output column will be a struct called 'window' by default with the nested columns 'start' and 'end', where 'start' and 'end' will be of :class:`pyspark.sql.types.TimestampType`. >>> df = spark.createDataFrame([("2016-03-11 09:00:07", 1)]).toDF("date", "val") >>> w = df.groupBy(window("date", "5 seconds")).agg(sum("val").alias("sum")) >>> w.select(w.window.start.cast("string").alias("start"), ... w.window.end.cast("string").alias("end"), "sum").collect() [Row(start=u'2016-03-11 09:00:05', end=u'2016-03-11 09:00:10', sum=1)]
[ "Bucketize", "rows", "into", "one", "or", "more", "time", "windows", "given", "a", "timestamp", "specifying", "column", ".", "Window", "starts", "are", "inclusive", "but", "the", "window", "ends", "are", "exclusive", "e", ".", "g", ".", "12", ":", "05", "will", "be", "in", "the", "window", "[", "12", ":", "05", "12", ":", "10", ")", "but", "not", "in", "[", "12", ":", "00", "12", ":", "05", ")", ".", "Windows", "can", "support", "microsecond", "precision", ".", "Windows", "in", "the", "order", "of", "months", "are", "not", "supported", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1358-L1402
apache/spark
python/pyspark/sql/functions.py
hash
def hash(*cols): """Calculates the hash code of given columns, and returns the result as an int column. >>> spark.createDataFrame([('ABC',)], ['a']).select(hash('a').alias('hash')).collect() [Row(hash=-757602832)] """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.hash(_to_seq(sc, cols, _to_java_column)) return Column(jc)
python
def hash(*cols): """Calculates the hash code of given columns, and returns the result as an int column. >>> spark.createDataFrame([('ABC',)], ['a']).select(hash('a').alias('hash')).collect() [Row(hash=-757602832)] """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.hash(_to_seq(sc, cols, _to_java_column)) return Column(jc)
[ "def", "hash", "(", "*", "cols", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "hash", "(", "_to_seq", "(", "sc", ",", "cols", ",", "_to_java_column", ")", ")", "return", "Column", "(", "jc", ")" ]
Calculates the hash code of given columns, and returns the result as an int column. >>> spark.createDataFrame([('ABC',)], ['a']).select(hash('a').alias('hash')).collect() [Row(hash=-757602832)]
[ "Calculates", "the", "hash", "code", "of", "given", "columns", "and", "returns", "the", "result", "as", "an", "int", "column", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1466-L1474
apache/spark
python/pyspark/sql/functions.py
concat_ws
def concat_ws(sep, *cols): """ Concatenates multiple input string columns together into a single string column, using the given separator. >>> df = spark.createDataFrame([('abcd','123')], ['s', 'd']) >>> df.select(concat_ws('-', df.s, df.d).alias('s')).collect() [Row(s=u'abcd-123')] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.concat_ws(sep, _to_seq(sc, cols, _to_java_column)))
python
def concat_ws(sep, *cols): """ Concatenates multiple input string columns together into a single string column, using the given separator. >>> df = spark.createDataFrame([('abcd','123')], ['s', 'd']) >>> df.select(concat_ws('-', df.s, df.d).alias('s')).collect() [Row(s=u'abcd-123')] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.concat_ws(sep, _to_seq(sc, cols, _to_java_column)))
[ "def", "concat_ws", "(", "sep", ",", "*", "cols", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "concat_ws", "(", "sep", ",", "_to_seq", "(", "sc", ",", "cols", ",", "_to_java_column", ")", ")", ")" ]
Concatenates multiple input string columns together into a single string column, using the given separator. >>> df = spark.createDataFrame([('abcd','123')], ['s', 'd']) >>> df.select(concat_ws('-', df.s, df.d).alias('s')).collect() [Row(s=u'abcd-123')]
[ "Concatenates", "multiple", "input", "string", "columns", "together", "into", "a", "single", "string", "column", "using", "the", "given", "separator", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1511-L1521
apache/spark
python/pyspark/sql/functions.py
decode
def decode(col, charset): """ Computes the first argument into a string from a binary using the provided character set (one of 'US-ASCII', 'ISO-8859-1', 'UTF-8', 'UTF-16BE', 'UTF-16LE', 'UTF-16'). """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.decode(_to_java_column(col), charset))
python
def decode(col, charset): """ Computes the first argument into a string from a binary using the provided character set (one of 'US-ASCII', 'ISO-8859-1', 'UTF-8', 'UTF-16BE', 'UTF-16LE', 'UTF-16'). """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.decode(_to_java_column(col), charset))
[ "def", "decode", "(", "col", ",", "charset", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "decode", "(", "_to_java_column", "(", "col", ")", ",", "charset", ")", ")" ]
Computes the first argument into a string from a binary using the provided character set (one of 'US-ASCII', 'ISO-8859-1', 'UTF-8', 'UTF-16BE', 'UTF-16LE', 'UTF-16').
[ "Computes", "the", "first", "argument", "into", "a", "string", "from", "a", "binary", "using", "the", "provided", "character", "set", "(", "one", "of", "US", "-", "ASCII", "ISO", "-", "8859", "-", "1", "UTF", "-", "8", "UTF", "-", "16BE", "UTF", "-", "16LE", "UTF", "-", "16", ")", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1525-L1531
apache/spark
python/pyspark/sql/functions.py
format_number
def format_number(col, d): """ Formats the number X to a format like '#,--#,--#.--', rounded to d decimal places with HALF_EVEN round mode, and returns the result as a string. :param col: the column name of the numeric value to be formatted :param d: the N decimal places >>> spark.createDataFrame([(5,)], ['a']).select(format_number('a', 4).alias('v')).collect() [Row(v=u'5.0000')] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.format_number(_to_java_column(col), d))
python
def format_number(col, d): """ Formats the number X to a format like '#,--#,--#.--', rounded to d decimal places with HALF_EVEN round mode, and returns the result as a string. :param col: the column name of the numeric value to be formatted :param d: the N decimal places >>> spark.createDataFrame([(5,)], ['a']).select(format_number('a', 4).alias('v')).collect() [Row(v=u'5.0000')] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.format_number(_to_java_column(col), d))
[ "def", "format_number", "(", "col", ",", "d", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "format_number", "(", "_to_java_column", "(", "col", ")", ",", "d", ")", ")" ]
Formats the number X to a format like '#,--#,--#.--', rounded to d decimal places with HALF_EVEN round mode, and returns the result as a string. :param col: the column name of the numeric value to be formatted :param d: the N decimal places >>> spark.createDataFrame([(5,)], ['a']).select(format_number('a', 4).alias('v')).collect() [Row(v=u'5.0000')]
[ "Formats", "the", "number", "X", "to", "a", "format", "like", "#", "--", "#", "--", "#", ".", "--", "rounded", "to", "d", "decimal", "places", "with", "HALF_EVEN", "round", "mode", "and", "returns", "the", "result", "as", "a", "string", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1546-L1558
apache/spark
python/pyspark/sql/functions.py
format_string
def format_string(format, *cols): """ Formats the arguments in printf-style and returns the result as a string column. :param col: the column name of the numeric value to be formatted :param d: the N decimal places >>> df = spark.createDataFrame([(5, "hello")], ['a', 'b']) >>> df.select(format_string('%d %s', df.a, df.b).alias('v')).collect() [Row(v=u'5 hello')] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.format_string(format, _to_seq(sc, cols, _to_java_column)))
python
def format_string(format, *cols): """ Formats the arguments in printf-style and returns the result as a string column. :param col: the column name of the numeric value to be formatted :param d: the N decimal places >>> df = spark.createDataFrame([(5, "hello")], ['a', 'b']) >>> df.select(format_string('%d %s', df.a, df.b).alias('v')).collect() [Row(v=u'5 hello')] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.format_string(format, _to_seq(sc, cols, _to_java_column)))
[ "def", "format_string", "(", "format", ",", "*", "cols", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "format_string", "(", "format", ",", "_to_seq", "(", "sc", ",", "cols", ",", "_to_java_column", ")", ")", ")" ]
Formats the arguments in printf-style and returns the result as a string column. :param col: the column name of the numeric value to be formatted :param d: the N decimal places >>> df = spark.createDataFrame([(5, "hello")], ['a', 'b']) >>> df.select(format_string('%d %s', df.a, df.b).alias('v')).collect() [Row(v=u'5 hello')]
[ "Formats", "the", "arguments", "in", "printf", "-", "style", "and", "returns", "the", "result", "as", "a", "string", "column", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1563-L1575
apache/spark
python/pyspark/sql/functions.py
instr
def instr(str, substr): """ Locate the position of the first occurrence of substr column in the given string. Returns null if either of the arguments are null. .. note:: The position is not zero based, but 1 based index. Returns 0 if substr could not be found in str. >>> df = spark.createDataFrame([('abcd',)], ['s',]) >>> df.select(instr(df.s, 'b').alias('s')).collect() [Row(s=2)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.instr(_to_java_column(str), substr))
python
def instr(str, substr): """ Locate the position of the first occurrence of substr column in the given string. Returns null if either of the arguments are null. .. note:: The position is not zero based, but 1 based index. Returns 0 if substr could not be found in str. >>> df = spark.createDataFrame([('abcd',)], ['s',]) >>> df.select(instr(df.s, 'b').alias('s')).collect() [Row(s=2)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.instr(_to_java_column(str), substr))
[ "def", "instr", "(", "str", ",", "substr", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "instr", "(", "_to_java_column", "(", "str", ")", ",", "substr", ")", ")" ]
Locate the position of the first occurrence of substr column in the given string. Returns null if either of the arguments are null. .. note:: The position is not zero based, but 1 based index. Returns 0 if substr could not be found in str. >>> df = spark.createDataFrame([('abcd',)], ['s',]) >>> df.select(instr(df.s, 'b').alias('s')).collect() [Row(s=2)]
[ "Locate", "the", "position", "of", "the", "first", "occurrence", "of", "substr", "column", "in", "the", "given", "string", ".", "Returns", "null", "if", "either", "of", "the", "arguments", "are", "null", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1579-L1592
apache/spark
python/pyspark/sql/functions.py
substring
def substring(str, pos, len): """ Substring starts at `pos` and is of length `len` when str is String type or returns the slice of byte array that starts at `pos` in byte and is of length `len` when str is Binary type. .. note:: The position is not zero based, but 1 based index. >>> df = spark.createDataFrame([('abcd',)], ['s',]) >>> df.select(substring(df.s, 1, 2).alias('s')).collect() [Row(s=u'ab')] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.substring(_to_java_column(str), pos, len))
python
def substring(str, pos, len): """ Substring starts at `pos` and is of length `len` when str is String type or returns the slice of byte array that starts at `pos` in byte and is of length `len` when str is Binary type. .. note:: The position is not zero based, but 1 based index. >>> df = spark.createDataFrame([('abcd',)], ['s',]) >>> df.select(substring(df.s, 1, 2).alias('s')).collect() [Row(s=u'ab')] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.substring(_to_java_column(str), pos, len))
[ "def", "substring", "(", "str", ",", "pos", ",", "len", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "substring", "(", "_to_java_column", "(", "str", ")", ",", "pos", ",", "len", ")", ")" ]
Substring starts at `pos` and is of length `len` when str is String type or returns the slice of byte array that starts at `pos` in byte and is of length `len` when str is Binary type. .. note:: The position is not zero based, but 1 based index. >>> df = spark.createDataFrame([('abcd',)], ['s',]) >>> df.select(substring(df.s, 1, 2).alias('s')).collect() [Row(s=u'ab')]
[ "Substring", "starts", "at", "pos", "and", "is", "of", "length", "len", "when", "str", "is", "String", "type", "or", "returns", "the", "slice", "of", "byte", "array", "that", "starts", "at", "pos", "in", "byte", "and", "is", "of", "length", "len", "when", "str", "is", "Binary", "type", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1597-L1610
apache/spark
python/pyspark/sql/functions.py
substring_index
def substring_index(str, delim, count): """ Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything the left of the final delimiter (counting from left) is returned. If count is negative, every to the right of the final delimiter (counting from the right) is returned. substring_index performs a case-sensitive match when searching for delim. >>> df = spark.createDataFrame([('a.b.c.d',)], ['s']) >>> df.select(substring_index(df.s, '.', 2).alias('s')).collect() [Row(s=u'a.b')] >>> df.select(substring_index(df.s, '.', -3).alias('s')).collect() [Row(s=u'b.c.d')] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.substring_index(_to_java_column(str), delim, count))
python
def substring_index(str, delim, count): """ Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything the left of the final delimiter (counting from left) is returned. If count is negative, every to the right of the final delimiter (counting from the right) is returned. substring_index performs a case-sensitive match when searching for delim. >>> df = spark.createDataFrame([('a.b.c.d',)], ['s']) >>> df.select(substring_index(df.s, '.', 2).alias('s')).collect() [Row(s=u'a.b')] >>> df.select(substring_index(df.s, '.', -3).alias('s')).collect() [Row(s=u'b.c.d')] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.substring_index(_to_java_column(str), delim, count))
[ "def", "substring_index", "(", "str", ",", "delim", ",", "count", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "substring_index", "(", "_to_java_column", "(", "str", ")", ",", "delim", ",", "count", ")", ")" ]
Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything the left of the final delimiter (counting from left) is returned. If count is negative, every to the right of the final delimiter (counting from the right) is returned. substring_index performs a case-sensitive match when searching for delim. >>> df = spark.createDataFrame([('a.b.c.d',)], ['s']) >>> df.select(substring_index(df.s, '.', 2).alias('s')).collect() [Row(s=u'a.b')] >>> df.select(substring_index(df.s, '.', -3).alias('s')).collect() [Row(s=u'b.c.d')]
[ "Returns", "the", "substring", "from", "string", "str", "before", "count", "occurrences", "of", "the", "delimiter", "delim", ".", "If", "count", "is", "positive", "everything", "the", "left", "of", "the", "final", "delimiter", "(", "counting", "from", "left", ")", "is", "returned", ".", "If", "count", "is", "negative", "every", "to", "the", "right", "of", "the", "final", "delimiter", "(", "counting", "from", "the", "right", ")", "is", "returned", ".", "substring_index", "performs", "a", "case", "-", "sensitive", "match", "when", "searching", "for", "delim", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1615-L1629
apache/spark
python/pyspark/sql/functions.py
levenshtein
def levenshtein(left, right): """Computes the Levenshtein distance of the two given strings. >>> df0 = spark.createDataFrame([('kitten', 'sitting',)], ['l', 'r']) >>> df0.select(levenshtein('l', 'r').alias('d')).collect() [Row(d=3)] """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.levenshtein(_to_java_column(left), _to_java_column(right)) return Column(jc)
python
def levenshtein(left, right): """Computes the Levenshtein distance of the two given strings. >>> df0 = spark.createDataFrame([('kitten', 'sitting',)], ['l', 'r']) >>> df0.select(levenshtein('l', 'r').alias('d')).collect() [Row(d=3)] """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.levenshtein(_to_java_column(left), _to_java_column(right)) return Column(jc)
[ "def", "levenshtein", "(", "left", ",", "right", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "levenshtein", "(", "_to_java_column", "(", "left", ")", ",", "_to_java_column", "(", "right", ")", ")", "return", "Column", "(", "jc", ")" ]
Computes the Levenshtein distance of the two given strings. >>> df0 = spark.createDataFrame([('kitten', 'sitting',)], ['l', 'r']) >>> df0.select(levenshtein('l', 'r').alias('d')).collect() [Row(d=3)]
[ "Computes", "the", "Levenshtein", "distance", "of", "the", "two", "given", "strings", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1634-L1643
apache/spark
python/pyspark/sql/functions.py
locate
def locate(substr, str, pos=1): """ Locate the position of the first occurrence of substr in a string column, after position pos. .. note:: The position is not zero based, but 1 based index. Returns 0 if substr could not be found in str. :param substr: a string :param str: a Column of :class:`pyspark.sql.types.StringType` :param pos: start position (zero based) >>> df = spark.createDataFrame([('abcd',)], ['s',]) >>> df.select(locate('b', df.s, 1).alias('s')).collect() [Row(s=2)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.locate(substr, _to_java_column(str), pos))
python
def locate(substr, str, pos=1): """ Locate the position of the first occurrence of substr in a string column, after position pos. .. note:: The position is not zero based, but 1 based index. Returns 0 if substr could not be found in str. :param substr: a string :param str: a Column of :class:`pyspark.sql.types.StringType` :param pos: start position (zero based) >>> df = spark.createDataFrame([('abcd',)], ['s',]) >>> df.select(locate('b', df.s, 1).alias('s')).collect() [Row(s=2)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.locate(substr, _to_java_column(str), pos))
[ "def", "locate", "(", "substr", ",", "str", ",", "pos", "=", "1", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "locate", "(", "substr", ",", "_to_java_column", "(", "str", ")", ",", "pos", ")", ")" ]
Locate the position of the first occurrence of substr in a string column, after position pos. .. note:: The position is not zero based, but 1 based index. Returns 0 if substr could not be found in str. :param substr: a string :param str: a Column of :class:`pyspark.sql.types.StringType` :param pos: start position (zero based) >>> df = spark.createDataFrame([('abcd',)], ['s',]) >>> df.select(locate('b', df.s, 1).alias('s')).collect() [Row(s=2)]
[ "Locate", "the", "position", "of", "the", "first", "occurrence", "of", "substr", "in", "a", "string", "column", "after", "position", "pos", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1647-L1663
apache/spark
python/pyspark/sql/functions.py
lpad
def lpad(col, len, pad): """ Left-pad the string column to width `len` with `pad`. >>> df = spark.createDataFrame([('abcd',)], ['s',]) >>> df.select(lpad(df.s, 6, '#').alias('s')).collect() [Row(s=u'##abcd')] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.lpad(_to_java_column(col), len, pad))
python
def lpad(col, len, pad): """ Left-pad the string column to width `len` with `pad`. >>> df = spark.createDataFrame([('abcd',)], ['s',]) >>> df.select(lpad(df.s, 6, '#').alias('s')).collect() [Row(s=u'##abcd')] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.lpad(_to_java_column(col), len, pad))
[ "def", "lpad", "(", "col", ",", "len", ",", "pad", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "lpad", "(", "_to_java_column", "(", "col", ")", ",", "len", ",", "pad", ")", ")" ]
Left-pad the string column to width `len` with `pad`. >>> df = spark.createDataFrame([('abcd',)], ['s',]) >>> df.select(lpad(df.s, 6, '#').alias('s')).collect() [Row(s=u'##abcd')]
[ "Left", "-", "pad", "the", "string", "column", "to", "width", "len", "with", "pad", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1668-L1677
apache/spark
python/pyspark/sql/functions.py
repeat
def repeat(col, n): """ Repeats a string column n times, and returns it as a new string column. >>> df = spark.createDataFrame([('ab',)], ['s',]) >>> df.select(repeat(df.s, 3).alias('s')).collect() [Row(s=u'ababab')] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.repeat(_to_java_column(col), n))
python
def repeat(col, n): """ Repeats a string column n times, and returns it as a new string column. >>> df = spark.createDataFrame([('ab',)], ['s',]) >>> df.select(repeat(df.s, 3).alias('s')).collect() [Row(s=u'ababab')] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.repeat(_to_java_column(col), n))
[ "def", "repeat", "(", "col", ",", "n", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "repeat", "(", "_to_java_column", "(", "col", ")", ",", "n", ")", ")" ]
Repeats a string column n times, and returns it as a new string column. >>> df = spark.createDataFrame([('ab',)], ['s',]) >>> df.select(repeat(df.s, 3).alias('s')).collect() [Row(s=u'ababab')]
[ "Repeats", "a", "string", "column", "n", "times", "and", "returns", "it", "as", "a", "new", "string", "column", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1696-L1705
apache/spark
python/pyspark/sql/functions.py
split
def split(str, pattern, limit=-1): """ Splits str around matches of the given pattern. :param str: a string expression to split :param pattern: a string representing a regular expression. The regex string should be a Java regular expression. :param limit: an integer which controls the number of times `pattern` is applied. * ``limit > 0``: The resulting array's length will not be more than `limit`, and the resulting array's last entry will contain all input beyond the last matched pattern. * ``limit <= 0``: `pattern` will be applied as many times as possible, and the resulting array can be of any size. .. versionchanged:: 3.0 `split` now takes an optional `limit` field. If not provided, default limit value is -1. >>> df = spark.createDataFrame([('oneAtwoBthreeC',)], ['s',]) >>> df.select(split(df.s, '[ABC]', 2).alias('s')).collect() [Row(s=[u'one', u'twoBthreeC'])] >>> df.select(split(df.s, '[ABC]', -1).alias('s')).collect() [Row(s=[u'one', u'two', u'three', u''])] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.split(_to_java_column(str), pattern, limit))
python
def split(str, pattern, limit=-1): """ Splits str around matches of the given pattern. :param str: a string expression to split :param pattern: a string representing a regular expression. The regex string should be a Java regular expression. :param limit: an integer which controls the number of times `pattern` is applied. * ``limit > 0``: The resulting array's length will not be more than `limit`, and the resulting array's last entry will contain all input beyond the last matched pattern. * ``limit <= 0``: `pattern` will be applied as many times as possible, and the resulting array can be of any size. .. versionchanged:: 3.0 `split` now takes an optional `limit` field. If not provided, default limit value is -1. >>> df = spark.createDataFrame([('oneAtwoBthreeC',)], ['s',]) >>> df.select(split(df.s, '[ABC]', 2).alias('s')).collect() [Row(s=[u'one', u'twoBthreeC'])] >>> df.select(split(df.s, '[ABC]', -1).alias('s')).collect() [Row(s=[u'one', u'two', u'three', u''])] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.split(_to_java_column(str), pattern, limit))
[ "def", "split", "(", "str", ",", "pattern", ",", "limit", "=", "-", "1", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "split", "(", "_to_java_column", "(", "str", ")", ",", "pattern", ",", "limit", ")", ")" ]
Splits str around matches of the given pattern. :param str: a string expression to split :param pattern: a string representing a regular expression. The regex string should be a Java regular expression. :param limit: an integer which controls the number of times `pattern` is applied. * ``limit > 0``: The resulting array's length will not be more than `limit`, and the resulting array's last entry will contain all input beyond the last matched pattern. * ``limit <= 0``: `pattern` will be applied as many times as possible, and the resulting array can be of any size. .. versionchanged:: 3.0 `split` now takes an optional `limit` field. If not provided, default limit value is -1. >>> df = spark.createDataFrame([('oneAtwoBthreeC',)], ['s',]) >>> df.select(split(df.s, '[ABC]', 2).alias('s')).collect() [Row(s=[u'one', u'twoBthreeC'])] >>> df.select(split(df.s, '[ABC]', -1).alias('s')).collect() [Row(s=[u'one', u'two', u'three', u''])]
[ "Splits", "str", "around", "matches", "of", "the", "given", "pattern", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1710-L1735
apache/spark
python/pyspark/sql/functions.py
regexp_extract
def regexp_extract(str, pattern, idx): r"""Extract a specific group matched by a Java regex, from the specified string column. If the regex did not match, or the specified group did not match, an empty string is returned. >>> df = spark.createDataFrame([('100-200',)], ['str']) >>> df.select(regexp_extract('str', r'(\d+)-(\d+)', 1).alias('d')).collect() [Row(d=u'100')] >>> df = spark.createDataFrame([('foo',)], ['str']) >>> df.select(regexp_extract('str', r'(\d+)', 1).alias('d')).collect() [Row(d=u'')] >>> df = spark.createDataFrame([('aaaac',)], ['str']) >>> df.select(regexp_extract('str', '(a+)(b)?(c)', 2).alias('d')).collect() [Row(d=u'')] """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.regexp_extract(_to_java_column(str), pattern, idx) return Column(jc)
python
def regexp_extract(str, pattern, idx): r"""Extract a specific group matched by a Java regex, from the specified string column. If the regex did not match, or the specified group did not match, an empty string is returned. >>> df = spark.createDataFrame([('100-200',)], ['str']) >>> df.select(regexp_extract('str', r'(\d+)-(\d+)', 1).alias('d')).collect() [Row(d=u'100')] >>> df = spark.createDataFrame([('foo',)], ['str']) >>> df.select(regexp_extract('str', r'(\d+)', 1).alias('d')).collect() [Row(d=u'')] >>> df = spark.createDataFrame([('aaaac',)], ['str']) >>> df.select(regexp_extract('str', '(a+)(b)?(c)', 2).alias('d')).collect() [Row(d=u'')] """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.regexp_extract(_to_java_column(str), pattern, idx) return Column(jc)
[ "def", "regexp_extract", "(", "str", ",", "pattern", ",", "idx", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "regexp_extract", "(", "_to_java_column", "(", "str", ")", ",", "pattern", ",", "idx", ")", "return", "Column", "(", "jc", ")" ]
r"""Extract a specific group matched by a Java regex, from the specified string column. If the regex did not match, or the specified group did not match, an empty string is returned. >>> df = spark.createDataFrame([('100-200',)], ['str']) >>> df.select(regexp_extract('str', r'(\d+)-(\d+)', 1).alias('d')).collect() [Row(d=u'100')] >>> df = spark.createDataFrame([('foo',)], ['str']) >>> df.select(regexp_extract('str', r'(\d+)', 1).alias('d')).collect() [Row(d=u'')] >>> df = spark.createDataFrame([('aaaac',)], ['str']) >>> df.select(regexp_extract('str', '(a+)(b)?(c)', 2).alias('d')).collect() [Row(d=u'')]
[ "r", "Extract", "a", "specific", "group", "matched", "by", "a", "Java", "regex", "from", "the", "specified", "string", "column", ".", "If", "the", "regex", "did", "not", "match", "or", "the", "specified", "group", "did", "not", "match", "an", "empty", "string", "is", "returned", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1740-L1756
apache/spark
python/pyspark/sql/functions.py
regexp_replace
def regexp_replace(str, pattern, replacement): r"""Replace all substrings of the specified string value that match regexp with rep. >>> df = spark.createDataFrame([('100-200',)], ['str']) >>> df.select(regexp_replace('str', r'(\d+)', '--').alias('d')).collect() [Row(d=u'-----')] """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.regexp_replace(_to_java_column(str), pattern, replacement) return Column(jc)
python
def regexp_replace(str, pattern, replacement): r"""Replace all substrings of the specified string value that match regexp with rep. >>> df = spark.createDataFrame([('100-200',)], ['str']) >>> df.select(regexp_replace('str', r'(\d+)', '--').alias('d')).collect() [Row(d=u'-----')] """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.regexp_replace(_to_java_column(str), pattern, replacement) return Column(jc)
[ "def", "regexp_replace", "(", "str", ",", "pattern", ",", "replacement", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "regexp_replace", "(", "_to_java_column", "(", "str", ")", ",", "pattern", ",", "replacement", ")", "return", "Column", "(", "jc", ")" ]
r"""Replace all substrings of the specified string value that match regexp with rep. >>> df = spark.createDataFrame([('100-200',)], ['str']) >>> df.select(regexp_replace('str', r'(\d+)', '--').alias('d')).collect() [Row(d=u'-----')]
[ "r", "Replace", "all", "substrings", "of", "the", "specified", "string", "value", "that", "match", "regexp", "with", "rep", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1761-L1770
apache/spark
python/pyspark/sql/functions.py
translate
def translate(srcCol, matching, replace): """A function translate any character in the `srcCol` by a character in `matching`. The characters in `replace` is corresponding to the characters in `matching`. The translate will happen when any character in the string matching with the character in the `matching`. >>> spark.createDataFrame([('translate',)], ['a']).select(translate('a', "rnlt", "123") \\ ... .alias('r')).collect() [Row(r=u'1a2s3ae')] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.translate(_to_java_column(srcCol), matching, replace))
python
def translate(srcCol, matching, replace): """A function translate any character in the `srcCol` by a character in `matching`. The characters in `replace` is corresponding to the characters in `matching`. The translate will happen when any character in the string matching with the character in the `matching`. >>> spark.createDataFrame([('translate',)], ['a']).select(translate('a', "rnlt", "123") \\ ... .alias('r')).collect() [Row(r=u'1a2s3ae')] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.translate(_to_java_column(srcCol), matching, replace))
[ "def", "translate", "(", "srcCol", ",", "matching", ",", "replace", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "translate", "(", "_to_java_column", "(", "srcCol", ")", ",", "matching", ",", "replace", ")", ")" ]
A function translate any character in the `srcCol` by a character in `matching`. The characters in `replace` is corresponding to the characters in `matching`. The translate will happen when any character in the string matching with the character in the `matching`. >>> spark.createDataFrame([('translate',)], ['a']).select(translate('a', "rnlt", "123") \\ ... .alias('r')).collect() [Row(r=u'1a2s3ae')]
[ "A", "function", "translate", "any", "character", "in", "the", "srcCol", "by", "a", "character", "in", "matching", ".", "The", "characters", "in", "replace", "is", "corresponding", "to", "the", "characters", "in", "matching", ".", "The", "translate", "will", "happen", "when", "any", "character", "in", "the", "string", "matching", "with", "the", "character", "in", "the", "matching", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1856-L1867
apache/spark
python/pyspark/sql/functions.py
arrays_overlap
def arrays_overlap(a1, a2): """ Collection function: returns true if the arrays contain any common non-null element; if not, returns null if both the arrays are non-empty and any of them contains a null element; returns false otherwise. >>> df = spark.createDataFrame([(["a", "b"], ["b", "c"]), (["a"], ["b", "c"])], ['x', 'y']) >>> df.select(arrays_overlap(df.x, df.y).alias("overlap")).collect() [Row(overlap=True), Row(overlap=False)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.arrays_overlap(_to_java_column(a1), _to_java_column(a2)))
python
def arrays_overlap(a1, a2): """ Collection function: returns true if the arrays contain any common non-null element; if not, returns null if both the arrays are non-empty and any of them contains a null element; returns false otherwise. >>> df = spark.createDataFrame([(["a", "b"], ["b", "c"]), (["a"], ["b", "c"])], ['x', 'y']) >>> df.select(arrays_overlap(df.x, df.y).alias("overlap")).collect() [Row(overlap=True), Row(overlap=False)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.arrays_overlap(_to_java_column(a1), _to_java_column(a2)))
[ "def", "arrays_overlap", "(", "a1", ",", "a2", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "arrays_overlap", "(", "_to_java_column", "(", "a1", ")", ",", "_to_java_column", "(", "a2", ")", ")", ")" ]
Collection function: returns true if the arrays contain any common non-null element; if not, returns null if both the arrays are non-empty and any of them contains a null element; returns false otherwise. >>> df = spark.createDataFrame([(["a", "b"], ["b", "c"]), (["a"], ["b", "c"])], ['x', 'y']) >>> df.select(arrays_overlap(df.x, df.y).alias("overlap")).collect() [Row(overlap=True), Row(overlap=False)]
[ "Collection", "function", ":", "returns", "true", "if", "the", "arrays", "contain", "any", "common", "non", "-", "null", "element", ";", "if", "not", "returns", "null", "if", "both", "the", "arrays", "are", "non", "-", "empty", "and", "any", "of", "them", "contains", "a", "null", "element", ";", "returns", "false", "otherwise", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1948-L1959
apache/spark
python/pyspark/sql/functions.py
slice
def slice(x, start, length): """ Collection function: returns an array containing all the elements in `x` from index `start` (or starting from the end if `start` is negative) with the specified `length`. >>> df = spark.createDataFrame([([1, 2, 3],), ([4, 5],)], ['x']) >>> df.select(slice(df.x, 2, 2).alias("sliced")).collect() [Row(sliced=[2, 3]), Row(sliced=[5])] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.slice(_to_java_column(x), start, length))
python
def slice(x, start, length): """ Collection function: returns an array containing all the elements in `x` from index `start` (or starting from the end if `start` is negative) with the specified `length`. >>> df = spark.createDataFrame([([1, 2, 3],), ([4, 5],)], ['x']) >>> df.select(slice(df.x, 2, 2).alias("sliced")).collect() [Row(sliced=[2, 3]), Row(sliced=[5])] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.slice(_to_java_column(x), start, length))
[ "def", "slice", "(", "x", ",", "start", ",", "length", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "slice", "(", "_to_java_column", "(", "x", ")", ",", "start", ",", "length", ")", ")" ]
Collection function: returns an array containing all the elements in `x` from index `start` (or starting from the end if `start` is negative) with the specified `length`. >>> df = spark.createDataFrame([([1, 2, 3],), ([4, 5],)], ['x']) >>> df.select(slice(df.x, 2, 2).alias("sliced")).collect() [Row(sliced=[2, 3]), Row(sliced=[5])]
[ "Collection", "function", ":", "returns", "an", "array", "containing", "all", "the", "elements", "in", "x", "from", "index", "start", "(", "or", "starting", "from", "the", "end", "if", "start", "is", "negative", ")", "with", "the", "specified", "length", ".", ">>>", "df", "=", "spark", ".", "createDataFrame", "(", "[", "(", "[", "1", "2", "3", "]", ")", "(", "[", "4", "5", "]", ")", "]", "[", "x", "]", ")", ">>>", "df", ".", "select", "(", "slice", "(", "df", ".", "x", "2", "2", ")", ".", "alias", "(", "sliced", "))", ".", "collect", "()", "[", "Row", "(", "sliced", "=", "[", "2", "3", "]", ")", "Row", "(", "sliced", "=", "[", "5", "]", ")", "]" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1963-L1972
apache/spark
python/pyspark/sql/functions.py
array_join
def array_join(col, delimiter, null_replacement=None): """ Concatenates the elements of `column` using the `delimiter`. Null values are replaced with `null_replacement` if set, otherwise they are ignored. >>> df = spark.createDataFrame([(["a", "b", "c"],), (["a", None],)], ['data']) >>> df.select(array_join(df.data, ",").alias("joined")).collect() [Row(joined=u'a,b,c'), Row(joined=u'a')] >>> df.select(array_join(df.data, ",", "NULL").alias("joined")).collect() [Row(joined=u'a,b,c'), Row(joined=u'a,NULL')] """ sc = SparkContext._active_spark_context if null_replacement is None: return Column(sc._jvm.functions.array_join(_to_java_column(col), delimiter)) else: return Column(sc._jvm.functions.array_join( _to_java_column(col), delimiter, null_replacement))
python
def array_join(col, delimiter, null_replacement=None): """ Concatenates the elements of `column` using the `delimiter`. Null values are replaced with `null_replacement` if set, otherwise they are ignored. >>> df = spark.createDataFrame([(["a", "b", "c"],), (["a", None],)], ['data']) >>> df.select(array_join(df.data, ",").alias("joined")).collect() [Row(joined=u'a,b,c'), Row(joined=u'a')] >>> df.select(array_join(df.data, ",", "NULL").alias("joined")).collect() [Row(joined=u'a,b,c'), Row(joined=u'a,NULL')] """ sc = SparkContext._active_spark_context if null_replacement is None: return Column(sc._jvm.functions.array_join(_to_java_column(col), delimiter)) else: return Column(sc._jvm.functions.array_join( _to_java_column(col), delimiter, null_replacement))
[ "def", "array_join", "(", "col", ",", "delimiter", ",", "null_replacement", "=", "None", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "if", "null_replacement", "is", "None", ":", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "array_join", "(", "_to_java_column", "(", "col", ")", ",", "delimiter", ")", ")", "else", ":", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "array_join", "(", "_to_java_column", "(", "col", ")", ",", "delimiter", ",", "null_replacement", ")", ")" ]
Concatenates the elements of `column` using the `delimiter`. Null values are replaced with `null_replacement` if set, otherwise they are ignored. >>> df = spark.createDataFrame([(["a", "b", "c"],), (["a", None],)], ['data']) >>> df.select(array_join(df.data, ",").alias("joined")).collect() [Row(joined=u'a,b,c'), Row(joined=u'a')] >>> df.select(array_join(df.data, ",", "NULL").alias("joined")).collect() [Row(joined=u'a,b,c'), Row(joined=u'a,NULL')]
[ "Concatenates", "the", "elements", "of", "column", "using", "the", "delimiter", ".", "Null", "values", "are", "replaced", "with", "null_replacement", "if", "set", "otherwise", "they", "are", "ignored", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1977-L1993
apache/spark
python/pyspark/sql/functions.py
concat
def concat(*cols): """ Concatenates multiple input columns together into a single column. The function works with strings, binary and compatible array columns. >>> df = spark.createDataFrame([('abcd','123')], ['s', 'd']) >>> df.select(concat(df.s, df.d).alias('s')).collect() [Row(s=u'abcd123')] >>> df = spark.createDataFrame([([1, 2], [3, 4], [5]), ([1, 2], None, [3])], ['a', 'b', 'c']) >>> df.select(concat(df.a, df.b, df.c).alias("arr")).collect() [Row(arr=[1, 2, 3, 4, 5]), Row(arr=None)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.concat(_to_seq(sc, cols, _to_java_column)))
python
def concat(*cols): """ Concatenates multiple input columns together into a single column. The function works with strings, binary and compatible array columns. >>> df = spark.createDataFrame([('abcd','123')], ['s', 'd']) >>> df.select(concat(df.s, df.d).alias('s')).collect() [Row(s=u'abcd123')] >>> df = spark.createDataFrame([([1, 2], [3, 4], [5]), ([1, 2], None, [3])], ['a', 'b', 'c']) >>> df.select(concat(df.a, df.b, df.c).alias("arr")).collect() [Row(arr=[1, 2, 3, 4, 5]), Row(arr=None)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.concat(_to_seq(sc, cols, _to_java_column)))
[ "def", "concat", "(", "*", "cols", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "concat", "(", "_to_seq", "(", "sc", ",", "cols", ",", "_to_java_column", ")", ")", ")" ]
Concatenates multiple input columns together into a single column. The function works with strings, binary and compatible array columns. >>> df = spark.createDataFrame([('abcd','123')], ['s', 'd']) >>> df.select(concat(df.s, df.d).alias('s')).collect() [Row(s=u'abcd123')] >>> df = spark.createDataFrame([([1, 2], [3, 4], [5]), ([1, 2], None, [3])], ['a', 'b', 'c']) >>> df.select(concat(df.a, df.b, df.c).alias("arr")).collect() [Row(arr=[1, 2, 3, 4, 5]), Row(arr=None)]
[ "Concatenates", "multiple", "input", "columns", "together", "into", "a", "single", "column", ".", "The", "function", "works", "with", "strings", "binary", "and", "compatible", "array", "columns", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L1998-L2012
apache/spark
python/pyspark/sql/functions.py
array_position
def array_position(col, value): """ Collection function: Locates the position of the first occurrence of the given value in the given array. Returns null if either of the arguments are null. .. note:: The position is not zero based, but 1 based index. Returns 0 if the given value could not be found in the array. >>> df = spark.createDataFrame([(["c", "b", "a"],), ([],)], ['data']) >>> df.select(array_position(df.data, "a")).collect() [Row(array_position(data, a)=3), Row(array_position(data, a)=0)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.array_position(_to_java_column(col), value))
python
def array_position(col, value): """ Collection function: Locates the position of the first occurrence of the given value in the given array. Returns null if either of the arguments are null. .. note:: The position is not zero based, but 1 based index. Returns 0 if the given value could not be found in the array. >>> df = spark.createDataFrame([(["c", "b", "a"],), ([],)], ['data']) >>> df.select(array_position(df.data, "a")).collect() [Row(array_position(data, a)=3), Row(array_position(data, a)=0)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.array_position(_to_java_column(col), value))
[ "def", "array_position", "(", "col", ",", "value", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "array_position", "(", "_to_java_column", "(", "col", ")", ",", "value", ")", ")" ]
Collection function: Locates the position of the first occurrence of the given value in the given array. Returns null if either of the arguments are null. .. note:: The position is not zero based, but 1 based index. Returns 0 if the given value could not be found in the array. >>> df = spark.createDataFrame([(["c", "b", "a"],), ([],)], ['data']) >>> df.select(array_position(df.data, "a")).collect() [Row(array_position(data, a)=3), Row(array_position(data, a)=0)]
[ "Collection", "function", ":", "Locates", "the", "position", "of", "the", "first", "occurrence", "of", "the", "given", "value", "in", "the", "given", "array", ".", "Returns", "null", "if", "either", "of", "the", "arguments", "are", "null", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L2016-L2029
apache/spark
python/pyspark/sql/functions.py
element_at
def element_at(col, extraction): """ Collection function: Returns element of array at given index in extraction if col is array. Returns value for the given key in extraction if col is map. :param col: name of column containing array or map :param extraction: index to check for in array or key to check for in map .. note:: The position is not zero based, but 1 based index. >>> df = spark.createDataFrame([(["a", "b", "c"],), ([],)], ['data']) >>> df.select(element_at(df.data, 1)).collect() [Row(element_at(data, 1)=u'a'), Row(element_at(data, 1)=None)] >>> df = spark.createDataFrame([({"a": 1.0, "b": 2.0},), ({},)], ['data']) >>> df.select(element_at(df.data, "a")).collect() [Row(element_at(data, a)=1.0), Row(element_at(data, a)=None)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.element_at(_to_java_column(col), extraction))
python
def element_at(col, extraction): """ Collection function: Returns element of array at given index in extraction if col is array. Returns value for the given key in extraction if col is map. :param col: name of column containing array or map :param extraction: index to check for in array or key to check for in map .. note:: The position is not zero based, but 1 based index. >>> df = spark.createDataFrame([(["a", "b", "c"],), ([],)], ['data']) >>> df.select(element_at(df.data, 1)).collect() [Row(element_at(data, 1)=u'a'), Row(element_at(data, 1)=None)] >>> df = spark.createDataFrame([({"a": 1.0, "b": 2.0},), ({},)], ['data']) >>> df.select(element_at(df.data, "a")).collect() [Row(element_at(data, a)=1.0), Row(element_at(data, a)=None)] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.element_at(_to_java_column(col), extraction))
[ "def", "element_at", "(", "col", ",", "extraction", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "element_at", "(", "_to_java_column", "(", "col", ")", ",", "extraction", ")", ")" ]
Collection function: Returns element of array at given index in extraction if col is array. Returns value for the given key in extraction if col is map. :param col: name of column containing array or map :param extraction: index to check for in array or key to check for in map .. note:: The position is not zero based, but 1 based index. >>> df = spark.createDataFrame([(["a", "b", "c"],), ([],)], ['data']) >>> df.select(element_at(df.data, 1)).collect() [Row(element_at(data, 1)=u'a'), Row(element_at(data, 1)=None)] >>> df = spark.createDataFrame([({"a": 1.0, "b": 2.0},), ({},)], ['data']) >>> df.select(element_at(df.data, "a")).collect() [Row(element_at(data, a)=1.0), Row(element_at(data, a)=None)]
[ "Collection", "function", ":", "Returns", "element", "of", "array", "at", "given", "index", "in", "extraction", "if", "col", "is", "array", ".", "Returns", "value", "for", "the", "given", "key", "in", "extraction", "if", "col", "is", "map", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L2034-L2053
apache/spark
python/pyspark/sql/functions.py
array_remove
def array_remove(col, element): """ Collection function: Remove all elements that equal to element from the given array. :param col: name of column containing array :param element: element to be removed from the array >>> df = spark.createDataFrame([([1, 2, 3, 1, 1],), ([],)], ['data']) >>> df.select(array_remove(df.data, 1)).collect() [Row(array_remove(data, 1)=[2, 3]), Row(array_remove(data, 1)=[])] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.array_remove(_to_java_column(col), element))
python
def array_remove(col, element): """ Collection function: Remove all elements that equal to element from the given array. :param col: name of column containing array :param element: element to be removed from the array >>> df = spark.createDataFrame([([1, 2, 3, 1, 1],), ([],)], ['data']) >>> df.select(array_remove(df.data, 1)).collect() [Row(array_remove(data, 1)=[2, 3]), Row(array_remove(data, 1)=[])] """ sc = SparkContext._active_spark_context return Column(sc._jvm.functions.array_remove(_to_java_column(col), element))
[ "def", "array_remove", "(", "col", ",", "element", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "return", "Column", "(", "sc", ".", "_jvm", ".", "functions", ".", "array_remove", "(", "_to_java_column", "(", "col", ")", ",", "element", ")", ")" ]
Collection function: Remove all elements that equal to element from the given array. :param col: name of column containing array :param element: element to be removed from the array >>> df = spark.createDataFrame([([1, 2, 3, 1, 1],), ([],)], ['data']) >>> df.select(array_remove(df.data, 1)).collect() [Row(array_remove(data, 1)=[2, 3]), Row(array_remove(data, 1)=[])]
[ "Collection", "function", ":", "Remove", "all", "elements", "that", "equal", "to", "element", "from", "the", "given", "array", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L2057-L2069
apache/spark
python/pyspark/sql/functions.py
explode
def explode(col): """ Returns a new row for each element in the given array or map. Uses the default column name `col` for elements in the array and `key` and `value` for elements in the map unless specified otherwise. >>> from pyspark.sql import Row >>> eDF = spark.createDataFrame([Row(a=1, intlist=[1,2,3], mapfield={"a": "b"})]) >>> eDF.select(explode(eDF.intlist).alias("anInt")).collect() [Row(anInt=1), Row(anInt=2), Row(anInt=3)] >>> eDF.select(explode(eDF.mapfield).alias("key", "value")).show() +---+-----+ |key|value| +---+-----+ | a| b| +---+-----+ """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.explode(_to_java_column(col)) return Column(jc)
python
def explode(col): """ Returns a new row for each element in the given array or map. Uses the default column name `col` for elements in the array and `key` and `value` for elements in the map unless specified otherwise. >>> from pyspark.sql import Row >>> eDF = spark.createDataFrame([Row(a=1, intlist=[1,2,3], mapfield={"a": "b"})]) >>> eDF.select(explode(eDF.intlist).alias("anInt")).collect() [Row(anInt=1), Row(anInt=2), Row(anInt=3)] >>> eDF.select(explode(eDF.mapfield).alias("key", "value")).show() +---+-----+ |key|value| +---+-----+ | a| b| +---+-----+ """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.explode(_to_java_column(col)) return Column(jc)
[ "def", "explode", "(", "col", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "explode", "(", "_to_java_column", "(", "col", ")", ")", "return", "Column", "(", "jc", ")" ]
Returns a new row for each element in the given array or map. Uses the default column name `col` for elements in the array and `key` and `value` for elements in the map unless specified otherwise. >>> from pyspark.sql import Row >>> eDF = spark.createDataFrame([Row(a=1, intlist=[1,2,3], mapfield={"a": "b"})]) >>> eDF.select(explode(eDF.intlist).alias("anInt")).collect() [Row(anInt=1), Row(anInt=2), Row(anInt=3)] >>> eDF.select(explode(eDF.mapfield).alias("key", "value")).show() +---+-----+ |key|value| +---+-----+ | a| b| +---+-----+
[ "Returns", "a", "new", "row", "for", "each", "element", "in", "the", "given", "array", "or", "map", ".", "Uses", "the", "default", "column", "name", "col", "for", "elements", "in", "the", "array", "and", "key", "and", "value", "for", "elements", "in", "the", "map", "unless", "specified", "otherwise", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L2144-L2164
apache/spark
python/pyspark/sql/functions.py
get_json_object
def get_json_object(col, path): """ Extracts json object from a json string based on json path specified, and returns json string of the extracted json object. It will return null if the input json string is invalid. :param col: string column in json format :param path: path to the json object to extract >>> data = [("1", '''{"f1": "value1", "f2": "value2"}'''), ("2", '''{"f1": "value12"}''')] >>> df = spark.createDataFrame(data, ("key", "jstring")) >>> df.select(df.key, get_json_object(df.jstring, '$.f1').alias("c0"), \\ ... get_json_object(df.jstring, '$.f2').alias("c1") ).collect() [Row(key=u'1', c0=u'value1', c1=u'value2'), Row(key=u'2', c0=u'value12', c1=None)] """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.get_json_object(_to_java_column(col), path) return Column(jc)
python
def get_json_object(col, path): """ Extracts json object from a json string based on json path specified, and returns json string of the extracted json object. It will return null if the input json string is invalid. :param col: string column in json format :param path: path to the json object to extract >>> data = [("1", '''{"f1": "value1", "f2": "value2"}'''), ("2", '''{"f1": "value12"}''')] >>> df = spark.createDataFrame(data, ("key", "jstring")) >>> df.select(df.key, get_json_object(df.jstring, '$.f1').alias("c0"), \\ ... get_json_object(df.jstring, '$.f2').alias("c1") ).collect() [Row(key=u'1', c0=u'value1', c1=u'value2'), Row(key=u'2', c0=u'value12', c1=None)] """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.get_json_object(_to_java_column(col), path) return Column(jc)
[ "def", "get_json_object", "(", "col", ",", "path", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "get_json_object", "(", "_to_java_column", "(", "col", ")", ",", "path", ")", "return", "Column", "(", "jc", ")" ]
Extracts json object from a json string based on json path specified, and returns json string of the extracted json object. It will return null if the input json string is invalid. :param col: string column in json format :param path: path to the json object to extract >>> data = [("1", '''{"f1": "value1", "f2": "value2"}'''), ("2", '''{"f1": "value12"}''')] >>> df = spark.createDataFrame(data, ("key", "jstring")) >>> df.select(df.key, get_json_object(df.jstring, '$.f1').alias("c0"), \\ ... get_json_object(df.jstring, '$.f2').alias("c1") ).collect() [Row(key=u'1', c0=u'value1', c1=u'value2'), Row(key=u'2', c0=u'value12', c1=None)]
[ "Extracts", "json", "object", "from", "a", "json", "string", "based", "on", "json", "path", "specified", "and", "returns", "json", "string", "of", "the", "extracted", "json", "object", ".", "It", "will", "return", "null", "if", "the", "input", "json", "string", "is", "invalid", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L2264-L2280
apache/spark
python/pyspark/sql/functions.py
json_tuple
def json_tuple(col, *fields): """Creates a new row for a json column according to the given field names. :param col: string column in json format :param fields: list of fields to extract >>> data = [("1", '''{"f1": "value1", "f2": "value2"}'''), ("2", '''{"f1": "value12"}''')] >>> df = spark.createDataFrame(data, ("key", "jstring")) >>> df.select(df.key, json_tuple(df.jstring, 'f1', 'f2')).collect() [Row(key=u'1', c0=u'value1', c1=u'value2'), Row(key=u'2', c0=u'value12', c1=None)] """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.json_tuple(_to_java_column(col), _to_seq(sc, fields)) return Column(jc)
python
def json_tuple(col, *fields): """Creates a new row for a json column according to the given field names. :param col: string column in json format :param fields: list of fields to extract >>> data = [("1", '''{"f1": "value1", "f2": "value2"}'''), ("2", '''{"f1": "value12"}''')] >>> df = spark.createDataFrame(data, ("key", "jstring")) >>> df.select(df.key, json_tuple(df.jstring, 'f1', 'f2')).collect() [Row(key=u'1', c0=u'value1', c1=u'value2'), Row(key=u'2', c0=u'value12', c1=None)] """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.json_tuple(_to_java_column(col), _to_seq(sc, fields)) return Column(jc)
[ "def", "json_tuple", "(", "col", ",", "*", "fields", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "json_tuple", "(", "_to_java_column", "(", "col", ")", ",", "_to_seq", "(", "sc", ",", "fields", ")", ")", "return", "Column", "(", "jc", ")" ]
Creates a new row for a json column according to the given field names. :param col: string column in json format :param fields: list of fields to extract >>> data = [("1", '''{"f1": "value1", "f2": "value2"}'''), ("2", '''{"f1": "value12"}''')] >>> df = spark.createDataFrame(data, ("key", "jstring")) >>> df.select(df.key, json_tuple(df.jstring, 'f1', 'f2')).collect() [Row(key=u'1', c0=u'value1', c1=u'value2'), Row(key=u'2', c0=u'value12', c1=None)]
[ "Creates", "a", "new", "row", "for", "a", "json", "column", "according", "to", "the", "given", "field", "names", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L2285-L2298
apache/spark
python/pyspark/sql/functions.py
from_json
def from_json(col, schema, options={}): """ Parses a column containing a JSON string into a :class:`MapType` with :class:`StringType` as keys type, :class:`StructType` or :class:`ArrayType` with the specified schema. Returns `null`, in the case of an unparseable string. :param col: string column in json format :param schema: a StructType or ArrayType of StructType to use when parsing the json column. :param options: options to control parsing. accepts the same options as the json datasource .. note:: Since Spark 2.3, the DDL-formatted string or a JSON format string is also supported for ``schema``. >>> from pyspark.sql.types import * >>> data = [(1, '''{"a": 1}''')] >>> schema = StructType([StructField("a", IntegerType())]) >>> df = spark.createDataFrame(data, ("key", "value")) >>> df.select(from_json(df.value, schema).alias("json")).collect() [Row(json=Row(a=1))] >>> df.select(from_json(df.value, "a INT").alias("json")).collect() [Row(json=Row(a=1))] >>> df.select(from_json(df.value, "MAP<STRING,INT>").alias("json")).collect() [Row(json={u'a': 1})] >>> data = [(1, '''[{"a": 1}]''')] >>> schema = ArrayType(StructType([StructField("a", IntegerType())])) >>> df = spark.createDataFrame(data, ("key", "value")) >>> df.select(from_json(df.value, schema).alias("json")).collect() [Row(json=[Row(a=1)])] >>> schema = schema_of_json(lit('''{"a": 0}''')) >>> df.select(from_json(df.value, schema).alias("json")).collect() [Row(json=Row(a=None))] >>> data = [(1, '''[1, 2, 3]''')] >>> schema = ArrayType(IntegerType()) >>> df = spark.createDataFrame(data, ("key", "value")) >>> df.select(from_json(df.value, schema).alias("json")).collect() [Row(json=[1, 2, 3])] """ sc = SparkContext._active_spark_context if isinstance(schema, DataType): schema = schema.json() elif isinstance(schema, Column): schema = _to_java_column(schema) jc = sc._jvm.functions.from_json(_to_java_column(col), schema, options) return Column(jc)
python
def from_json(col, schema, options={}): """ Parses a column containing a JSON string into a :class:`MapType` with :class:`StringType` as keys type, :class:`StructType` or :class:`ArrayType` with the specified schema. Returns `null`, in the case of an unparseable string. :param col: string column in json format :param schema: a StructType or ArrayType of StructType to use when parsing the json column. :param options: options to control parsing. accepts the same options as the json datasource .. note:: Since Spark 2.3, the DDL-formatted string or a JSON format string is also supported for ``schema``. >>> from pyspark.sql.types import * >>> data = [(1, '''{"a": 1}''')] >>> schema = StructType([StructField("a", IntegerType())]) >>> df = spark.createDataFrame(data, ("key", "value")) >>> df.select(from_json(df.value, schema).alias("json")).collect() [Row(json=Row(a=1))] >>> df.select(from_json(df.value, "a INT").alias("json")).collect() [Row(json=Row(a=1))] >>> df.select(from_json(df.value, "MAP<STRING,INT>").alias("json")).collect() [Row(json={u'a': 1})] >>> data = [(1, '''[{"a": 1}]''')] >>> schema = ArrayType(StructType([StructField("a", IntegerType())])) >>> df = spark.createDataFrame(data, ("key", "value")) >>> df.select(from_json(df.value, schema).alias("json")).collect() [Row(json=[Row(a=1)])] >>> schema = schema_of_json(lit('''{"a": 0}''')) >>> df.select(from_json(df.value, schema).alias("json")).collect() [Row(json=Row(a=None))] >>> data = [(1, '''[1, 2, 3]''')] >>> schema = ArrayType(IntegerType()) >>> df = spark.createDataFrame(data, ("key", "value")) >>> df.select(from_json(df.value, schema).alias("json")).collect() [Row(json=[1, 2, 3])] """ sc = SparkContext._active_spark_context if isinstance(schema, DataType): schema = schema.json() elif isinstance(schema, Column): schema = _to_java_column(schema) jc = sc._jvm.functions.from_json(_to_java_column(col), schema, options) return Column(jc)
[ "def", "from_json", "(", "col", ",", "schema", ",", "options", "=", "{", "}", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "if", "isinstance", "(", "schema", ",", "DataType", ")", ":", "schema", "=", "schema", ".", "json", "(", ")", "elif", "isinstance", "(", "schema", ",", "Column", ")", ":", "schema", "=", "_to_java_column", "(", "schema", ")", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "from_json", "(", "_to_java_column", "(", "col", ")", ",", "schema", ",", "options", ")", "return", "Column", "(", "jc", ")" ]
Parses a column containing a JSON string into a :class:`MapType` with :class:`StringType` as keys type, :class:`StructType` or :class:`ArrayType` with the specified schema. Returns `null`, in the case of an unparseable string. :param col: string column in json format :param schema: a StructType or ArrayType of StructType to use when parsing the json column. :param options: options to control parsing. accepts the same options as the json datasource .. note:: Since Spark 2.3, the DDL-formatted string or a JSON format string is also supported for ``schema``. >>> from pyspark.sql.types import * >>> data = [(1, '''{"a": 1}''')] >>> schema = StructType([StructField("a", IntegerType())]) >>> df = spark.createDataFrame(data, ("key", "value")) >>> df.select(from_json(df.value, schema).alias("json")).collect() [Row(json=Row(a=1))] >>> df.select(from_json(df.value, "a INT").alias("json")).collect() [Row(json=Row(a=1))] >>> df.select(from_json(df.value, "MAP<STRING,INT>").alias("json")).collect() [Row(json={u'a': 1})] >>> data = [(1, '''[{"a": 1}]''')] >>> schema = ArrayType(StructType([StructField("a", IntegerType())])) >>> df = spark.createDataFrame(data, ("key", "value")) >>> df.select(from_json(df.value, schema).alias("json")).collect() [Row(json=[Row(a=1)])] >>> schema = schema_of_json(lit('''{"a": 0}''')) >>> df.select(from_json(df.value, schema).alias("json")).collect() [Row(json=Row(a=None))] >>> data = [(1, '''[1, 2, 3]''')] >>> schema = ArrayType(IntegerType()) >>> df = spark.createDataFrame(data, ("key", "value")) >>> df.select(from_json(df.value, schema).alias("json")).collect() [Row(json=[1, 2, 3])]
[ "Parses", "a", "column", "containing", "a", "JSON", "string", "into", "a", ":", "class", ":", "MapType", "with", ":", "class", ":", "StringType", "as", "keys", "type", ":", "class", ":", "StructType", "or", ":", "class", ":", "ArrayType", "with", "the", "specified", "schema", ".", "Returns", "null", "in", "the", "case", "of", "an", "unparseable", "string", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L2303-L2347
apache/spark
python/pyspark/sql/functions.py
schema_of_json
def schema_of_json(json, options={}): """ Parses a JSON string and infers its schema in DDL format. :param json: a JSON string or a string literal containing a JSON string. :param options: options to control parsing. accepts the same options as the JSON datasource .. versionchanged:: 3.0 It accepts `options` parameter to control schema inferring. >>> df = spark.range(1) >>> df.select(schema_of_json(lit('{"a": 0}')).alias("json")).collect() [Row(json=u'struct<a:bigint>')] >>> schema = schema_of_json('{a: 1}', {'allowUnquotedFieldNames':'true'}) >>> df.select(schema.alias("json")).collect() [Row(json=u'struct<a:bigint>')] """ if isinstance(json, basestring): col = _create_column_from_literal(json) elif isinstance(json, Column): col = _to_java_column(json) else: raise TypeError("schema argument should be a column or string") sc = SparkContext._active_spark_context jc = sc._jvm.functions.schema_of_json(col, options) return Column(jc)
python
def schema_of_json(json, options={}): """ Parses a JSON string and infers its schema in DDL format. :param json: a JSON string or a string literal containing a JSON string. :param options: options to control parsing. accepts the same options as the JSON datasource .. versionchanged:: 3.0 It accepts `options` parameter to control schema inferring. >>> df = spark.range(1) >>> df.select(schema_of_json(lit('{"a": 0}')).alias("json")).collect() [Row(json=u'struct<a:bigint>')] >>> schema = schema_of_json('{a: 1}', {'allowUnquotedFieldNames':'true'}) >>> df.select(schema.alias("json")).collect() [Row(json=u'struct<a:bigint>')] """ if isinstance(json, basestring): col = _create_column_from_literal(json) elif isinstance(json, Column): col = _to_java_column(json) else: raise TypeError("schema argument should be a column or string") sc = SparkContext._active_spark_context jc = sc._jvm.functions.schema_of_json(col, options) return Column(jc)
[ "def", "schema_of_json", "(", "json", ",", "options", "=", "{", "}", ")", ":", "if", "isinstance", "(", "json", ",", "basestring", ")", ":", "col", "=", "_create_column_from_literal", "(", "json", ")", "elif", "isinstance", "(", "json", ",", "Column", ")", ":", "col", "=", "_to_java_column", "(", "json", ")", "else", ":", "raise", "TypeError", "(", "\"schema argument should be a column or string\"", ")", "sc", "=", "SparkContext", ".", "_active_spark_context", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "schema_of_json", "(", "col", ",", "options", ")", "return", "Column", "(", "jc", ")" ]
Parses a JSON string and infers its schema in DDL format. :param json: a JSON string or a string literal containing a JSON string. :param options: options to control parsing. accepts the same options as the JSON datasource .. versionchanged:: 3.0 It accepts `options` parameter to control schema inferring. >>> df = spark.range(1) >>> df.select(schema_of_json(lit('{"a": 0}')).alias("json")).collect() [Row(json=u'struct<a:bigint>')] >>> schema = schema_of_json('{a: 1}', {'allowUnquotedFieldNames':'true'}) >>> df.select(schema.alias("json")).collect() [Row(json=u'struct<a:bigint>')]
[ "Parses", "a", "JSON", "string", "and", "infers", "its", "schema", "in", "DDL", "format", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L2393-L2419
apache/spark
python/pyspark/sql/functions.py
schema_of_csv
def schema_of_csv(csv, options={}): """ Parses a CSV string and infers its schema in DDL format. :param col: a CSV string or a string literal containing a CSV string. :param options: options to control parsing. accepts the same options as the CSV datasource >>> df = spark.range(1) >>> df.select(schema_of_csv(lit('1|a'), {'sep':'|'}).alias("csv")).collect() [Row(csv=u'struct<_c0:int,_c1:string>')] >>> df.select(schema_of_csv('1|a', {'sep':'|'}).alias("csv")).collect() [Row(csv=u'struct<_c0:int,_c1:string>')] """ if isinstance(csv, basestring): col = _create_column_from_literal(csv) elif isinstance(csv, Column): col = _to_java_column(csv) else: raise TypeError("schema argument should be a column or string") sc = SparkContext._active_spark_context jc = sc._jvm.functions.schema_of_csv(col, options) return Column(jc)
python
def schema_of_csv(csv, options={}): """ Parses a CSV string and infers its schema in DDL format. :param col: a CSV string or a string literal containing a CSV string. :param options: options to control parsing. accepts the same options as the CSV datasource >>> df = spark.range(1) >>> df.select(schema_of_csv(lit('1|a'), {'sep':'|'}).alias("csv")).collect() [Row(csv=u'struct<_c0:int,_c1:string>')] >>> df.select(schema_of_csv('1|a', {'sep':'|'}).alias("csv")).collect() [Row(csv=u'struct<_c0:int,_c1:string>')] """ if isinstance(csv, basestring): col = _create_column_from_literal(csv) elif isinstance(csv, Column): col = _to_java_column(csv) else: raise TypeError("schema argument should be a column or string") sc = SparkContext._active_spark_context jc = sc._jvm.functions.schema_of_csv(col, options) return Column(jc)
[ "def", "schema_of_csv", "(", "csv", ",", "options", "=", "{", "}", ")", ":", "if", "isinstance", "(", "csv", ",", "basestring", ")", ":", "col", "=", "_create_column_from_literal", "(", "csv", ")", "elif", "isinstance", "(", "csv", ",", "Column", ")", ":", "col", "=", "_to_java_column", "(", "csv", ")", "else", ":", "raise", "TypeError", "(", "\"schema argument should be a column or string\"", ")", "sc", "=", "SparkContext", ".", "_active_spark_context", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "schema_of_csv", "(", "col", ",", "options", ")", "return", "Column", "(", "jc", ")" ]
Parses a CSV string and infers its schema in DDL format. :param col: a CSV string or a string literal containing a CSV string. :param options: options to control parsing. accepts the same options as the CSV datasource >>> df = spark.range(1) >>> df.select(schema_of_csv(lit('1|a'), {'sep':'|'}).alias("csv")).collect() [Row(csv=u'struct<_c0:int,_c1:string>')] >>> df.select(schema_of_csv('1|a', {'sep':'|'}).alias("csv")).collect() [Row(csv=u'struct<_c0:int,_c1:string>')]
[ "Parses", "a", "CSV", "string", "and", "infers", "its", "schema", "in", "DDL", "format", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L2424-L2446
apache/spark
python/pyspark/sql/functions.py
to_csv
def to_csv(col, options={}): """ Converts a column containing a :class:`StructType` into a CSV string. Throws an exception, in the case of an unsupported type. :param col: name of column containing a struct. :param options: options to control converting. accepts the same options as the CSV datasource. >>> from pyspark.sql import Row >>> data = [(1, Row(name='Alice', age=2))] >>> df = spark.createDataFrame(data, ("key", "value")) >>> df.select(to_csv(df.value).alias("csv")).collect() [Row(csv=u'2,Alice')] """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.to_csv(_to_java_column(col), options) return Column(jc)
python
def to_csv(col, options={}): """ Converts a column containing a :class:`StructType` into a CSV string. Throws an exception, in the case of an unsupported type. :param col: name of column containing a struct. :param options: options to control converting. accepts the same options as the CSV datasource. >>> from pyspark.sql import Row >>> data = [(1, Row(name='Alice', age=2))] >>> df = spark.createDataFrame(data, ("key", "value")) >>> df.select(to_csv(df.value).alias("csv")).collect() [Row(csv=u'2,Alice')] """ sc = SparkContext._active_spark_context jc = sc._jvm.functions.to_csv(_to_java_column(col), options) return Column(jc)
[ "def", "to_csv", "(", "col", ",", "options", "=", "{", "}", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "jc", "=", "sc", ".", "_jvm", ".", "functions", ".", "to_csv", "(", "_to_java_column", "(", "col", ")", ",", "options", ")", "return", "Column", "(", "jc", ")" ]
Converts a column containing a :class:`StructType` into a CSV string. Throws an exception, in the case of an unsupported type. :param col: name of column containing a struct. :param options: options to control converting. accepts the same options as the CSV datasource. >>> from pyspark.sql import Row >>> data = [(1, Row(name='Alice', age=2))] >>> df = spark.createDataFrame(data, ("key", "value")) >>> df.select(to_csv(df.value).alias("csv")).collect() [Row(csv=u'2,Alice')]
[ "Converts", "a", "column", "containing", "a", ":", "class", ":", "StructType", "into", "a", "CSV", "string", ".", "Throws", "an", "exception", "in", "the", "case", "of", "an", "unsupported", "type", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/functions.py#L2451-L2468