Skip to content
Snippets Groups Projects
Commit 74a6541a authored by Davies Liu's avatar Davies Liu Committed by Davies Liu
Browse files

[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
parent 106c0789
No related branches found
No related tags found
No related merge requests found
...@@ -1197,13 +1197,36 @@ class Row(tuple): ...@@ -1197,13 +1197,36 @@ class Row(tuple):
else: else:
raise ValueError("No args or kwargs") raise ValueError("No args or kwargs")
def asDict(self): def asDict(self, recursive=False):
""" """
Return as an dict 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__"): if not hasattr(self, "__fields__"):
raise TypeError("Cannot convert a Row class into dict") 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 # let object acts like class
def __call__(self, *args): def __call__(self, *args):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment