diff options
author | Maciej Brynski <maciej.brynski@adpilot.pl> | 2016-05-11 13:15:11 -0700 |
---|---|---|
committer | Reynold Xin <rxin@databricks.com> | 2016-05-11 13:15:11 -0700 |
commit | 7ecd496884f6f126ab186b9ceaa861a571d6155c (patch) | |
tree | fa0e489627b9a2b37b963c9a5a3075e2e760569f /python/pyspark/sql | |
parent | bb88ad4e0e870c88d474c71939a19541522a3023 (diff) | |
download | spark-7ecd496884f6f126ab186b9ceaa861a571d6155c.tar.gz spark-7ecd496884f6f126ab186b9ceaa861a571d6155c.tar.bz2 spark-7ecd496884f6f126ab186b9ceaa861a571d6155c.zip |
[SPARK-12200][SQL] Add __contains__ implementation to Row
https://issues.apache.org/jira/browse/SPARK-12200
Author: Maciej Brynski <maciej.brynski@adpilot.pl>
Author: Maciej BryĆski <maciek-github@brynski.pl>
Closes #10194 from maver1ck/master.
Diffstat (limited to 'python/pyspark/sql')
-rw-r--r-- | python/pyspark/sql/types.py | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/python/pyspark/sql/types.py b/python/pyspark/sql/types.py index f7cd4b80ca..30ab130f29 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""" |