Skip to content
Snippets Groups Projects
Commit 89562a17 authored by Yanbo Liang's avatar Yanbo Liang Committed by Davies Liu
Browse files

[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.
parent 42047577
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
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