Repository: spark
Updated Branches:
  refs/heads/master d2987e8f7 -> d60a9d440


[SPARK-4051] [SQL] [PySpark] Convert Row into dictionary

Added a method to Row to turn row into dict:

```
>>> row = Row(a=1)
>>> row.asDict()
{'a': 1}
```

Author: Davies Liu <[email protected]>

Closes #2896 from davies/dict and squashes the following commits:

8d97366 [Davies Liu] convert Row into dict


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

Branch: refs/heads/master
Commit: d60a9d440b00beb107c1f1d7f42886c94f04a092
Parents: d2987e8
Author: Davies Liu <[email protected]>
Authored: Fri Oct 24 10:48:03 2014 -0700
Committer: Josh Rosen <[email protected]>
Committed: Fri Oct 24 10:48:03 2014 -0700

----------------------------------------------------------------------
 python/pyspark/sql.py   | 12 ++++++++++++
 python/pyspark/tests.py |  9 +++++++++
 2 files changed, 21 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/d60a9d44/python/pyspark/sql.py
----------------------------------------------------------------------
diff --git a/python/pyspark/sql.py b/python/pyspark/sql.py
index b31a82f..7daf306 100644
--- a/python/pyspark/sql.py
+++ b/python/pyspark/sql.py
@@ -883,6 +883,10 @@ def _create_cls(dataType):
         # create property for fast access
         locals().update(_create_properties(dataType.fields))
 
+        def asDict(self):
+            """ Return as a dict """
+            return dict(zip(self.__FIELDS__, self))
+
         def __repr__(self):
             # call collect __repr__ for nested objects
             return ("Row(%s)" % ", ".join("%s=%r" % (n, getattr(self, n))
@@ -1466,6 +1470,14 @@ class Row(tuple):
         else:
             raise ValueError("No args or kwargs")
 
+    def asDict(self):
+        """
+        Return as an dict
+        """
+        if not hasattr(self, "__FIELDS__"):
+            raise TypeError("Cannot convert a Row class into dict")
+        return dict(zip(self.__FIELDS__, self))
+
     # let obect acs like class
     def __call__(self, *args):
         """create new Row object"""

http://git-wip-us.apache.org/repos/asf/spark/blob/d60a9d44/python/pyspark/tests.py
----------------------------------------------------------------------
diff --git a/python/pyspark/tests.py b/python/pyspark/tests.py
index 7a2107e..047d857 100644
--- a/python/pyspark/tests.py
+++ b/python/pyspark/tests.py
@@ -771,6 +771,15 @@ class SQLTests(ReusedPySparkTestCase):
         self.assertEqual(1.0, row.c)
         self.assertEqual("2", row.d)
 
+    def test_convert_row_to_dict(self):
+        row = Row(l=[Row(a=1, b='s')], d={"key": Row(c=1.0, d="2")})
+        self.assertEqual(1, row.asDict()['l'][0].a)
+        rdd = self.sc.parallelize([row])
+        srdd = self.sqlCtx.inferSchema(rdd)
+        srdd.registerTempTable("test")
+        row = self.sqlCtx.sql("select l[0].a AS la from test").first()
+        self.assertEqual(1, row.asDict()["la"])
+
 
 class InputFormatTests(ReusedPySparkTestCase):
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to