-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-20090][PYTHON] Add StructType.fieldNames in PySpark #18618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -445,9 +445,12 @@ class StructType(DataType): | |
|
|
||
| This is the data type representing a :class:`Row`. | ||
|
|
||
| Iterating a :class:`StructType` will iterate its :class:`StructField`s. | ||
| Iterating a :class:`StructType` will iterate its :class:`StructField`\\s. | ||
| A contained :class:`StructField` can be accessed by name or position. | ||
|
|
||
| .. note:: `names` attribute is deprecated in 2.3. Use `fieldNames` method instead | ||
| to get a list of field names. | ||
|
|
||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @holdenk, would you maybe still prefer to deprecate it? I am willing to follow your decision.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is good enough :) |
||
| >>> struct1 = StructType([StructField("f1", StringType(), True)]) | ||
| >>> struct1["f1"] | ||
| StructField(f1,StringType,true) | ||
|
|
@@ -562,6 +565,16 @@ def jsonValue(self): | |
| def fromJson(cls, json): | ||
| return StructType([StructField.fromJson(f) for f in json["fields"]]) | ||
|
|
||
| def fieldNames(self): | ||
| """ | ||
| Returns all field names in a list. | ||
|
|
||
| >>> struct = StructType([StructField("f1", StringType(), True)]) | ||
| >>> struct.fieldNames() | ||
| ['f1'] | ||
| """ | ||
| return list(self.names) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to note that this >>> df = spark.range(1)
>>> a = df.schema.fieldNames()
>>> b = df.schema.names
>>> df.schema.names[0] = "a"
>>> a
['id']
>>> b
['a']
>>> a[0] = "aaaa"
>>> a
['aaaa']
>>> b
['a'] |
||
|
|
||
| def needConversion(self): | ||
| # We need convert Row()/namedtuple into tuple() | ||
| return True | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before
After
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank's for fixing the documentation issue while you were here :) +1