aboutsummaryrefslogtreecommitdiff
path: root/python/pyspark/sql/types.py
diff options
context:
space:
mode:
authorDavies Liu <davies@databricks.com>2015-08-08 08:36:14 -0700
committerDavies Liu <davies.liu@gmail.com>2015-08-08 08:36:14 -0700
commit74a6541aa82bcd7a052b2e57b5ca55b7c316495b (patch)
tree4ec60a3296a09cdf628fe4e0e2b87db85f55a94a /python/pyspark/sql/types.py
parent106c0789d8c83c7081bc9a335df78ba728e95872 (diff)
downloadspark-74a6541aa82bcd7a052b2e57b5ca55b7c316495b.tar.gz
spark-74a6541aa82bcd7a052b2e57b5ca55b7c316495b.tar.bz2
spark-74a6541aa82bcd7a052b2e57b5ca55b7c316495b.zip
[SPARK-4561] [PYSPARK] [SQL] turn Row into dict recursively
Add an option `recursive` to `Row.asDict()`, when True (default is False), it will convert the nested Row into dict. Author: Davies Liu <davies@databricks.com> Closes #8006 from davies/as_dict and squashes the following commits: 922cc5a [Davies Liu] turn Row into dict recursively
Diffstat (limited to 'python/pyspark/sql/types.py')
-rw-r--r--python/pyspark/sql/types.py27
1 files changed, 25 insertions, 2 deletions
diff --git a/python/pyspark/sql/types.py b/python/pyspark/sql/types.py
index 6f74b7162f..e2e6f03ae9 100644
--- a/python/pyspark/sql/types.py
+++ b/python/pyspark/sql/types.py
@@ -1197,13 +1197,36 @@ class Row(tuple):
else:
raise ValueError("No args or kwargs")
- def asDict(self):
+ def asDict(self, recursive=False):
"""
Return as an dict
+
+ :param recursive: turns the nested Row as dict (default: False).
+
+ >>> Row(name="Alice", age=11).asDict() == {'name': 'Alice', 'age': 11}
+ True
+ >>> row = Row(key=1, value=Row(name='a', age=2))
+ >>> row.asDict() == {'key': 1, 'value': Row(age=2, name='a')}
+ True
+ >>> row.asDict(True) == {'key': 1, 'value': {'name': 'a', 'age': 2}}
+ True
"""
if not hasattr(self, "__fields__"):
raise TypeError("Cannot convert a Row class into dict")
- return dict(zip(self.__fields__, self))
+
+ if recursive:
+ def conv(obj):
+ if isinstance(obj, Row):
+ return obj.asDict(True)
+ elif isinstance(obj, list):
+ return [conv(o) for o in obj]
+ elif isinstance(obj, dict):
+ return dict((k, conv(v)) for k, v in obj.items())
+ else:
+ return obj
+ return dict(zip(self.__fields__, (conv(o) for o in self)))
+ else:
+ return dict(zip(self.__fields__, self))
# let object acts like class
def __call__(self, *args):