spark-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject spark git commit: [SPARK-7544] [SQL] [PySpark] pyspark.sql.types.Row implements __getitem__
Date Thu, 10 Sep 2015 20:54:24 GMT
Repository: spark
Updated Branches:
  refs/heads/master 420475771 -> 89562a172


[SPARK-7544] [SQL] [PySpark] pyspark.sql.types.Row implements __getitem__

pyspark.sql.types.Row implements ```__getitem__```

Author: Yanbo Liang <ybliang8@gmail.com>

Closes #8333 from yanboliang/spark-7544.


Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/89562a17
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/89562a17
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/89562a17

Branch: refs/heads/master
Commit: 89562a172fd3efa032f60714d600407c6cfe2c2f
Parents: 4204757
Author: Yanbo Liang <ybliang8@gmail.com>
Authored: Thu Sep 10 13:54:20 2015 -0700
Committer: Davies Liu <davies.liu@gmail.com>
Committed: Thu Sep 10 13:54:20 2015 -0700

----------------------------------------------------------------------
 python/pyspark/sql/types.py | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/89562a17/python/pyspark/sql/types.py
----------------------------------------------------------------------
diff --git a/python/pyspark/sql/types.py b/python/pyspark/sql/types.py
index 8bd58d6..1f86894 100644
--- a/python/pyspark/sql/types.py
+++ b/python/pyspark/sql/types.py
@@ -1176,6 +1176,8 @@ class Row(tuple):
     >>> row = Row(name="Alice", age=11)
     >>> row
     Row(age=11, name='Alice')
+    >>> row['name'], row['age']
+    ('Alice', 11)
     >>> row.name, row.age
     ('Alice', 11)
 
@@ -1243,6 +1245,19 @@ class Row(tuple):
         """create new Row object"""
         return _create_row(self, args)
 
+    def __getitem__(self, item):
+        if isinstance(item, (int, slice)):
+            return super(Row, self).__getitem__(item)
+        try:
+            # it will be slow when it has many fields,
+            # but this will not be used in normal cases
+            idx = self.__fields__.index(item)
+            return super(Row, self).__getitem__(idx)
+        except IndexError:
+            raise KeyError(item)
+        except ValueError:
+            raise ValueError(item)
+
     def __getattr__(self, item):
         if item.startswith("__"):
             raise AttributeError(item)


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org


Mime
View raw message