Repository: spark Updated Branches: refs/heads/master bb88ad4e0 -> 7ecd49688
[SPARK-12200][SQL] Add __contains__ implementation to Row https://issues.apache.org/jira/browse/SPARK-12200 Author: Maciej Brynski <maciej.bryn...@adpilot.pl> Author: Maciej BryÅski <maciek-git...@brynski.pl> Closes #10194 from maver1ck/master. Project: http://git-wip-us.apache.org/repos/asf/spark/repo Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/7ecd4968 Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/7ecd4968 Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/7ecd4968 Branch: refs/heads/master Commit: 7ecd496884f6f126ab186b9ceaa861a571d6155c Parents: bb88ad4 Author: Maciej Brynski <maciej.bryn...@adpilot.pl> Authored: Wed May 11 13:15:11 2016 -0700 Committer: Reynold Xin <r...@databricks.com> Committed: Wed May 11 13:15:11 2016 -0700 ---------------------------------------------------------------------- python/pyspark/sql/types.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/spark/blob/7ecd4968/python/pyspark/sql/types.py ---------------------------------------------------------------------- diff --git a/python/pyspark/sql/types.py b/python/pyspark/sql/types.py index f7cd4b8..30ab130 100644 --- a/python/pyspark/sql/types.py +++ b/python/pyspark/sql/types.py @@ -1359,7 +1359,13 @@ def _create_row(fields, values): class Row(tuple): """ - A row in L{DataFrame}. The fields in it can be accessed like attributes. + A row in L{DataFrame}. + The fields in it can be accessed: + + * like attributes (``row.key``) + * like dictionary values (``row[key]``) + + ``key in row`` will search through row keys. Row can be used to create a row object by using named arguments, the fields will be sorted by names. @@ -1371,6 +1377,10 @@ class Row(tuple): ('Alice', 11) >>> row.name, row.age ('Alice', 11) + >>> 'name' in row + True + >>> 'wrong_key' in row + False Row also can be used to create another Row like class, then it could be used to create Row objects, such as @@ -1378,6 +1388,10 @@ class Row(tuple): >>> Person = Row("name", "age") >>> Person <Row(name, age)> + >>> 'name' in Person + True + >>> 'wrong_key' in Person + False >>> Person("Alice", 11) Row(name='Alice', age=11) """ @@ -1431,6 +1445,12 @@ class Row(tuple): else: return dict(zip(self.__fields__, self)) + def __contains__(self, item): + if hasattr(self, "__fields__"): + return item in self.__fields__ + else: + return super(Row, self).__contains__(item) + # let object acts like class def __call__(self, *args): """create new Row object""" --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org For additional commands, e-mail: commits-h...@spark.apache.org