Skip to content

Commit 2d010f7

Browse files
yhuairxin
authored andcommitted
[SPARK-7060][SQL] Add alias function to python dataframe
This pr tries to provide a way to let python users workaround https://issues.apache.org/jira/browse/SPARK-6231. Author: Yin Huai <[email protected]> Closes apache#5634 from yhuai/pythonDFAlias and squashes the following commits: 8465acd [Yin Huai] Add an alias to a Python DF.
1 parent 336f7f5 commit 2d010f7

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

python/pyspark/sql/dataframe.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,20 @@ def columns(self):
452452
"""
453453
return [f.name for f in self.schema.fields]
454454

455+
@ignore_unicode_prefix
456+
def alias(self, alias):
457+
"""Returns a new :class:`DataFrame` with an alias set.
458+
459+
>>> from pyspark.sql.functions import *
460+
>>> df_as1 = df.alias("df_as1")
461+
>>> df_as2 = df.alias("df_as2")
462+
>>> joined_df = df_as1.join(df_as2, col("df_as1.name") == col("df_as2.name"), 'inner')
463+
>>> joined_df.select(col("df_as1.name"), col("df_as2.name"), col("df_as2.age")).collect()
464+
[Row(name=u'Alice', name=u'Alice', age=2), Row(name=u'Bob', name=u'Bob', age=5)]
465+
"""
466+
assert isinstance(alias, basestring), "alias should be a string"
467+
return DataFrame(getattr(self._jdf, "as")(alias), self.sql_ctx)
468+
455469
@ignore_unicode_prefix
456470
def join(self, other, joinExprs=None, joinType=None):
457471
"""Joins with another :class:`DataFrame`, using the given join expression.

0 commit comments

Comments
 (0)