Skip to content

Commit

Permalink
[SPARK-4051] [SQL] [PySpark] Convert Row into dictionary
Browse files Browse the repository at this point in the history
Added a method to Row to turn row into dict:

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

Author: Davies Liu <[email protected]>

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

8d97366 [Davies Liu] convert Row into dict
  • Loading branch information
Davies Liu authored and JoshRosen committed Oct 24, 2014
1 parent d2987e8 commit d60a9d4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions python/pyspark/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,10 @@ class Row(tuple):
# 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))
Expand Down Expand Up @@ -1466,6 +1470,14 @@ def __new__(self, *args, **kwargs):
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"""
Expand Down
9 changes: 9 additions & 0 deletions python/pyspark/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,15 @@ def test_serialize_nested_array_and_map(self):
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):

Expand Down

0 comments on commit d60a9d4

Please sign in to comment.