pySpark 中文API (2)

pyspark.sql模块

模块上下文

Spark SQL和DataFrames的重要类:

pyspark.sql.SparkSession 主要入口点DataFrame和SQL功能。

pyspark.sql.DataFrame 分组到已命名列中的分布式数据集合。

pyspark.sql.Column a中的列表达式DataFrame

pyspark.sql.Row a中的一行数据DataFrame

pyspark.sql.GroupedData 聚合方法,由返回DataFrame.groupBy()

pyspark.sql.DataFrameNaFunctions 处理缺失数据的方法(空值)。

pyspark.sql.DataFrameStatFunctions 统计功能的方法。

pyspark.sql.functions 可用的内置功能列表DataFrame

pyspark.sql.types 可用数据类型列表。

pyspark.sql.Window 用于处理窗口函数。

pyspark.sql.SparkSession(sparkContextjsparkSession = None [source]

使用数据集和DataFrame API编程Spark的入口点。

SparkSession可用于创建DataFrame,注册DataFrame为表格,在表格上执行SQL,缓存表格以及读取实木复合地板文件。要创建SparkSession,请使用以下构建器模式:

>>> spark = SparkSession 。建设者 \

... 。主(“本地” ) \

... 。APPNAME (“字数” ) \

... 。配置(“spark.some.config.option” ,“某些价值” ) \

... 。getOrCreate ()

builder

一个具有a Builder构造SparkSession实例的类属性

class Builder[source]

建设者SparkSession

appName(名称[来源]

设置应用程序的名称,该名称将显示在Spark Web UI中。

如果未设置应用程序名称,则会使用随机生成的名称。

参数:名称 - 应用程序名称

2.0版本中的新功能。

config(key = Nonevalue = Noneconf = None [source]

设置一个配置选项。使用此方法设置的选项会自动传播到两个SparkConf和SparkSession自己的配置。

对于现有的SparkConf,请使用conf参数。

>>> 从pyspark.conf 导入SparkConf >>> SparkSession 。建设者。config (conf = SparkConf ())

对于(键,值)对,可以省略参数名称。

>>> SparkSession 。建设者。config (“spark.some.config.option” ,“some-value” )

参数: - 配置属性的键名字符串

 - 配置属性的值

conf - 一个实例SparkConf

2.0版本中的新功能。

enableHiveSupport()[source]

启用Hive支持,包括连接到持续Hive Metastore,支持Hive serdes和Hive用户定义的功能。

2.0版本中的新功能。

getOrCreate()[source]

获取现有的SparkSession或者,如果没有现有的,则根据此构建器中设置的选项创建一个新的。

此方法首先检查是否存在有效的全局默认SparkSession,如果是,则返回该值。如果没有有效的全局默认SparkSession存在,则该方法创建一个新的SparkSession并将新创建的SparkSession指定为全局默认值。

>>> s1=SparkSession.builder.config("k1","v1").getOrCreate()>>> s1.conf.get("k1")==s1.sparkContext.getConf().get("k1")=="v1"True

In case an existing SparkSession is returned, the config options specified in this builder will be applied to the existing SparkSession.

>>> s2=SparkSession.builder.config("k2","v2").getOrCreate()>>> s1.conf.get("k1")==s2.conf.get("k1")True>>> s1.conf.get("k2")==s2.conf.get("k2")True

New in version 2.0.

master(master)[source]

Sets the Spark master URL to connect to, such as “local” to run locally, “local[4]” to run locally with 4 cores, or “spark://master:7077” to run on a Spark standalone cluster.

Parameters:master – a url for spark master

New in version 2.0.

catalog

Interface through which the user may create, drop, alter or query underlying databases, tables, functions etc.

Returns:Catalog

New in version 2.0.

conf

Runtime configuration interface for Spark.

This is the interface through which the user can get and set all Spark and Hadoop configurations that are relevant to Spark SQL. When getting the value of a config, this defaults to the value set in the underlying SparkContext, if any.

New in version 2.0.

createDataFrame(dataschema=NonesamplingRatio=NoneverifySchema=True)[source]

Creates a DataFrame from an RDD, a list or a pandas.DataFrame.

When schema is a list of column names, the type of each column will be inferred from data.

When schema is None, it will try to infer the schema (column names and types) from data, which should be an RDD of Row, or namedtuple, or dict.

When schema is pyspark.sql.types.DataType or a datatype string, it must match the real data, or an exception will be thrown at runtime. If the given schema is not pyspark.sql.types.StructType, it will be wrapped into a pyspark.sql.types.StructType as its only field, and the field name will be “value”, each record will also be wrapped into a tuple, which can be converted to row later.

If schema inference is needed, samplingRatio is used to determined the ratio of rows used for schema inference. The first row will be used if samplingRatio is None.

Parameters:data – an RDD of any kind of SQL data representation(e.g. row, tuple, int, boolean, etc.), or list, or pandas.DataFrame.

schema – a pyspark.sql.types.DataType or a datatype string or a list of column names, default is None. The data type string format equals to pyspark.sql.types.DataType.simpleString, except that top level struct type can omit the struct<> and atomic types use typeName() as their format, e.g. use byte instead of tinyint for pyspark.sql.types.ByteType. We can also use int as a short name for IntegerType.

samplingRatio – the sample ratio of rows used for inferring

verifySchema – verify data types of every row against schema.

Returns:DataFrame

Changed in version 2.1: Added verifySchema.

Note


Usage with spark.sql.execution.arrow.enabled=True is experimental.

>>> l=[('Alice',1)]>>> spark.createDataFrame(l).collect()[Row(_1=u'Alice', _2=1)]>>> spark.createDataFrame(l,['name','age']).collect()[Row(name=u'Alice', age=1)]

>>> d=[{'name':'Alice','age':1}]>>> spark.createDataFrame(d).collect()[Row(age=1, name=u'Alice')]

>>> rdd=sc.parallelize(l)>>> spark.createDataFrame(rdd).collect()[Row(_1=u'Alice', _2=1)]>>> df=spark.createDataFrame(rdd,['name','age'])>>> df.collect()[Row(name=u'Alice', age=1)]

>>> frompyspark.sqlimportRow>>> Person=Row('name','age')>>> person=rdd.map(lambdar:Person(*r))>>> df2=spark.createDataFrame(person)>>> df2.collect()[Row(name=u'Alice', age=1)]

>>> frompyspark.sql.typesimport*>>> schema=StructType([... StructField("name",StringType(),True),... StructField("age",IntegerType(),True)])>>> df3=spark.createDataFrame(rdd,schema)>>> df3.collect()[Row(name=u'Alice', age=1)]

>>> spark.createDataFrame(df.toPandas()).collect()[Row(name=u'Alice', age=1)]>>> spark.createDataFrame(pandas.DataFrame([[1,2]])).collect()[Row(0=1, 1=2)]

>>> spark.createDataFrame(rdd,"a: string, b: int").collect()[Row(a=u'Alice', b=1)]>>> rdd=rdd.map(lambdarow:row[1])>>> spark.createDataFrame(rdd,"int").collect()[Row(value=1)]>>> spark.createDataFrame(rdd,"boolean").collect()Traceback (most recent call last):...Py4JJavaError:...

New in version 2.0.

newSession()[source]

Returns a new SparkSession as new session, that has separate SQLConf, registered temporary views and UDFs, but shared SparkContext and table cache.

New in version 2.0.

range(startend=Nonestep=1numPartitions=None)[source]

Create a DataFrame with single pyspark.sql.types.LongType column named id, containing elements in a range from start to end (exclusive) with step value step.

Parameters:start – the start value

end – the end value (exclusive)

step – the incremental step (default: 1)

numPartitions – the number of partitions of the DataFrame

Returns:DataFrame

>>> spark.range(1,7,2).collect()[Row(id=1), Row(id=3), Row(id=5)]

If only one argument is specified, it will be used as the end value.

>>> spark.range(3).collect()[Row(id=0), Row(id=1), Row(id=2)]

New in version 2.0.

read

Returns a DataFrameReader that can be used to read data in as a DataFrame.

Returns:DataFrameReader

New in version 2.0.

readStream

Returns a DataStreamReader that can be used to read data streams as a streaming DataFrame.

Note


Evolving.

Returns:DataStreamReader

New in version 2.0.

sparkContext

Returns the underlying SparkContext.

New in version 2.0.

sql(sqlQuery)[source]

Returns a DataFrame representing the result of the given query.

Returns:DataFrame

>>> df.createOrReplaceTempView("table1")>>> df2=spark.sql("SELECT field1 AS f1, field2 as f2 from table1")>>> df2.collect()[Row(f1=1, f2=u'row1'), Row(f1=2, f2=u'row2'), Row(f1=3, f2=u'row3')]

New in version 2.0.

stop()[source]

Stop the underlying SparkContext.

New in version 2.0.

streams

Returns a StreamingQueryManager that allows managing all the StreamingQuery StreamingQueries active on this context.

Note


Evolving.

Returns:StreamingQueryManager

New in version 2.0.

table(tableName)[source]

Returns the specified table as a DataFrame.

Returns:DataFrame

>>> df.createOrReplaceTempView("table1")>>> df2=spark.table("table1")>>> sorted(df.collect())==sorted(df2.collect())True

New in version 2.0.

udf

Returns a UDFRegistration for UDF registration.

Returns:UDFRegistration

New in version 2.0.

version

The version of Spark on which this application is running.

New in version 2.0.

class pyspark.sql.SQLContext(sparkContextsparkSession=NonejsqlContext=None)[source]

The entry point for working with structured data (rows and columns) in Spark, in Spark 1.x.

As of Spark 2.0, this is replaced by SparkSession. However, we are keeping the class here for backward compatibility.

A SQLContext can be used create DataFrame, register DataFrame as tables, execute SQL over tables, cache tables, and read parquet files.

Parameters:sparkContext – The SparkContext backing this SQLContext.

sparkSession – The SparkSession around which this SQLContext wraps.

jsqlContext – An optional JVM Scala SQLContext. If set, we do not instantiate a new SQLContext in the JVM, instead we make all calls to this object.

cacheTable(tableName)[source]

Caches the specified table in-memory.

New in version 1.0.

clearCache()[source]

Removes all cached tables from the in-memory cache.

New in version 1.3.

createDataFrame(dataschema=NonesamplingRatio=NoneverifySchema=True)[source]

Creates a DataFrame from an RDD, a list or a pandas.DataFrame.

When schema is a list of column names, the type of each column will be inferred from data.

When schema is None, it will try to infer the schema (column names and types) from data, which should be an RDD of Row, or namedtuple, or dict.

When schema is pyspark.sql.types.DataType or a datatype string it must match the real data, or an exception will be thrown at runtime. If the given schema is not pyspark.sql.types.StructType, it will be wrapped into a pyspark.sql.types.StructType as its only field, and the field name will be “value”, each record will also be wrapped into a tuple, which can be converted to row later.

If schema inference is needed, samplingRatio is used to determined the ratio of rows used for schema inference. The first row will be used if samplingRatio is None.

Parameters:data – an RDD of any kind of SQL data representation(e.g. Row, tuple, int, boolean, etc.), or list, or pandas.DataFrame.

schema – a pyspark.sql.types.DataType or a datatype string or a list of column names, default is None. The data type string format equals to pyspark.sql.types.DataType.simpleString, except that top level struct type can omit the struct<> and atomic types use typeName() as their format, e.g. use byte instead of tinyint for pyspark.sql.types.ByteType. We can also use int as a short name for pyspark.sql.types.IntegerType.

samplingRatio – the sample ratio of rows used for inferring

verifySchema – verify data types of every row against schema.

Returns:DataFrame

Changed in version 2.0: The schema parameter can be a pyspark.sql.types.DataType or a datatype string after 2.0. If it’s not a pyspark.sql.types.StructType, it will be wrapped into a pyspark.sql.types.StructType and each record will also be wrapped into a tuple.

Changed in version 2.1: Added verifySchema.

>>> l=[('Alice',1)]>>> sqlContext.createDataFrame(l).collect()[Row(_1=u'Alice', _2=1)]>>> sqlContext.createDataFrame(l,['name','age']).collect()[Row(name=u'Alice', age=1)]

>>> d=[{'name':'Alice','age':1}]>>> sqlContext.createDataFrame(d).collect()[Row(age=1, name=u'Alice')]

>>> rdd=sc.parallelize(l)>>> sqlContext.createDataFrame(rdd).collect()[Row(_1=u'Alice', _2=1)]>>> df=sqlContext.createDataFrame(rdd,['name','age'])>>> df.collect()[Row(name=u'Alice', age=1)]

>>> frompyspark.sqlimportRow>>> Person=Row('name','age')>>> person=rdd.map(lambdar:Person(*r))>>> df2=sqlContext.createDataFrame(person)>>> df2.collect()[Row(name=u'Alice', age=1)]

>>> frompyspark.sql.typesimport*>>> schema=StructType([... StructField("name",StringType(),True),... StructField("age",IntegerType(),True)])>>> df3=sqlContext.createDataFrame(rdd,schema)>>> df3.collect()[Row(name=u'Alice', age=1)]

>>> sqlContext.createDataFrame(df.toPandas()).collect()[Row(name=u'Alice', age=1)]>>> sqlContext.createDataFrame(pandas.DataFrame([[1,2]])).collect()[Row(0=1, 1=2)]

>>> sqlContext.createDataFrame(rdd,"a: string, b: int").collect()[Row(a=u'Alice', b=1)]>>> rdd=rdd.map(lambdarow:row[1])>>> sqlContext.createDataFrame(rdd,"int").collect()[Row(value=1)]>>> sqlContext.createDataFrame(rdd,"boolean").collect()Traceback (most recent call last):...Py4JJavaError:...

New in version 1.3.

createExternalTable(tableNamepath=Nonesource=Noneschema=None**options)[source]

Creates an external table based on the dataset in a data source.

It returns the DataFrame associated with the external table.

The data source is specified by the source and a set of options. If source is not specified, the default data source configured byspark.sql.sources.default will be used.

Optionally, a schema can be provided as the schema of the returned DataFrame and created external table.

Returns:DataFrame

New in version 1.3.

dropTempTable(tableName)[source]

Remove the temp table from catalog.

>>> sqlContext.registerDataFrameAsTable(df,"table1")>>> sqlContext.dropTempTable("table1")

New in version 1.6.

getConf(keydefaultValue=)[source]

Returns the value of Spark SQL configuration property for the given key.

If the key is not set and defaultValue is set, return defaultValue. If the key is not set and defaultValue is not set, return the system default value.

>>> sqlContext.getConf("spark.sql.shuffle.partitions")u'200'>>> sqlContext.getConf("spark.sql.shuffle.partitions",u"10")u'10'>>> sqlContext.setConf("spark.sql.shuffle.partitions",u"50")>>> sqlContext.getConf("spark.sql.shuffle.partitions",u"10")u'50'

New in version 1.3.

classmethod getOrCreate(sc)[source]

Get the existing SQLContext or create a new one with given SparkContext.

Parameters:sc – SparkContext

New in version 1.6.

newSession()[source]

Returns a new SQLContext as new session, that has separate SQLConf, registered temporary views and UDFs, but shared SparkContext and table cache.

New in version 1.6.

range(startend=Nonestep=1numPartitions=None)[source]

Create a DataFrame with single pyspark.sql.types.LongType column named id, containing elements in a range from start to end (exclusive) with step value step.

Parameters:start – the start value

end – the end value (exclusive)

step – the incremental step (default: 1)

numPartitions – the number of partitions of the DataFrame

Returns:DataFrame

>>> sqlContext.range(1,7,2).collect()[Row(id=1), Row(id=3), Row(id=5)]

If only one argument is specified, it will be used as the end value.

>>> sqlContext.range(3).collect()[Row(id=0), Row(id=1), Row(id=2)]

New in version 1.4.

read

Returns a DataFrameReader that can be used to read data in as a DataFrame.

Returns:DataFrameReader

New in version 1.4.

readStream

Returns a DataStreamReader that can be used to read data streams as a streaming DataFrame.

Note


Evolving.

Returns:DataStreamReader

>>> text_sdf=sqlContext.readStream.text(tempfile.mkdtemp())>>> text_sdf.isStreamingTrue

New in version 2.0.

registerDataFrameAsTable(dftableName)[source]

Registers the given DataFrame as a temporary table in the catalog.

Temporary tables exist only during the lifetime of this instance of SQLContext.

>>> sqlContext.registerDataFrameAsTable(df,"table1")

New in version 1.3.

registerFunction(namefreturnType=None)[source]

An alias for spark.udf.register(). See pyspark.sql.UDFRegistration.register().

Note


Deprecated in 2.3.0. Use spark.udf.register() instead.

New in version 1.2.

registerJavaFunction(namejavaClassNamereturnType=None)[source]

An alias for spark.udf.registerJavaFunction(). See pyspark.sql.UDFRegistration.registerJavaFunction().

Note


Deprecated in 2.3.0. Use spark.udf.registerJavaFunction() instead.

New in version 2.1.

setConf(keyvalue)[source]

Sets the given Spark SQL configuration property.

New in version 1.3.

sql(sqlQuery)[source]

Returns a DataFrame representing the result of the given query.

Returns:DataFrame

>>> sqlContext.registerDataFrameAsTable(df,"table1")>>> df2=sqlContext.sql("SELECT field1 AS f1, field2 as f2 from table1")>>> df2.collect()[Row(f1=1, f2=u'row1'), Row(f1=2, f2=u'row2'), Row(f1=3, f2=u'row3')]

New in version 1.0.

streams

Returns a StreamingQueryManager that allows managing all the StreamingQuery StreamingQueries active on this context.

Note


Evolving.

New in version 2.0.

table(tableName)[source]

Returns the specified table or view as a DataFrame.

Returns:DataFrame

>>> sqlContext.registerDataFrameAsTable(df,"table1")>>> df2=sqlContext.table("table1")>>> sorted(df.collect())==sorted(df2.collect())True

New in version 1.0.

tableNames(dbName=None)[source]

Returns a list of names of tables in the database dbName.

Parameters:dbName – string, name of the database to use. Default to the current database.

Returns:list of table names, in string

>>> sqlContext.registerDataFrameAsTable(df,"table1")>>> "table1"insqlContext.tableNames()True>>> "table1"insqlContext.tableNames("default")True

New in version 1.3.

tables(dbName=None)[source]

Returns a DataFrame containing names of tables in the given database.

If dbName is not specified, the current database will be used.

The returned DataFrame has two columns: tableName and isTemporary (a column with BooleanType indicating if a table is a temporary one or not).

Parameters:dbName – string, name of the database to use.

Returns:DataFrame

>>> sqlContext.registerDataFrameAsTable(df,"table1")>>> df2=sqlContext.tables()>>> df2.filter("tableName = 'table1'").first()Row(database=u'', tableName=u'table1', isTemporary=True)

New in version 1.3.

udf

Returns a UDFRegistration for UDF registration.

Returns:UDFRegistration

New in version 1.3.1.

uncacheTable(tableName)[source]

Removes the specified table from the in-memory cache.

New in version 1.0.

class pyspark.sql.HiveContext(sparkContextjhiveContext=None)[source]

A variant of Spark SQL that integrates with data stored in Hive.

Configuration for Hive is read from hive-site.xml on the classpath. It supports running both SQL and HiveQL commands.

Parameters:sparkContext – The SparkContext to wrap.

jhiveContext – An optional JVM Scala HiveContext. If set, we do not instantiate a new HiveContext in the JVM, instead we make all calls to this object.

Note


Deprecated in 2.0.0. Use SparkSession.builder.enableHiveSupport().getOrCreate().

refreshTable(tableName)[source]

Invalidate and refresh all the cached the metadata of the given table. For performance reasons, Spark SQL or the external data source library it uses might cache certain metadata about a table, such as the location of blocks. When those change outside of Spark SQL, users should call this function to invalidate the cache.

class pyspark.sql.UDFRegistration(sparkSession)[source]

Wrapper for user-defined function registration. This instance can be accessed by spark.udf or sqlContext.udf.

New in version 1.3.1.

register(namefreturnType=None)[source]

Register a Python function (including lambda function) or a user-defined function as a SQL function.

Parameters:name – name of the user-defined function in SQL statements.

f – a Python function, or a user-defined function. The user-defined function can be either row-at-a-time or vectorized. See pyspark.sql.functions.udf() and pyspark.sql.functions.pandas_udf().

returnType – the return type of the registered user-defined function. The value can be either a pyspark.sql.types.DataType object or a DDL-formatted type string.

Returns:a user-defined function.

To register a nondeterministic Python function, users need to first build a nondeterministic user-defined function for the Python function and then register it as a SQL function.

returnType can be optionally specified when f is a Python function but not when f is a user-defined function. Please see below.

When f is a Python function:

returnType defaults to string type and can be optionally specified. The produced object must match the specified type. In this case, this API works as if register(name, f, returnType=StringType()).

>>> strlen=spark.udf.register("stringLengthString",lambdax:len(x))>>> spark.sql("SELECT stringLengthString('test')").collect()[Row(stringLengthString(test)=u'4')]

>>> spark.sql("SELECT 'foo' AS text").select(strlen("text")).collect()[Row(stringLengthString(text)=u'3')]

>>> frompyspark.sql.typesimportIntegerType>>> _=spark.udf.register("stringLengthInt",lambdax:len(x),IntegerType())>>> spark.sql("SELECT stringLengthInt('test')").collect()[Row(stringLengthInt(test)=4)]

>>> frompyspark.sql.typesimportIntegerType>>> _=spark.udf.register("stringLengthInt",lambdax:len(x),IntegerType())>>> spark.sql("SELECT stringLengthInt('test')").collect()[Row(stringLengthInt(test)=4)]

When f is a user-defined function:

Spark uses the return type of the given user-defined function as the return type of the registered user-defined function. returnTypeshould not be specified. In this case, this API works as if register(name, f).

>>> frompyspark.sql.typesimportIntegerType>>> frompyspark.sql.functionsimportudf>>> slen=udf(lambdas:len(s),IntegerType())>>> _=spark.udf.register("slen",slen)>>> spark.sql("SELECT slen('test')").collect()[Row(slen(test)=4)]

>>> importrandom>>> frompyspark.sql.functionsimportudf>>> frompyspark.sql.typesimportIntegerType>>> random_udf=udf(lambda:random.randint(0,100),IntegerType()).asNondeterministic()>>> new_random_udf=spark.udf.register("random_udf",random_udf)>>> spark.sql("SELECT random_udf()").collect()[Row(random_udf()=82)]

>>> frompyspark.sql.functionsimportpandas_udf,PandasUDFType>>> :pandas_udf("integer",PandasUDFType.SCALAR)... defadd_one(x):... returnx+1...>>> _=spark.udf.register("add_one",add_one)>>> spark.sql("SELECT add_one(id) FROM range(3)").collect()[Row(add_one(id)=1), Row(add_one(id)=2), Row(add_one(id)=3)]

Note


Registration for a user-defined function (case 2.) was added from Spark 2.3.0.

New in version 1.3.1.

registerJavaFunction(namejavaClassNamereturnType=None)[source]

Register a Java user-defined function as a SQL function.

In addition to a name and the function itself, the return type can be optionally specified. When the return type is not specified we would infer it via reflection.

Parameters:name – name of the user-defined function

javaClassName – fully qualified name of java class

returnType – the return type of the registered Java function. The value can be either a pyspark.sql.types.DataType object or a DDL-formatted type string.

>>> frompyspark.sql.typesimportIntegerType>>> spark.udf.registerJavaFunction(... "javaStringLength","test.org.apache.spark.sql.JavaStringLength",IntegerType())>>> spark.sql("SELECT javaStringLength('test')").collect()[Row(UDF:javaStringLength(test)=4)]

>>> spark.udf.registerJavaFunction(... "javaStringLength2","test.org.apache.spark.sql.JavaStringLength")>>> spark.sql("SELECT javaStringLength2('test')").collect()[Row(UDF:javaStringLength2(test)=4)]

>>> spark.udf.registerJavaFunction(... "javaStringLength3","test.org.apache.spark.sql.JavaStringLength","integer")>>> spark.sql("SELECT javaStringLength3('test')").collect()[Row(UDF:javaStringLength3(test)=4)]

New in version 2.3.

registerJavaUDAF(namejavaClassName)[source]

Register a Java user-defined aggregate function as a SQL function.

Parameters:name – name of the user-defined aggregate function

javaClassName – fully qualified name of java class

>>> spark.udf.registerJavaUDAF("javaUDAF","test.org.apache.spark.sql.MyDoubleAvg")>>> df=spark.createDataFrame([(1,"a"),(2,"b"),(3,"a")],["id","name"])>>> df.createOrReplaceTempView("df")>>> spark.sql("SELECT name, javaUDAF(id) as avg from df group by name").collect()[Row(name=u'b', avg=102.0), Row(name=u'a', avg=102.0)]

New in version 2.3.

class pyspark.sql.DataFrame(jdfsql_ctx)[source]

A distributed collection of data grouped into named columns.

DataFrame is equivalent to a relational table in Spark SQL, and can be created using various functions in SparkSession:

people=spark.read.parquet("...")

Once created, it can be manipulated using the various domain-specific-language (DSL) functions defined in: DataFrameColumn.

To select a column from the data frame, use the apply method:

ageCol=people.age

A more concrete example:

# To create DataFrame using SparkSessionpeople=spark.read.parquet("...")department=spark.read.parquet("...")people.filter(people.age>30).join(department,people.deptId==department.id)\.groupBy(department.name,"gender").agg({"salary":"avg","age":"max"})

New in version 1.3.

agg(*exprs)[source]

Aggregate on the entire DataFrame without groups (shorthand for df.groupBy.agg()).

>>> df.agg({"age":"max"}).collect()[Row(max(age)=5)]>>> frompyspark.sqlimportfunctionsasF>>> df.agg(F.min(df.age)).collect()[Row(min(age)=2)]

New in version 1.3.

alias(alias)[source]

Returns a new DataFrame with an alias set.

>>> frompyspark.sql.functionsimport*>>> df_as1=df.alias("df_as1")>>> df_as2=df.alias("df_as2")>>> joined_df=df_as1.join(df_as2,col("df_as1.name")==col("df_as2.name"),'inner')>>> joined_df.select("df_as1.name","df_as2.name","df_as2.age").collect()[Row(name=u'Bob', name=u'Bob', age=5), Row(name=u'Alice', name=u'Alice', age=2)]

New in version 1.3.

approxQuantile(colprobabilitiesrelativeError)[source]

Calculates the approximate quantiles of numerical columns of a DataFrame.

The result of this algorithm has the following deterministic bound: If the DataFrame has N elements and if we request the quantile at probability p up to error err, then the algorithm will return a sample x from the DataFrame so that the exact rank of x is close to (p * N). More precisely,

floor((p - err) * N) <= rank(x) <= ceil((p + err) * N).

This method implements a variation of the Greenwald-Khanna algorithm (with some speed optimizations). The algorithm was first present in [[http://dx.doi.org/10.1145/375663.375670 Space-efficient Online Computation of Quantile Summaries]] by Greenwald and Khanna.

Note that null values will be ignored in numerical columns before calculation. For columns only containing null values, an empty list is returned.

Parameters:col – str, list. Can be a single column name, or a list of names for multiple columns.

probabilities – a list of quantile probabilities Each number must belong to [0, 1]. For example 0 is the minimum, 0.5 is the median, 1 is the maximum.

relativeError – The relative target precision to achieve (>= 0). If set to zero, the exact quantiles are computed, which could be very expensive. Note that values greater than 1 are accepted but give the same result as 1.

Returns:the approximate quantiles at the given probabilities. If the input col is a string, the output is a list of floats. If the input col is a list or tuple of strings, the output is also a list, but each element in it is a list of floats, i.e., the output is a list of list of floats.

Changed in version 2.2: Added support for multiple columns.

New in version 2.0.

cache()[source]

Persists the DataFrame with the default storage level (MEMORY_AND_DISK).

Note


The default storage level has changed to MEMORY_AND_DISK to match Scala in 2.0.

New in version 1.3.

checkpoint(eager=True)[source]

Returns a checkpointed version of this Dataset. Checkpointing can be used to truncate the logical plan of this DataFrame, which is especially useful in iterative algorithms where the plan may grow exponentially. It will be saved to files inside the checkpoint directory set with SparkContext.setCheckpointDir().

Parameters:eager – Whether to checkpoint this DataFrame immediately

Note


Experimental

New in version 2.1.

coalesce(numPartitions)[source]

Returns a new DataFrame that has exactly numPartitions partitions.

Similar to coalesce defined on an RDD, this operation results in a narrow dependency, e.g. if you go from 1000 partitions to 100 partitions, there will not be a shuffle, instead each of the 100 new partitions will claim 10 of the current partitions. If a larger number of partitions is requested, it will stay at the current number of partitions.

However, if you’re doing a drastic coalesce, e.g. to numPartitions = 1, this may result in your computation taking place on fewer nodes than you like (e.g. one node in the case of numPartitions = 1). To avoid this, you can call repartition(). This will add a shuffle step, but means the current upstream partitions will be executed in parallel (per whatever the current partitioning is).

>>> df.coalesce(1).rdd.getNumPartitions()1

New in version 1.4.

colRegex(colName)[source]

Selects column based on the column name specified as a regex and returns it as Column.

Parameters:colName – string, column name specified as a regex.

>>> df=spark.createDataFrame([("a",1),("b",2),("c",3)],["Col1","Col2"])>>> df.select(df.colRegex("`(Col1)?+.+`")).show()+----+|Col2|+----+|  1||  2||  3|+----+

New in version 2.3.

collect()[source]

Returns all the records as a list of Row.

>>> df.collect()[Row(age=2, name=u'Alice'), Row(age=5, name=u'Bob')]

New in version 1.3.

columns

Returns all column names as a list.

>>> df.columns['age', 'name']

New in version 1.3.

corr(col1col2method=None)[source]

Calculates the correlation of two columns of a DataFrame as a double value. Currently only supports the Pearson Correlation Coefficient.DataFrame.corr() and DataFrameStatFunctions.corr() are aliases of each other.

Parameters:col1 – The name of the first column

col2 – The name of the second column

method – The correlation method. Currently only supports “pearson”

New in version 1.4.

count()[source]

Returns the number of rows in this DataFrame.

>>> df.count()2

New in version 1.3.

cov(col1col2)[source]

Calculate the sample covariance for the given columns, specified by their names, as a double value. DataFrame.cov() and DataFrameStatFunctions.cov() are aliases.

Parameters:col1 – The name of the first column

col2 – The name of the second column

New in version 1.4.

createGlobalTempView(name)[source]

Creates a global temporary view with this DataFrame.

The lifetime of this temporary view is tied to this Spark application. throws TempTableAlreadyExistsException, if the view name already exists in the catalog.

>>> df.createGlobalTempView("people")>>> df2=spark.sql("select * from global_temp.people")>>> sorted(df.collect())==sorted(df2.collect())True>>> df.createGlobalTempView("people")Traceback (most recent call last):...AnalysisException:u"Temporary table 'people' already exists;">>> spark.catalog.dropGlobalTempView("people")

New in version 2.1.

createOrReplaceGlobalTempView(name)[source]

Creates or replaces a global temporary view using the given name.

The lifetime of this temporary view is tied to this Spark application.

>>> df.createOrReplaceGlobalTempView("people")>>> df2=df.filter(df.age>3)>>> df2.createOrReplaceGlobalTempView("people")>>> df3=spark.sql("select * from global_temp.people")>>> sorted(df3.collect())==sorted(df2.collect())True>>> spark.catalog.dropGlobalTempView("people")

New in version 2.2.

createOrReplaceTempView(name)[source]

Creates or replaces a local temporary view with this DataFrame.

The lifetime of this temporary table is tied to the SparkSession that was used to create this DataFrame.

>>> df.createOrReplaceTempView("people")>>> df2=df.filter(df.age>3)>>> df2.createOrReplaceTempView("people")>>> df3=spark.sql("select * from people")>>> sorted(df3.collect())==sorted(df2.collect())True>>> spark.catalog.dropTempView("people")

New in version 2.0.

createTempView(name)[source]

Creates a local temporary view with this DataFrame.

The lifetime of this temporary table is tied to the SparkSession that was used to create this DataFrame. throws TempTableAlreadyExistsException, if the view name already exists in the catalog.

>>> df.createTempView("people")>>> df2=spark.sql("select * from people")>>> sorted(df.collect())==sorted(df2.collect())True>>> df.createTempView("people")Traceback (most recent call last):...AnalysisException:u"Temporary table 'people' already exists;">>> spark.catalog.dropTempView("people")

New in version 2.0.

crossJoin(other)[source]

Returns the cartesian product with another DataFrame.

Parameters:other – Right side of the cartesian product.

>>> df.select("age","name").collect()[Row(age=2, name=u'Alice'), Row(age=5, name=u'Bob')]>>> df2.select("name","height").collect()[Row(name=u'Tom', height=80), Row(name=u'Bob', height=85)]>>> df.crossJoin(df2.select("height")).select("age","name","height").collect()[Row(age=2, name=u'Alice', height=80), Row(age=2, name=u'Alice', height=85), Row(age=5, name=u'Bob', height=80), Row(age=5, name=u'Bob', height=85)]

New in version 2.1.

crosstab(col1col2)[source]

Computes a pair-wise frequency table of the given columns. Also known as a contingency table. The number of distinct values for each column should be less than 1e4. At most 1e6 non-zero pair frequencies will be returned. The first column of each row will be the distinct values of col1 and the column names will be the distinct values of col2. The name of the first column will be $col1_$col2. Pairs that have no occurrences will have zero as their counts. DataFrame.crosstab() and DataFrameStatFunctions.crosstab() are aliases.

Parameters:col1 – The name of the first column. Distinct items will make the first item of each row.

col2 – The name of the second column. Distinct items will make the column names of the DataFrame.

New in version 1.4.

cube(*cols)[source]

Create a multi-dimensional cube for the current DataFrame using the specified columns, so we can run aggregation on them.

>>> df.cube("name",df.age).count().orderBy("name","age").show()+-----+----+-----+| name| age|count|+-----+----+-----+| null|null|    2|| null|  2|    1|| null|  5|    1||Alice|null|    1||Alice|  2|    1||  Bob|null|    1||  Bob|  5|    1|+-----+----+-----+

New in version 1.4.

describe(*cols)[source]

Computes basic statistics for numeric and string columns.

This include count, mean, stddev, min, and max. If no columns are given, this function computes statistics for all numerical or string columns.

Note


This function is meant for exploratory data analysis, as we make no guarantee about the backward compatibility of the schema of the resulting DataFrame.

>>> df.describe(['age']).show()+-------+------------------+|summary|              age|+-------+------------------+|  count|                2||  mean|              3.5|| stddev|2.1213203435596424||    min|                2||    max|                5|+-------+------------------+>>> df.describe().show()+-------+------------------+-----+|summary|              age| name|+-------+------------------+-----+|  count|                2|    2||  mean|              3.5| null|| stddev|2.1213203435596424| null||    min|                2|Alice||    max|                5|  Bob|+-------+------------------+-----+

Use summary for expanded statistics and control over which statistics to compute.

New in version 1.3.1.

distinct()[source]

Returns a new DataFrame containing the distinct rows in this DataFrame.

>>> df.distinct().count()2

New in version 1.3.

drop(*cols)[source]

Returns a new DataFrame that drops the specified column. This is a no-op if schema doesn’t contain the given column name(s).

Parameters:cols – a string name of the column to drop, or a Column to drop, or a list of string name of the columns to drop.

>>> df.drop('age').collect()[Row(name=u'Alice'), Row(name=u'Bob')]

>>> df.drop(df.age).collect()[Row(name=u'Alice'), Row(name=u'Bob')]

>>> df.join(df2,df.name==df2.name,'inner').drop(df.name).collect()[Row(age=5, height=85, name=u'Bob')]

>>> df.join(df2,df.name==df2.name,'inner').drop(df2.name).collect()[Row(age=5, name=u'Bob', height=85)]

>>> df.join(df2,'name','inner').drop('age','height').collect()[Row(name=u'Bob')]

New in version 1.4.

dropDuplicates(subset=None)[source]

Return a new DataFrame with duplicate rows removed, optionally only considering certain columns.

For a static batch DataFrame, it just drops duplicate rows. For a streaming DataFrame, it will keep all data across triggers as intermediate state to drop duplicates rows. You can use withWatermark() to limit how late the duplicate data can be and system will accordingly limit the state. In addition, too late data older than watermark will be dropped to avoid any possibility of duplicates.

drop_duplicates() is an alias for dropDuplicates().

>>> frompyspark.sqlimportRow>>> df=sc.parallelize([\... Row(name='Alice',age=5,height=80),\... Row(name='Alice',age=5,height=80),\... Row(name='Alice',age=10,height=80)]).toDF()>>> df.dropDuplicates().show()+---+------+-----+|age|height| name|+---+------+-----+|  5|    80|Alice|| 10|    80|Alice|+---+------+-----+

>>> df.dropDuplicates(['name','height']).show()+---+------+-----+|age|height| name|+---+------+-----+|  5|    80|Alice|+---+------+-----+

New in version 1.4.

drop_duplicates(subset=None)

drop_duplicates() is an alias for dropDuplicates().

New in version 1.4.

dropna(how='any'thresh=Nonesubset=None)[source]

Returns a new DataFrame omitting rows with null values. DataFrame.dropna() and DataFrameNaFunctions.drop() are aliases of each other.

Parameters:how – ‘any’ or ‘all’. If ‘any’, drop a row if it contains any nulls. If ‘all’, drop a row only if all its values are null.

thresh – int, default None If specified, drop rows that have less than thresh non-null values. This overwrites the how parameter.

subset – optional list of column names to consider.

>>> df4.na.drop().show()+---+------+-----+|age|height| name|+---+------+-----+| 10|    80|Alice|+---+------+-----+

New in version 1.3.1.

dtypes

Returns all column names and their data types as a list.

>>> df.dtypes[('age', 'int'), ('name', 'string')]

New in version 1.3.

explain(extended=False)[source]

Prints the (logical and physical) plans to the console for debugging purpose.

Parameters:extended – boolean, default False. If False, prints only the physical plan.

>>> df.explain()== Physical Plan ==Scan ExistingRDD[age#0,name#1]

>>> df.explain(True)== Parsed Logical Plan ==...== Analyzed Logical Plan ==...== Optimized Logical Plan ==...== Physical Plan ==...

New in version 1.3.

fillna(valuesubset=None)[source]

Replace null values, alias for na.fill(). DataFrame.fillna() and DataFrameNaFunctions.fill() are aliases of each other.

Parameters:value – int, long, float, string, bool or dict. Value to replace null values with. If the value is a dict, then subset is ignored and valuemust be a mapping from column name (string) to replacement value. The replacement value must be an int, long, float, boolean, or string.

subset – optional list of column names to consider. Columns specified in subset that do not have matching data type are ignored. For example, if value is a string, and subset contains a non-string column, then the non-string column is simply ignored.

>>> df4.na.fill(50).show()+---+------+-----+|age|height| name|+---+------+-----+| 10|    80|Alice||  5|    50|  Bob|| 50|    50|  Tom|| 50|    50| null|+---+------+-----+

>>> df5.na.fill(False).show()+----+-------+-----+| age|  name|  spy|+----+-------+-----+|  10|  Alice|false||  5|    Bob|false||null|Mallory| true|+----+-------+-----+

>>> df4.na.fill({'age':50,'name':'unknown'}).show()+---+------+-------+|age|height|  name|+---+------+-------+| 10|    80|  Alice||  5|  null|    Bob|| 50|  null|    Tom|| 50|  null|unknown|+---+------+-------+

New in version 1.3.1.

filter(condition)[source]

Filters rows using the given condition.

where() is an alias for filter().

Parameters:condition – a Column of types.BooleanType or a string of SQL expression.

>>> df.filter(df.age>3).collect()[Row(age=5, name=u'Bob')]>>> df.where(df.age==2).collect()[Row(age=2, name=u'Alice')]

>>> df.filter("age > 3").collect()[Row(age=5, name=u'Bob')]>>> df.where("age = 2").collect()[Row(age=2, name=u'Alice')]

New in version 1.3.

first()[source]

Returns the first row as a Row.

>>> df.first()Row(age=2, name=u'Alice')

New in version 1.3.

foreach(f)[source]

Applies the f function to all Row of this DataFrame.

This is a shorthand for df.rdd.foreach().

>>> deff(person):... print(person.name)>>> df.foreach(f)

New in version 1.3.

foreachPartition(f)[source]

Applies the f function to each partition of this DataFrame.

This a shorthand for df.rdd.foreachPartition().

>>> deff(people):... forpersoninpeople:... print(person.name)>>> df.foreachPartition(f)

New in version 1.3.

freqItems(colssupport=None)[source]

Finding frequent items for columns, possibly with false positives. Using the frequent element count algorithm described in “http://dx.doi.org/10.1145/762471.762473, proposed by Karp, Schenker, and Papadimitriou”. DataFrame.freqItems() and DataFrameStatFunctions.freqItems() are aliases.

Note


This function is meant for exploratory data analysis, as we make no guarantee about the backward compatibility of the schema of the resulting DataFrame.

Parameters:cols – Names of the columns to calculate frequent items for as a list or tuple of strings.

support – The frequency with which to consider an item ‘frequent’. Default is 1%. The support must be greater than 1e-4.

New in version 1.4.

groupBy(*cols)[source]

Groups the DataFrame using the specified columns, so we can run aggregation on them. See GroupedData for all the available aggregate functions.

groupby() is an alias for groupBy().

Parameters:cols – list of columns to group by. Each element should be a column name (string) or an expression (Column).

>>> df.groupBy().avg().collect()[Row(avg(age)=3.5)]>>> sorted(df.groupBy('name').agg({'age':'mean'}).collect())[Row(name=u'Alice', avg(age)=2.0), Row(name=u'Bob', avg(age)=5.0)]>>> sorted(df.groupBy(df.name).avg().collect())[Row(name=u'Alice', avg(age)=2.0), Row(name=u'Bob', avg(age)=5.0)]>>> sorted(df.groupBy(['name',df.age]).count().collect())[Row(name=u'Alice', age=2, count=1), Row(name=u'Bob', age=5, count=1)]

New in version 1.3.

groupby(*cols)

groupby() is an alias for groupBy().

New in version 1.4.

head(n=None)[source]

Returns the first n rows.

Note


This method should only be used if the resulting array is expected to be small, as all the data is loaded into the driver’s memory.

Parameters:n – int, default 1. Number of rows to return.

Returns:If n is greater than 1, return a list of Row. If n is 1, return a single Row.

>>> df.head()Row(age=2, name=u'Alice')>>> df.head(1)[Row(age=2, name=u'Alice')]

New in version 1.3.

hint(name*parameters)[source]

Specifies some hint on the current DataFrame.

Parameters:name – A name of the hint.

parameters – Optional parameters.

Returns:DataFrame

>>> df.join(df2.hint("broadcast"),"name").show()+----+---+------+|name|age|height|+----+---+------+| Bob|  5|    85|+----+---+------+

New in version 2.2.

intersect(other)[source]

Return a new DataFrame containing rows only in both this frame and another frame.

This is equivalent to INTERSECT in SQL.

New in version 1.3.

isLocal()[source]

Returns True if the collect() and take() methods can be run locally (without any Spark executors).

New in version 1.3.

isStreaming

Returns true if this Dataset contains one or more sources that continuously return data as it arrives. A Dataset that reads data from a streaming source must be executed as a StreamingQuery using the start() method in DataStreamWriter. Methods that return a single answer, (e.g., count() or collect()) will throw an AnalysisException when there is a streaming source present.

Note


Evolving

New in version 2.0.

join(otheron=Nonehow=None)[source]

Joins with another DataFrame, using the given join expression.

Parameters:other – Right side of the join

on – a string for the join column name, a list of column names, a join expression (Column), or a list of Columns. If on is a string or a list of strings indicating the name of the join column(s), the column(s) must exist on both sides, and this performs an equi-join.

how – str, default inner. Must be one of: inner, cross, outer, full, full_outer, left, left_outer, right, right_outer, left_semi, and left_anti.

The following performs a full outer join between df1 and df2.

>>> df.join(df2,df.name==df2.name,'outer').select(df.name,df2.height).collect()[Row(name=None, height=80), Row(name=u'Bob', height=85), Row(name=u'Alice', height=None)]

>>> df.join(df2,'name','outer').select('name','height').collect()[Row(name=u'Tom', height=80), Row(name=u'Bob', height=85), Row(name=u'Alice', height=None)]

>>> cond=[df.name==df3.name,df.age==df3.age]>>> df.join(df3,cond,'outer').select(df.name,df3.age).collect()[Row(name=u'Alice', age=2), Row(name=u'Bob', age=5)]

>>> df.join(df2,'name').select(df.name,df2.height).collect()[Row(name=u'Bob', height=85)]

>>> df.join(df4,['name','age']).select(df.name,df.age).collect()[Row(name=u'Bob', age=5)]

New in version 1.3.

limit(num)[source]

Limits the result count to the number specified.

>>> df.limit(1).collect()[Row(age=2, name=u'Alice')]>>> df.limit(0).collect()[]

New in version 1.3.

localCheckpoint(eager=True)[source]

Returns a locally checkpointed version of this Dataset. Checkpointing can be used to truncate the logical plan of this DataFrame, which is especially useful in iterative algorithms where the plan may grow exponentially. Local checkpoints are stored in the executors using the caching subsystem and therefore they are not reliable.

Parameters:eager – Whether to checkpoint this DataFrame immediately

Note


Experimental

New in version 2.3.

na

Returns a DataFrameNaFunctions for handling missing values.

New in version 1.3.1.

orderBy(*cols**kwargs)

Returns a new DataFrame sorted by the specified column(s).

Parameters:cols – list of Column or column names to sort by.

ascending – boolean or list of boolean (default True). Sort ascending vs. descending. Specify list for multiple sort orders. If a list is specified, length of the list must equal length of the cols.

>>> df.sort(df.age.desc()).collect()[Row(age=5, name=u'Bob'), Row(age=2, name=u'Alice')]>>> df.sort("age",ascending=False).collect()[Row(age=5, name=u'Bob'), Row(age=2, name=u'Alice')]>>> df.orderBy(df.age.desc()).collect()[Row(age=5, name=u'Bob'), Row(age=2, name=u'Alice')]>>> frompyspark.sql.functionsimport*>>> df.sort(asc("age")).collect()[Row(age=2, name=u'Alice'), Row(age=5, name=u'Bob')]>>> df.orderBy(desc("age"),"name").collect()[Row(age=5, name=u'Bob'), Row(age=2, name=u'Alice')]>>> df.orderBy(["age","name"],ascending=[0,1]).collect()[Row(age=5, name=u'Bob'), Row(age=2, name=u'Alice')]

New in version 1.3.

persist(storageLevel=StorageLevel(TrueTrueFalseFalse1))[source]

Sets the storage level to persist the contents of the DataFrame across operations after the first time it is computed. This can only be used to assign a new storage level if the DataFrame does not have a storage level set yet. If no storage level is specified defaults to (MEMORY_AND_DISK).

Note


The default storage level has changed to MEMORY_AND_DISK to match Scala in 2.0.

New in version 1.3.

printSchema()[source]

Prints out the schema in the tree format.

>>> df.printSchema()root |-- age: integer (nullable = true) |-- name: string (nullable = true)

New in version 1.3.

randomSplit(weightsseed=None)[source]

Randomly splits this DataFrame with the provided weights.

Parameters:weights – list of doubles as weights with which to split the DataFrame. Weights will be normalized if they don’t sum up to 1.0.

seed – The seed for sampling.

>>> splits=df4.randomSplit([1.0,2.0],24)>>> splits[0].count()1

>>> splits[1].count()3

New in version 1.4.

rdd

Returns the content as an pyspark.RDD of Row.

New in version 1.3.

registerTempTable(name)[source]

Registers this DataFrame as a temporary table using the given name.

The lifetime of this temporary table is tied to the SparkSession that was used to create this DataFrame.

>>> df.registerTempTable("people")>>> df2=spark.sql("select * from people")>>> sorted(df.collect())==sorted(df2.collect())True>>> spark.catalog.dropTempView("people")

Note


Deprecated in 2.0, use createOrReplaceTempView instead.

New in version 1.3.

repartition(numPartitions*cols)[source]

Returns a new DataFrame partitioned by the given partitioning expressions. The resulting DataFrame is hash partitioned.

numPartitions can be an int to specify the target number of partitions or a Column. If it is a Column, it will be used as the first partitioning column. If not specified, the default number of partitions is used.

Changed in version 1.6: Added optional arguments to specify the partitioning columns. Also made numPartitions optional if partitioning columns are specified.

>>> df.repartition(10).rdd.getNumPartitions()10>>> data=df.union(df).repartition("age")>>> data.show()+---+-----+|age| name|+---+-----+|  5|  Bob||  5|  Bob||  2|Alice||  2|Alice|+---+-----+>>> data=data.repartition(7,"age")>>> data.show()+---+-----+|age| name|+---+-----+|  2|Alice||  5|  Bob||  2|Alice||  5|  Bob|+---+-----+>>> data.rdd.getNumPartitions()7>>> data=data.repartition("name","age")>>> data.show()+---+-----+|age| name|+---+-----+|  5|  Bob||  5|  Bob||  2|Alice||  2|Alice|+---+-----+

New in version 1.3.

replace(to_replacevalue=subset=None)[source]

Returns a new DataFrame replacing a value with another value. DataFrame.replace() and DataFrameNaFunctions.replace() are aliases of each other. Values to_replace and value must have the same type and can only be numerics, booleans, or strings. Value can have None. When replacing, the new value will be cast to the type of the existing column. For numeric replacements all values to be replaced should have unique floating point representation. In case of conflicts (for example with {42: -1, 42.0: 1}) and arbitrary replacement will be used.

Parameters:to_replace – bool, int, long, float, string, list or dict. Value to be replaced. If the value is a dict, then value is ignored or can be omitted, and to_replace must be a mapping between a value and a replacement.

value – bool, int, long, float, string, list or None. The replacement value must be a bool, int, long, float, string or None. If value is a list, value should be of the same length and type as to_replace. If value is a scalar and to_replace is a sequence, then value is used as a replacement for each item in to_replace.

subset – optional list of column names to consider. Columns specified in subset that do not have matching data type are ignored. For example, if value is a string, and subset contains a non-string column, then the non-string column is simply ignored.

>>> df4.na.replace(10,20).show()+----+------+-----+| age|height| name|+----+------+-----+|  20|    80|Alice||  5|  null|  Bob||null|  null|  Tom||null|  null| null|+----+------+-----+

>>> df4.na.replace('Alice',None).show()+----+------+----+| age|height|name|+----+------+----+|  10|    80|null||  5|  null| Bob||null|  null| Tom||null|  null|null|+----+------+----+

>>> df4.na.replace({'Alice':None}).show()+----+------+----+| age|height|name|+----+------+----+|  10|    80|null||  5|  null| Bob||null|  null| Tom||null|  null|null|+----+------+----+

>>> df4.na.replace(['Alice','Bob'],['A','B'],'name').show()+----+------+----+| age|height|name|+----+------+----+|  10|    80|  A||  5|  null|  B||null|  null| Tom||null|  null|null|+----+------+----+

New in version 1.4.

rollup(*cols)[source]

Create a multi-dimensional rollup for the current DataFrame using the specified columns, so we can run aggregation on them.

>>> df.rollup("name",df.age).count().orderBy("name","age").show()+-----+----+-----+| name| age|count|+-----+----+-----+| null|null|    2||Alice|null|    1||Alice|  2|    1||  Bob|null|    1||  Bob|  5|    1|+-----+----+-----+

New in version 1.4.

sample(withReplacement=Nonefraction=Noneseed=None)[source]

Returns a sampled subset of this DataFrame.

Parameters:withReplacement – Sample with replacement or not (default False).

fraction – Fraction of rows to generate, range [0.0, 1.0].

seed – Seed for sampling (default a random seed).

Note


This is not guaranteed to provide exactly the fraction specified of the total count of the given DataFrame.

Note


fraction is required and, withReplacement and seed are optional.

>>> df=spark.range(10)>>> df.sample(0.5,3).count()4>>> df.sample(fraction=0.5,seed=3).count()4>>> df.sample(withReplacement=True,fraction=0.5,seed=3).count()1>>> df.sample(1.0).count()10>>> df.sample(fraction=1.0).count()10>>> df.sample(False,fraction=1.0).count()10

New in version 1.3.

sampleBy(colfractionsseed=None)[source]

Returns a stratified sample without replacement based on the fraction given on each stratum.

Parameters:col – column that defines strata

fractions – sampling fraction for each stratum. If a stratum is not specified, we treat its fraction as zero.

seed – random seed

Returns:a new DataFrame that represents the stratified sample

>>> frompyspark.sql.functionsimportcol>>> dataset=sqlContext.range(0,100).select((col("id")%3).alias("key"))>>> sampled=dataset.sampleBy("key",fractions={0:0.1,1:0.2},seed=0)>>> sampled.groupBy("key").count().orderBy("key").show()+---+-----+|key|count|+---+-----+|  0|    5||  1|    9|+---+-----+

New in version 1.5.

schema

Returns the schema of this DataFrame as a pyspark.sql.types.StructType.

>>> df.schemaStructType(List(StructField(age,IntegerType,true),StructField(name,StringType,true)))

New in version 1.3.

select(*cols)[source]

Projects a set of expressions and returns a new DataFrame.

Parameters:cols – list of column names (string) or expressions (Column). If one of the column names is ‘*’, that column is expanded to include all columns in the current DataFrame.

>>> df.select('*').collect()[Row(age=2, name=u'Alice'), Row(age=5, name=u'Bob')]>>> df.select('name','age').collect()[Row(name=u'Alice', age=2), Row(name=u'Bob', age=5)]>>> df.select(df.name,(df.age+10).alias('age')).collect()[Row(name=u'Alice', age=12), Row(name=u'Bob', age=15)]

New in version 1.3.

selectExpr(*expr)[source]

Projects a set of SQL expressions and returns a new DataFrame.

This is a variant of select() that accepts SQL expressions.

>>> df.selectExpr("age * 2","abs(age)").collect()[Row((age * 2)=4, abs(age)=2), Row((age * 2)=10, abs(age)=5)]

New in version 1.3.

show(n=20truncate=Truevertical=False)[source]

Prints the first n rows to the console.

Parameters:n – Number of rows to show.

truncate – If set to True, truncate strings longer than 20 chars by default. If set to a number greater than one, truncates long strings to length truncate and align cells right.

vertical – If set to True, print output rows vertically (one line per column value).

>>> dfDataFrame[age: int, name: string]>>> df.show()+---+-----+|age| name|+---+-----+|  2|Alice||  5|  Bob|+---+-----+>>> df.show(truncate=3)+---+----+|age|name|+---+----+|  2| Ali||  5| Bob|+---+----+>>> df.show(vertical=True)-RECORD 0----- age  | 2 name | Alice-RECORD 1----- age  | 5 name | Bob

New in version 1.3.

sort(*cols**kwargs)[source]

Returns a new DataFrame sorted by the specified column(s).

Parameters:cols – list of Column or column names to sort by.

ascending – boolean or list of boolean (default True). Sort ascending vs. descending. Specify list for multiple sort orders. If a list is specified, length of the list must equal length of the cols.

>>> df.sort(df.age.desc()).collect()[Row(age=5, name=u'Bob'), Row(age=2, name=u'Alice')]>>> df.sort("age",ascending=False).collect()[Row(age=5, name=u'Bob'), Row(age=2, name=u'Alice')]>>> df.orderBy(df.age.desc()).collect()[Row(age=5, name=u'Bob'), Row(age=2, name=u'Alice')]>>> frompyspark.sql.functionsimport*>>> df.sort(asc("age")).collect()[Row(age=2, name=u'Alice'), Row(age=5, name=u'Bob')]>>> df.orderBy(desc("age"),"name").collect()[Row(age=5, name=u'Bob'), Row(age=2, name=u'Alice')]>>> df.orderBy(["age","name"],ascending=[0,1]).collect()[Row(age=5, name=u'Bob'), Row(age=2, name=u'Alice')]

New in version 1.3.

sortWithinPartitions(*cols**kwargs)[source]

Returns a new DataFrame with each partition sorted by the specified column(s).

Parameters:cols – list of Column or column names to sort by.

ascending – boolean or list of boolean (default True). Sort ascending vs. descending. Specify list for multiple sort orders. If a list is specified, length of the list must equal length of the cols.

>>> df.sortWithinPartitions("age",ascending=False).show()+---+-----+|age| name|+---+-----+|  2|Alice||  5|  Bob|+---+-----+

New in version 1.6.

stat

Returns a DataFrameStatFunctions for statistic functions.

New in version 1.4.

storageLevel

Get the DataFrame’s current storage level.

>>> df.storageLevelStorageLevel(False, False, False, False, 1)>>> df.cache().storageLevelStorageLevel(True, True, False, True, 1)>>> df2.persist(StorageLevel.DISK_ONLY_2).storageLevelStorageLevel(True, False, False, False, 2)

New in version 2.1.

subtract(other)[source]

Return a new DataFrame containing rows in this frame but not in another frame.

This is equivalent to EXCEPT DISTINCT in SQL.

New in version 1.3.

summary(*statistics)[source]

Computes specified statistics for numeric and string columns. Available statistics are: - count - mean - stddev - min - max - arbitrary approximate percentiles specified as a percentage (eg, 75%)

If no statistics are given, this function computes count, mean, stddev, min, approximate quartiles (percentiles at 25%, 50%, and 75%), and max.

Note


This function is meant for exploratory data analysis, as we make no guarantee about the backward compatibility of the schema of the resulting DataFrame.

>>> df.summary().show()+-------+------------------+-----+|summary|              age| name|+-------+------------------+-----+|  count|                2|    2||  mean|              3.5| null|| stddev|2.1213203435596424| null||    min|                2|Alice||    25%|                2| null||    50%|                2| null||    75%|                5| null||    max|                5|  Bob|+-------+------------------+-----+

>>> df.summary("count","min","25%","75%","max").show()+-------+---+-----+|summary|age| name|+-------+---+-----+|  count|  2|    2||    min|  2|Alice||    25%|  2| null||    75%|  5| null||    max|  5|  Bob|+-------+---+-----+

To do a summary for specific columns first select them:

>>> df.select("age","name").summary("count").show()+-------+---+----+|summary|age|name|+-------+---+----+|  count|  2|  2|+-------+---+----+

See also describe for basic statistics.

New in version 2.3.0.

take(num)[source]

Returns the first num rows as a list of Row.

>>> df.take(2)[Row(age=2, name=u'Alice'), Row(age=5, name=u'Bob')]

New in version 1.3.

toDF(*cols)[source]

Returns a new class:DataFrame that with new specified column names

Parameters:cols – list of new column names (string)

>>> df.toDF('f1','f2').collect()[Row(f1=2, f2=u'Alice'), Row(f1=5, f2=u'Bob')]

toJSON(use_unicode=True)[source]

Converts a DataFrame into a RDD of string.

Each row is turned into a JSON document as one element in the returned RDD.

>>> df.toJSON().first()u'{"age":2,"name":"Alice"}'

New in version 1.3.

toLocalIterator()[source]

Returns an iterator that contains all of the rows in this DataFrame. The iterator will consume as much memory as the largest partition in this DataFrame.

>>> list(df.toLocalIterator())[Row(age=2, name=u'Alice'), Row(age=5, name=u'Bob')]

New in version 2.0.

toPandas()[source]

Returns the contents of this DataFrame as Pandas pandas.DataFrame.

This is only available if Pandas is installed and available.

Note


This method should only be used if the resulting Pandas’s DataFrame is expected to be small, as all the data is loaded into the driver’s memory.

Note


Usage with spark.sql.execution.arrow.enabled=True is experimental.

>>> df.toPandas()  age  name0    2  Alice1    5    Bob

New in version 1.3.

union(other)[source]

Return a new DataFrame containing union of rows in this and another frame.

This is equivalent to UNION ALL in SQL. To do a SQL-style set union (that does deduplication of elements), use this function followed by distinct().

Also as standard in SQL, this function resolves columns by position (not by name).

New in version 2.0.

unionAll(other)[source]

Return a new DataFrame containing union of rows in this and another frame.

This is equivalent to UNION ALL in SQL. To do a SQL-style set union (that does deduplication of elements), use this function followed by distinct().

Also as standard in SQL, this function resolves columns by position (not by name).

Note


Deprecated in 2.0, use union() instead.

New in version 1.3.

unionByName(other)[source]

Returns a new DataFrame containing union of rows in this and another frame.

This is different from both UNION ALL and UNION DISTINCT in SQL. To do a SQL-style set union (that does deduplication of elements), use this function followed by distinct().

The difference between this function and union() is that this function resolves columns by name (not by position):

>>> df1=spark.createDataFrame([[1,2,3]],["col0","col1","col2"])>>> df2=spark.createDataFrame([[4,5,6]],["col1","col2","col0"])>>> df1.unionByName(df2).show()+----+----+----+|col0|col1|col2|+----+----+----+|  1|  2|  3||  6|  4|  5|+----+----+----+

New in version 2.3.

unpersist(blocking=False)[source]

Marks the DataFrame as non-persistent, and remove all blocks for it from memory and disk.

Note


blocking default has changed to False to match Scala in 2.0.

New in version 1.3.

where(condition)

where() is an alias for filter().

New in version 1.3.

withColumn(colNamecol)[source]

Returns a new DataFrame by adding a column or replacing the existing column that has the same name.

The column expression must be an expression over this DataFrame; attempting to add a column from some other dataframe will raise an error.

Parameters:colName – string, name of the new column.

col – a Column expression for the new column.

>>> df.withColumn('age2',df.age+2).collect()[Row(age=2, name=u'Alice', age2=4), Row(age=5, name=u'Bob', age2=7)]

New in version 1.3.

withColumnRenamed(existingnew)[source]

Returns a new DataFrame by renaming an existing column. This is a no-op if schema doesn’t contain the given column name.

Parameters:existing – string, name of the existing column to rename.

col – string, new name of the column.

>>> df.withColumnRenamed('age','age2').collect()[Row(age2=2, name=u'Alice'), Row(age2=5, name=u'Bob')]

New in version 1.3.

withWatermark(eventTimedelayThreshold)[source]

Defines an event time watermark for this DataFrame. A watermark tracks a point in time before which we assume no more late data is going to arrive.

Spark will use this watermark for several purposes:

To know when a given time window aggregation can be finalized and thus can be emitted when using output modes that do not allow updates.

To minimize the amount of state that we need to keep for on-going aggregations.

The current watermark is computed by looking at the MAX(eventTime) seen across all of the partitions in the query minus a user specified delayThreshold. Due to the cost of coordinating this value across partitions, the actual watermark used is only guaranteed to be at least delayThreshold behind the actual event time. In some cases we may still process records that arrive more than delayThreshold late.

Parameters:eventTime – the name of the column that contains the event time of the row.

delayThreshold – the minimum delay to wait to data to arrive late, relative to the latest record that has been processed in the form of an interval (e.g. “1 minute” or “5 hours”).

Note


Evolving

>>> sdf.select('name',sdf.time.cast('timestamp')).withWatermark('time','10 minutes')DataFrame[name: string, time: timestamp]

New in version 2.1.

write

Interface for saving the content of the non-streaming DataFrame out into external storage.

Returns:DataFrameWriter

New in version 1.4.

writeStream

Interface for saving the content of the streaming DataFrame out into external storage.

Note


Evolving.

Returns:DataStreamWriter

New in version 2.0.

class pyspark.sql.GroupedData(jgddf)[source]

A set of methods for aggregations on a DataFrame, created by DataFrame.groupBy().

Note


Experimental

New in version 1.3.

agg(*exprs)[source]

Compute aggregates and returns the result as a DataFrame.

The available aggregate functions are avg, max, min, sum, count.

If exprs is a single dict mapping from string to string, then the key is the column to perform aggregation on, and the value is the aggregate function.

Alternatively, exprs can also be a list of aggregate Column expressions.

Parameters:exprs – a dict mapping from column name (string) to aggregate functions (string), or a list of Column.

>>> gdf=df.groupBy(df.name)>>> sorted(gdf.agg({"*":"count"}).collect())[Row(name=u'Alice', count(1)=1), Row(name=u'Bob', count(1)=1)]

>>> frompyspark.sqlimportfunctionsasF>>> sorted(gdf.agg(F.min(df.age)).collect())[Row(name=u'Alice', min(age)=2), Row(name=u'Bob', min(age)=5)]

New in version 1.3.

apply(udf)[source]

Maps each group of the current DataFrame using a pandas udf and returns the result as a DataFrame.

The user-defined function should take a pandas.DataFrame and return another pandas.DataFrame. For each group, all columns are passed together as a pandas.DataFrame to the user-function and the returned pandas.DataFrame`s are combined as a :class:`DataFrame. The returned pandas.DataFrame can be of arbitrary length and its schema must match the returnType of the pandas udf.

This function does not support partial aggregation, and requires shuffling all the data in the DataFrame.

Note


Experimental

Parameters:udf – a grouped map user-defined function returned by pyspark.sql.functions.pandas_udf().

>>> frompyspark.sql.functionsimportpandas_udf,PandasUDFType>>> df=spark.createDataFrame(... [(1,1.0),(1,2.0),(2,3.0),(2,5.0),(2,10.0)],... ("id","v"))>>> :pandas_udf("id long, v double",PandasUDFType.GROUPED_MAP)... defnormalize(pdf):... v=pdf.v... returnpdf.assign(v=(v-v.mean())/v.std())>>> df.groupby("id").apply(normalize).show()+---+-------------------+| id|                  v|+---+-------------------+|  1|-0.7071067811865475||  1| 0.7071067811865475||  2|-0.8320502943378437||  2|-0.2773500981126146||  2| 1.1094003924504583|+---+-------------------+

See also


pyspark.sql.functions.pandas_udf()

New in version 2.3.

avg(*cols)[source]

Computes average values for each numeric columns for each group.

mean() is an alias for avg().

Parameters:cols – list of column names (string). Non-numeric columns are ignored.

>>> df.groupBy().avg('age').collect()[Row(avg(age)=3.5)]>>> df3.groupBy().avg('age','height').collect()[Row(avg(age)=3.5, avg(height)=82.5)]

New in version 1.3.

count()[source]

Counts the number of records for each group.

>>> sorted(df.groupBy(df.age).count().collect())[Row(age=2, count=1), Row(age=5, count=1)]

New in version 1.3.

max(*cols)[source]

Computes the max value for each numeric columns for each group.

>>> df.groupBy().max('age').collect()[Row(max(age)=5)]>>> df3.groupBy().max('age','height').collect()[Row(max(age)=5, max(height)=85)]

New in version 1.3.

mean(*cols)[source]

Computes average values for each numeric columns for each group.

mean() is an alias for avg().

Parameters:cols – list of column names (string). Non-numeric columns are ignored.

>>> df.groupBy().mean('age').collect()[Row(avg(age)=3.5)]>>> df3.groupBy().mean('age','height').collect()[Row(avg(age)=3.5, avg(height)=82.5)]

New in version 1.3.

min(*cols)[source]

Computes the min value for each numeric column for each group.

Parameters:cols – list of column names (string). Non-numeric columns are ignored.

>>> df.groupBy().min('age').collect()[Row(min(age)=2)]>>> df3.groupBy().min('age','height').collect()[Row(min(age)=2, min(height)=80)]

New in version 1.3.

pivot(pivot_colvalues=None)[source]

Pivots a column of the current DataFrame and perform the specified aggregation. There are two versions of pivot function: one that requires the caller to specify the list of distinct values to pivot on, and one that does not. The latter is more concise but less efficient, because Spark needs to first compute the list of distinct values internally.

Parameters:pivot_col – Name of the column to pivot.

values – List of values that will be translated to columns in the output DataFrame.

# Compute the sum of earnings for each year by course with each course as a separate column

>>> df4.groupBy("year").pivot("course",["dotNET","Java"]).sum("earnings").collect()[Row(year=2012, dotNET=15000, Java=20000), Row(year=2013, dotNET=48000, Java=30000)]

# Or without specifying column values (less efficient)

>>> df4.groupBy("year").pivot("course").sum("earnings").collect()[Row(year=2012, Java=20000, dotNET=15000), Row(year=2013, Java=30000, dotNET=48000)]

New in version 1.6.

sum(*cols)[source]

Compute the sum for each numeric columns for each group.

Parameters:cols – list of column names (string). Non-numeric columns are ignored.

>>> df.groupBy().sum('age').collect()[Row(sum(age)=7)]>>> df3.groupBy().sum('age','height').collect()[Row(sum(age)=7, sum(height)=165)]

New in version 1.3.

class pyspark.sql.Column(jc)[source]

A column in a DataFrame.

Column instances can be created by:

# 1. Select a column out of a DataFramedf.colNamedf["colName"]# 2. Create from an expressiondf.colName+11/df.colName

New in version 1.3.

alias(*alias**kwargs)[source]

Returns this column aliased with a new name or names (in the case of expressions that return more than one column, such as explode).

Parameters:alias – strings of desired column names (collects all positional arguments passed)

metadata – a dict of information to be stored in metadata attribute of the corresponding :class: StructField (optional, keyword only argument)

Changed in version 2.2: Added optional metadata argument.

>>> df.select(df.age.alias("age2")).collect()[Row(age2=2), Row(age2=5)]>>> df.select(df.age.alias("age3",metadata={'max':99})).schema['age3'].metadata['max']99

New in version 1.3.

asc()

Returns a sort expression based on the ascending order of the given column name

>>> frompyspark.sqlimportRow>>> df=spark.createDataFrame([Row(name=u'Tom',height=80),Row(name=u'Alice',height=None)])>>> df.select(df.name).orderBy(df.name.asc()).collect()[Row(name=u'Alice'), Row(name=u'Tom')]

astype(dataType)

astype() is an alias for cast().

New in version 1.4.

between(lowerBoundupperBound)[source]

A boolean expression that is evaluated to true if the value of this expression is between the given columns.

>>> df.select(df.name,df.age.between(2,4)).show()+-----+---------------------------+| name|((age >= 2) AND (age <= 4))|+-----+---------------------------+|Alice|                      true||  Bob|                      false|+-----+---------------------------+

New in version 1.3.

bitwiseAND(other)

Compute bitwise AND of this expression with another expression.

Parameters:other – a value or Column to calculate bitwise and(&) against this Column.

>>> frompyspark.sqlimportRow>>> df=spark.createDataFrame([Row(a=170,b=75)])>>> df.select(df.a.bitwiseAND(df.b)).collect()[Row((a & b)=10)]

bitwiseOR(other)

Compute bitwise OR of this expression with another expression.

Parameters:other – a value or Column to calculate bitwise or(|) against this Column.

>>> frompyspark.sqlimportRow>>> df=spark.createDataFrame([Row(a=170,b=75)])>>> df.select(df.a.bitwiseOR(df.b)).collect()[Row((a | b)=235)]

bitwiseXOR(other)

Compute bitwise XOR of this expression with another expression.

Parameters:other – a value or Column to calculate bitwise xor(^) against this Column.

>>> frompyspark.sqlimportRow>>> df=spark.createDataFrame([Row(a=170,b=75)])>>> df.select(df.a.bitwiseXOR(df.b)).collect()[Row((a ^ b)=225)]

cast(dataType)[source]

Convert the column into type dataType.

>>> df.select(df.age.cast("string").alias('ages')).collect()[Row(ages=u'2'), Row(ages=u'5')]>>> df.select(df.age.cast(StringType()).alias('ages')).collect()[Row(ages=u'2'), Row(ages=u'5')]

New in version 1.3.

contains(other)

Contains the other element. Returns a boolean Column based on a string match.

Parameters:other – string in line

>>> df.filter(df.name.contains('o')).collect()[Row(age=5, name=u'Bob')]

desc()

Returns a sort expression based on the descending order of the given column name.

>>> frompyspark.sqlimportRow>>> df=spark.createDataFrame([Row(name=u'Tom',height=80),Row(name=u'Alice',height=None)])>>> df.select(df.name).orderBy(df.name.desc()).collect()[Row(name=u'Tom'), Row(name=u'Alice')]

endswith(other)

String ends with. Returns a boolean Column based on a string match.

Parameters:other – string at end of line (do not use a regex $)

>>> df.filter(df.name.endswith('ice')).collect()[Row(age=2, name=u'Alice')]>>> df.filter(df.name.endswith('ice$')).collect()[]

eqNullSafe(other)

Equality test that is safe for null values.

Parameters:other – a value or Column

>>> frompyspark.sqlimportRow>>> df1=spark.createDataFrame([... Row(id=1,value='foo'),... Row(id=2,value=None)... ])>>> df1.select(... df1['value']=='foo',... df1['value'].eqNullSafe('foo'),... df1['value'].eqNullSafe(None)... ).show()+-------------+---------------+----------------+|(value = foo)|(value <=> foo)|(value <=> NULL)|+-------------+---------------+----------------+|        true|          true|          false||        null|          false|            true|+-------------+---------------+----------------+>>> df2=spark.createDataFrame([... Row(value='bar'),... Row(value=None)... ])>>> df1.join(df2,df1["value"]==df2["value"]).count()0>>> df1.join(df2,df1["value"].eqNullSafe(df2["value"])).count()1>>> df2=spark.createDataFrame([... Row(id=1,value=float('NaN')),... Row(id=2,value=42.0),... Row(id=3,value=None)... ])>>> df2.select(... df2['value'].eqNullSafe(None),... df2['value'].eqNullSafe(float('NaN')),... df2['value'].eqNullSafe(42.0)... ).show()+----------------+---------------+----------------+|(value <=> NULL)|(value <=> NaN)|(value <=> 42.0)|+----------------+---------------+----------------+|          false|          true|          false||          false|          false|            true||            true|          false|          false|+----------------+---------------+----------------+

Note


Unlike Pandas, PySpark doesn’t consider NaN values to be NULL. See the NaN Semantics for details.

New in version 2.3.0.

getField(name)[source]

An expression that gets a field by name in a StructField.

>>> frompyspark.sqlimportRow>>> df=spark.createDataFrame([Row(r=Row(a=1,b="b"))])>>> df.select(df.r.getField("b")).show()+---+|r.b|+---+|  b|+---+>>> df.select(df.r.a).show()+---+|r.a|+---+|  1|+---+

New in version 1.3.

getItem(key)[source]

An expression that gets an item at position ordinal out of a list, or gets an item by key out of a dict.

>>> df=spark.createDataFrame([([1,2],{"key":"value"})],["l","d"])>>> df.select(df.l.getItem(0),df.d.getItem("key")).show()+----+------+|l[0]|d[key]|+----+------+|  1| value|+----+------+>>> df.select(df.l[0],df.d["key"]).show()+----+------+|l[0]|d[key]|+----+------+|  1| value|+----+------+

New in version 1.3.

isNotNull()

True if the current expression is NOT null.

>>> frompyspark.sqlimportRow>>> df=spark.createDataFrame([Row(name=u'Tom',height=80),Row(name=u'Alice',height=None)])>>> df.filter(df.height.isNotNull()).collect()[Row(height=80, name=u'Tom')]

isNull()

True if the current expression is null.

>>> frompyspark.sqlimportRow>>> df=spark.createDataFrame([Row(name=u'Tom',height=80),Row(name=u'Alice',height=None)])>>> df.filter(df.height.isNull()).collect()[Row(height=None, name=u'Alice')]

isin(*cols)[source]

A boolean expression that is evaluated to true if the value of this expression is contained by the evaluated values of the arguments.

>>> df[df.name.isin("Bob","Mike")].collect()[Row(age=5, name=u'Bob')]>>> df[df.age.isin([1,2,3])].collect()[Row(age=2, name=u'Alice')]

New in version 1.5.

like(other)

SQL like expression. Returns a boolean Column based on a SQL LIKE match.

Parameters:other – a SQL LIKE pattern

See rlike() for a regex version

>>> df.filter(df.name.like('Al%')).collect()[Row(age=2, name=u'Alice')]

name(*alias**kwargs)

name() is an alias for alias().

New in version 2.0.

otherwise(value)[source]

Evaluates a list of conditions and returns one of multiple possible result expressions. If Column.otherwise() is not invoked, None is returned for unmatched conditions.

See pyspark.sql.functions.when() for example usage.

Parameters:value – a literal value, or a Column expression.

>>> frompyspark.sqlimportfunctionsasF>>> df.select(df.name,F.when(df.age>3,1).otherwise(0)).show()+-----+-------------------------------------+| name|CASE WHEN (age > 3) THEN 1 ELSE 0 END|+-----+-------------------------------------+|Alice|                                    0||  Bob|                                    1|+-----+-------------------------------------+

New in version 1.4.

over(window)[source]

Define a windowing column.

Parameters:window – a WindowSpec

Returns:a Column

>>> frompyspark.sqlimportWindow>>> window=Window.partitionBy("name").orderBy("age").rowsBetween(-1,1)>>> frompyspark.sql.functionsimportrank,min>>> # df.select(rank().over(window), min('age').over(window))

New in version 1.4.

rlike(other)

SQL RLIKE expression (LIKE with Regex). Returns a boolean Column based on a regex match.

Parameters:other – an extended regex expression

>>> df.filter(df.name.rlike('ice$')).collect()[Row(age=2, name=u'Alice')]

startswith(other)

String starts with. Returns a boolean Column based on a string match.

Parameters:other – string at start of line (do not use a regex ^)

>>> df.filter(df.name.startswith('Al')).collect()[Row(age=2, name=u'Alice')]>>> df.filter(df.name.startswith('^Al')).collect()[]

substr(startPoslength)[source]

Return a Column which is a substring of the column.

Parameters:startPos – start position (int or Column)

length – length of the substring (int or Column)

>>> df.select(df.name.substr(1,3).alias("col")).collect()[Row(col=u'Ali'), Row(col=u'Bob')]

New in version 1.3.

when(conditionvalue)[source]

Evaluates a list of conditions and returns one of multiple possible result expressions. If Column.otherwise() is not invoked, None is returned for unmatched conditions.

See pyspark.sql.functions.when() for example usage.

Parameters:condition – a boolean Column expression.

value – a literal value, or a Column expression.

>>> frompyspark.sqlimportfunctionsasF>>> df.select(df.name,F.when(df.age>4,1).when(df.age<3,-1).otherwise(0)).show()+-----+------------------------------------------------------------+| name|CASE WHEN (age > 4) THEN 1 WHEN (age < 3) THEN -1 ELSE 0 END|+-----+------------------------------------------------------------+|Alice|                                                          -1||  Bob|                                                          1|+-----+------------------------------------------------------------+

New in version 1.4.

class pyspark.sql.Catalog(sparkSession)[source]

User-facing catalog API, accessible through SparkSession.catalog.

This is a thin wrapper around its Scala implementation org.apache.spark.sql.catalog.Catalog.

cacheTable(tableName)[source]

Caches the specified table in-memory.

New in version 2.0.

clearCache()[source]

Removes all cached tables from the in-memory cache.

New in version 2.0.

createExternalTable(tableNamepath=Nonesource=Noneschema=None**options)[source]

Creates a table based on the dataset in a data source.

It returns the DataFrame associated with the external table.

The data source is specified by the source and a set of options. If source is not specified, the default data source configured byspark.sql.sources.default will be used.

Optionally, a schema can be provided as the schema of the returned DataFrame and created external table.

Returns:DataFrame

New in version 2.0.

createTable(tableNamepath=Nonesource=Noneschema=None**options)[source]

Creates a table based on the dataset in a data source.

It returns the DataFrame associated with the table.

数据源由source和一组指定options。如果source未指定,spark.sql.sources.default则将使用由其配置的默认数据源 。当path指定时,外部表从数据在给定的路径中创建。否则,将创建一个托管表。

或者,可以将模式作为返回DataFrame和创建的表的模式提供。

返回:DataFrame

2.2版本中的新功能。

currentDatabase()[source]

返回此会话中的当前默认数据库。

2.0版本中的新功能。

dropGlobalTempView(viewName [source]

使用目录中的给定视图名称删除全局临时视图。如果视图之前被缓存过,那么它也将被解除缓存。如果此视图已成功删除,则返回true,否则返回false。

>>> 火花。createDataFrame ([(1 ,1 )]) 。createGlobalTempView (“my_table” )>>> spark 。表(“global_temp.my_table” )。collect ()[Row(_1 = 1,_2 = 1)] >>> spark 。目录。dropGlobalTempView (“my_table” )>>> spark 。表(“global_temp.my_table” )追踪(最近的最后一次呼叫):...:...

2.1版本中的新功能。

dropTempView(viewName [source]

删除目录中给定视图名称的本地临时视图。如果视图之前被缓存过,那么它也将被解除缓存。如果此视图已成功删除,则返回true,否则返回false。

请注意,此方法的返回类型在Spark 2.0中为None,但在Spark 2.1中更改为Boolean。

>>> 火花。createDataFrame ([(1 ,1 )]) 。createTempView (“my_table” )>>> spark 。表(“my_table” )。collect ()[Row(_1 = 1,_2 = 1)] >>> spark 。目录。dropTempView (“my_table” )>>> spark 。table (“my_table” )Traceback(最近一次调用最后一次):... AnalysisException:...

2.0版本中的新功能。

isCached(tableName [source]

如果表当前缓存在内存中,则返回true。

2.0版本中的新功能。

listColumns(tableNamedbName = None [source]

返回指定数据库中给定表/视图的列的列表。

如果没有指定数据库,则使用当前数据库。

注意:这里的参数顺序与JVM对应的顺序不同,因为Python不支持方法重载。

2.0版本中的新功能。

listDatabases()[source]

返回所有会话中可用的数据库列表。

2.0版本中的新功能。

listFunctions(dbName = None [source]

返回在指定数据库中注册的函数列表。

如果没有指定数据库,则使用当前数据库。这包括所有临时功能。

2.0版本中的新功能。

listTables(dbName = None [source]

Returns a list of tables/views in the specified database.

If no database is specified, the current database is used. This includes all temporary views.

New in version 2.0.

recoverPartitions(tableName)[source]

Recovers all the partitions of the given table and update the catalog.

Only works with a partitioned table, and not a view.

New in version 2.1.1.

refreshByPath(path)[source]

Invalidates and refreshes all the cached data (and the associated metadata) for any DataFrame that contains the given data source path.

New in version 2.2.0.

refreshTable(tableName)[source]

Invalidates and refreshes all the cached data and metadata of the given table.

New in version 2.0.

registerFunction(namefreturnType=None)[source]

An alias for spark.udf.register(). See pyspark.sql.UDFRegistration.register().

Note


Deprecated in 2.3.0. Use spark.udf.register() instead.

New in version 2.0.

setCurrentDatabase(dbName)[source]

Sets the current default database in this session.

New in version 2.0.

uncacheTable(tableName)[source]

Removes the specified table from the in-memory cache.

New in version 2.0.

class pyspark.sql.Row[source]

A row in DataFrame. The fields in it can be accessed:

like attributes (row.key)

like dictionary values (row[key])

key in row will search through row keys.

Row can be used to create a row object by using named arguments, the fields will be sorted by names. It is not allowed to omit a named argument to represent the value is None or missing. This should be explicitly set to None in this case.

>>> row=Row(name="Alice",age=11)>>> rowRow(age=11, name='Alice')>>> row['name'],row['age']('Alice', 11)>>> row.name,row.age('Alice', 11)>>> 'name'inrowTrue>>> 'wrong_key'inrowFalse

Row also can be used to create another Row like class, then it could be used to create Row objects, such as

>>> Person=Row("name","age")>>> Person>>> 'name'inPersonTrue>>> 'wrong_key'inPersonFalse>>> Person("Alice",11)Row(name='Alice', age=11)

asDict(recursive=False)[source]

Return as an dict

Parameters: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

class pyspark.sql.DataFrameNaFunctions(df)[source]

Functionality for working with missing data in DataFrame.

New in version 1.4.

drop(how='any'thresh=Nonesubset=None)[source]

Returns a new DataFrame omitting rows with null values. DataFrame.dropna() and DataFrameNaFunctions.drop() are aliases of each other.

Parameters:how – ‘any’ or ‘all’. If ‘any’, drop a row if it contains any nulls. If ‘all’, drop a row only if all its values are null.

thresh – int, default None If specified, drop rows that have less than thresh non-null values. This overwrites the how parameter.

subset – optional list of column names to consider.

>>> df4.na.drop().show()+---+------+-----+|age|height| name|+---+------+-----+| 10|    80|Alice|+---+------+-----+

New in version 1.3.1.

fill(valuesubset=None)[source]

Replace null values, alias for na.fill(). DataFrame.fillna() and DataFrameNaFunctions.fill() are aliases of each other.

Parameters:value – int, long, float, string, bool or dict. Value to replace null values with. If the value is a dict, then subset is ignored and valuemust be a mapping from column name (string) to replacement value. The replacement value must be an int, long, float, boolean, or string.

subset – optional list of column names to consider. Columns specified in subset that do not have matching data type are ignored. For example, if value is a string, and subset contains a non-string column, then the non-string column is simply ignored.

>>> df4.na.fill(50).show()+---+------+-----+|age|height| name|+---+------+-----+| 10|    80|Alice||  5|    50|  Bob|| 50|    50|  Tom|| 50|    50| null|+---+------+-----+

>>> df5.na.fill(False).show()+----+-------+-----+| age|  name|  spy|+----+-------+-----+|  10|  Alice|false||  5|    Bob|false||null|Mallory| true|+----+-------+-----+

>>> df4.na.fill({'age':50,'name':'unknown'}).show()+---+------+-------+|age|height|  name|+---+------+-------+| 10|    80|  Alice||  5|  null|    Bob|| 50|  null|    Tom|| 50|  null|unknown|+---+------+-------+

New in version 1.3.1.

replace(to_replacevalue=subset=None)[source]

Returns a new DataFrame replacing a value with another value. DataFrame.replace() and DataFrameNaFunctions.replace() are aliases of each other. Values to_replace and value must have the same type and can only be numerics, booleans, or strings. Value can have None. When replacing, the new value will be cast to the type of the existing column. For numeric replacements all values to be replaced should have unique floating point representation. In case of conflicts (for example with {42: -1, 42.0: 1}) and arbitrary replacement will be used.

Parameters:to_replace – bool, int, long, float, string, list or dict. Value to be replaced. If the value is a dict, then value is ignored or can be omitted, and to_replace must be a mapping between a value and a replacement.

value – bool, int, long, float, string, list or None. The replacement value must be a bool, int, long, float, string or None. If value is a list, value should be of the same length and type as to_replace. If value is a scalar and to_replace is a sequence, then value is used as a replacement for each item in to_replace.

subset – optional list of column names to consider. Columns specified in subset that do not have matching data type are ignored. For example, if value is a string, and subset contains a non-string column, then the non-string column is simply ignored.

>>> df4.na.replace(10,20).show()+----+------+-----+| age|height| name|+----+------+-----+|  20|    80|Alice||  5|  null|  Bob||null|  null|  Tom||null|  null| null|+----+------+-----+

>>> df4.na.replace('Alice',None).show()+----+------+----+| age|height|name|+----+------+----+|  10|    80|null||  5|  null| Bob||null|  null| Tom||null|  null|null|+----+------+----+

>>> df4.na.replace({'Alice':None}).show()+----+------+----+| age|height|name|+----+------+----+|  10|    80|null||  5|  null| Bob||null|  null| Tom||null|  null|null|+----+------+----+

>>> df4.na.replace(['Alice','Bob'],['A','B'],'name').show()+----+------+----+| age|height|name|+----+------+----+|  10|    80|  A||  5|  null|  B||null|  null| Tom||null|  null|null|+----+------+----+

New in version 1.4.

class pyspark.sql.DataFrameStatFunctions(df)[source]

Functionality for statistic functions with DataFrame.

New in version 1.4.

approxQuantile(colprobabilitiesrelativeError)[source]

Calculates the approximate quantiles of numerical columns of a DataFrame.

The result of this algorithm has the following deterministic bound: If the DataFrame has N elements and if we request the quantile at probability p up to error err, then the algorithm will return a sample x from the DataFrame so that the exact rank of x is close to (p * N). More precisely,

floor((p - err) * N) <= rank(x) <= ceil((p + err) * N).

This method implements a variation of the Greenwald-Khanna algorithm (with some speed optimizations). The algorithm was first present in [[http://dx.doi.org/10.1145/375663.375670 Space-efficient Online Computation of Quantile Summaries]] by Greenwald and Khanna.

Note that null values will be ignored in numerical columns before calculation. For columns only containing null values, an empty list is returned.

Parameters:col – str, list. Can be a single column name, or a list of names for multiple columns.

probabilities – a list of quantile probabilities Each number must belong to [0, 1]. For example 0 is the minimum, 0.5 is the median, 1 is the maximum.

relativeError – The relative target precision to achieve (>= 0). If set to zero, the exact quantiles are computed, which could be very expensive. Note that values greater than 1 are accepted but give the same result as 1.

Returns:the approximate quantiles at the given probabilities. If the input col is a string, the output is a list of floats. If the input col is a list or tuple of strings, the output is also a list, but each element in it is a list of floats, i.e., the output is a list of list of floats.

Changed in version 2.2: Added support for multiple columns.

New in version 2.0.

corr(col1col2method=None)[source]

Calculates the correlation of two columns of a DataFrame as a double value. Currently only supports the Pearson Correlation Coefficient.DataFrame.corr() and DataFrameStatFunctions.corr() are aliases of each other.

Parameters:col1 – The name of the first column

col2 – The name of the second column

method – The correlation method. Currently only supports “pearson”

New in version 1.4.

cov(col1col2)[source]

Calculate the sample covariance for the given columns, specified by their names, as a double value. DataFrame.cov() and DataFrameStatFunctions.cov() are aliases.

Parameters:col1 – The name of the first column

col2 – The name of the second column

New in version 1.4.

crosstab(col1col2)[source]

Computes a pair-wise frequency table of the given columns. Also known as a contingency table. The number of distinct values for each column should be less than 1e4. At most 1e6 non-zero pair frequencies will be returned. The first column of each row will be the distinct values of col1 and the column names will be the distinct values of col2. The name of the first column will be $col1_$col2. Pairs that have no occurrences will have zero as their counts. DataFrame.crosstab() and DataFrameStatFunctions.crosstab() are aliases.

Parameters:col1 – The name of the first column. Distinct items will make the first item of each row.

col2 – The name of the second column. Distinct items will make the column names of the DataFrame.

New in version 1.4.

freqItems(colssupport=None)[source]

Finding frequent items for columns, possibly with false positives. Using the frequent element count algorithm described in “http://dx.doi.org/10.1145/762471.762473, proposed by Karp, Schenker, and Papadimitriou”. DataFrame.freqItems() and DataFrameStatFunctions.freqItems() are aliases.

Note


This function is meant for exploratory data analysis, as we make no guarantee about the backward compatibility of the schema of the resulting DataFrame.

Parameters:cols – Names of the columns to calculate frequent items for as a list or tuple of strings.

support – The frequency with which to consider an item ‘frequent’. Default is 1%. The support must be greater than 1e-4.

New in version 1.4.

sampleBy(colfractionsseed=None)[source]

Returns a stratified sample without replacement based on the fraction given on each stratum.

Parameters:col – column that defines strata

fractions – sampling fraction for each stratum. If a stratum is not specified, we treat its fraction as zero.

seed – random seed

Returns:a new DataFrame that represents the stratified sample

>>> frompyspark.sql.functionsimportcol>>> dataset=sqlContext.range(0,100).select((col("id")%3).alias("key"))>>> sampled=dataset.sampleBy("key",fractions={0:0.1,1:0.2},seed=0)>>> sampled.groupBy("key").count().orderBy("key").show()+---+-----+|key|count|+---+-----+|  0|    5||  1|    9|+---+-----+

New in version 1.5.

class pyspark.sql.Window[source]

Utility functions for defining window in DataFrames.

For example:

>>> # ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW>>> window=Window.orderBy("date").rowsBetween(Window.unboundedPreceding,Window.currentRow)

>>> # PARTITION BY country ORDER BY date RANGE BETWEEN 3 PRECEDING AND 3 FOLLOWING>>> window=Window.orderBy("date").partitionBy("country").rangeBetween(-3,3)

Note


Experimental

New in version 1.4.

currentRow = 0

static orderBy(*cols)[source]

Creates a WindowSpec with the ordering defined.

New in version 1.4.

static partitionBy(*cols)[source]

Creates a WindowSpec with the partitioning defined.

New in version 1.4.

static rangeBetween(startend)[source]

Creates a WindowSpec with the frame boundaries defined, from start (inclusive) to end (inclusive).

Both start and end are relative from the current row. For example, “0” means “current row”, while “-1” means one off before the current row, and “5” means the five off after the current row.

We recommend users use Window.unboundedPreceding, Window.unboundedFollowing, and Window.currentRow to specify special boundary values, rather than using integral values directly.

Parameters:start – boundary start, inclusive. The frame is unbounded if this is Window.unboundedPreceding, or any value less than or equal to max(-sys.maxsize, -9223372036854775808).

end – boundary end, inclusive. The frame is unbounded if this is Window.unboundedFollowing, or any value greater than or equal to min(sys.maxsize, 9223372036854775807).

New in version 2.1.

static rowsBetween(startend)[source]

Creates a WindowSpec with the frame boundaries defined, from start (inclusive) to end (inclusive).

Both start and end are relative positions from the current row. For example, “0” means “current row”, while “-1” means the row before the current row, and “5” means the fifth row after the current row.

We recommend users use Window.unboundedPreceding, Window.unboundedFollowing, and Window.currentRow to specify special boundary values, rather than using integral values directly.

Parameters:start – boundary start, inclusive. The frame is unbounded if this is Window.unboundedPreceding, or any value less than or equal to -9223372036854775808.

end – boundary end, inclusive. The frame is unbounded if this is Window.unboundedFollowing, or any value greater than or equal to 9223372036854775807.

New in version 2.1.

unboundedFollowing = 9223372036854775807L

unboundedPreceding = -9223372036854775808L

class pyspark.sql.WindowSpec(jspec)[source]

A window specification that defines the partitioning, ordering, and frame boundaries.

Use the static methods in Window to create a WindowSpec.

Note


Experimental

New in version 1.4.

orderBy(*cols)[source]

Defines the ordering columns in a WindowSpec.

Parameters:cols – names of columns or expressions

New in version 1.4.

partitionBy(*cols)[source]

Defines the partitioning columns in a WindowSpec.

Parameters:cols – names of columns or expressions

New in version 1.4.

rangeBetween(startend)[source]

Defines the frame boundaries, from start (inclusive) to end (inclusive).

Both start and end are relative from the current row. For example, “0” means “current row”, while “-1” means one off before the current row, and “5” means the five off after the current row.

We recommend users use Window.unboundedPreceding, Window.unboundedFollowing, and Window.currentRow to specify special boundary values, rather than using integral values directly.

Parameters:start – boundary start, inclusive. The frame is unbounded if this is Window.unboundedPreceding, or any value less than or equal to max(-sys.maxsize, -9223372036854775808).

end – boundary end, inclusive. The frame is unbounded if this is Window.unboundedFollowing, or any value greater than or equal to min(sys.maxsize, 9223372036854775807).

New in version 1.4.

rowsBetween(startend)[source]

Defines the frame boundaries, from start (inclusive) to end (inclusive).

Both start and end are relative positions from the current row. For example, “0” means “current row”, while “-1” means the row before the current row, and “5” means the fifth row after the current row.

We recommend users use Window.unboundedPreceding, Window.unboundedFollowing, and Window.currentRow to specify special boundary values, rather than using integral values directly.

Parameters:start – boundary start, inclusive. The frame is unbounded if this is Window.unboundedPreceding, or any value less than or equal to max(-sys.maxsize, -9223372036854775808).

end – boundary end, inclusive. The frame is unbounded if this is Window.unboundedFollowing, or any value greater than or equal to min(sys.maxsize, 9223372036854775807).

New in version 1.4.

class pyspark.sql.DataFrameReader(spark)[source]

Interface used to load a DataFrame from external storage systems (e.g. file systems, key-value stores, etc). Use spark.read() to access this.

New in version 1.4.

csv(pathschema=Nonesep=Noneencoding=Nonequote=Noneescape=Nonecomment=Noneheader=NoneinferSchema=NoneignoreLeadingWhiteSpace=NoneignoreTrailingWhiteSpace=NonenullValue=NonenanValue=NonepositiveInf=NonenegativeInf=NonedateFormat=NonetimestampFormat=NonemaxColumns=NonemaxCharsPerColumn=NonemaxMalformedLogPerPartition=Nonemode=NonecolumnNameOfCorruptRecord=NonemultiLine=NonecharToEscapeQuoteEscaping=None)[source]

Loads a CSV file and returns the result as a DataFrame.

This function will go through the input once to determine the input schema if inferSchema is enabled. To avoid going through the entire data once, disable inferSchema option or specify the schema explicitly using schema.

Parameters:path – string, or list of strings, for input path(s), or RDD of Strings storing CSV rows.

schema – an optional pyspark.sql.types.StructType for the input schema or a DDL-formatted string (For example col0 INT, col1DOUBLE).

sep – sets a single character as a separator for each field and value. If None is set, it uses the default value, ,.

encoding – decodes the CSV files by the given encoding type. If None is set, it uses the default value, UTF-8.

quote – sets a single character used for escaping quoted values where the separator can be part of the value. If None is set, it uses the default value, ". If you would like to turn off quotations, you need to set an empty string.

escape – sets a single character used for escaping quotes inside an already quoted value. If None is set, it uses the default value, \.

comment – sets a single character used for skipping lines beginning with this character. By default (None), it is disabled.

header – uses the first line as names of columns. If None is set, it uses the default value, false.

inferSchema – infers the input schema automatically from data. It requires one extra pass over the data. If None is set, it uses the default value, false.

ignoreLeadingWhiteSpace – A flag indicating whether or not leading whitespaces from values being read should be skipped. If None is set, it uses the default value, false.

ignoreTrailingWhiteSpace – A flag indicating whether or not trailing whitespaces from values being read should be skipped. If None is set, it uses the default value, false.

nullValue – sets the string representation of a null value. If None is set, it uses the default value, empty string. Since 2.0.1, this nullValue param applies to all supported types including the string type.

nanValue – sets the string representation of a non-number value. If None is set, it uses the default value, NaN.

positiveInf – sets the string representation of a positive infinity value. If None is set, it uses the default value, Inf.

negativeInf – sets the string representation of a negative infinity value. If None is set, it uses the default value, Inf.

dateFormat – sets the string that indicates a date format. Custom date formats follow the formats at java.text.SimpleDateFormat. This applies to date type. If None is set, it uses the default value, yyyy-MM-dd.

timestampFormat – sets the string that indicates a timestamp format. Custom date formats follow the formats at java.text.SimpleDateFormat. This applies to timestamp type. If None is set, it uses the default value, yyyy-MM-dd'T'HH:mm:ss.SSSXXX.

maxColumns – defines a hard limit of how many columns a record can have. If None is set, it uses the default value, 20480.

maxCharsPerColumn – defines the maximum number of characters allowed for any given value being read. If None is set, it uses the default value, -1 meaning unlimited length.

maxMalformedLogPerPartition – this parameter is no longer used since Spark 2.2.0. If specified, it is ignored.

mode –

allows a mode for dealing with corrupt records during parsing. If None is

set, it uses the default value, PERMISSIVE.

PERMISSIVE : when it meets a corrupted record, puts the malformed string into a field configured by columnNameOfCorruptRecord, and sets other fields to null. To keep corrupt records, an user can set a string type field named columnNameOfCorruptRecord in an user-defined schema. If a schema does not have the field, it drops corrupt records during parsing. A record with less/more tokens than schema is not a corrupted record to CSV. When it meets a record having fewer tokens than the length of the schema, sets null to extra fields. When the record has more tokens than the length of the schema, it drops extra tokens.

DROPMALFORMED : ignores the whole corrupted records.

FAILFAST : throws an exception when it meets corrupted records.

columnNameOfCorruptRecord – allows renaming the new field having malformed string created by PERMISSIVE mode. This overrides spark.sql.columnNameOfCorruptRecord. If None is set, it uses the value specified in spark.sql.columnNameOfCorruptRecord.

multiLine – parse records, which may span multiple lines. If None is set, it uses the default value, false.

charToEscapeQuoteEscaping – sets a single character used for escaping the escape for the quote character. If None is set, the default value is escape character when escape and quote characters are different, \ otherwise.

>>> df=spark.read.csv('python/test_support/sql/ages.csv')>>> df.dtypes[('_c0', 'string'), ('_c1', 'string')]>>> rdd=sc.textFile('python/test_support/sql/ages.csv')>>> df2=spark.read.csv(rdd)>>> df2.dtypes[('_c0', 'string'), ('_c1', 'string')]

New in version 2.0.

format(source)[source]

Specifies the input data source format.

Parameters:source – string, name of the data source, e.g. ‘json’, ‘parquet’.

>>> df=spark.read.format('json').load('python/test_support/sql/people.json')>>> df.dtypes[('age', 'bigint'), ('name', 'string')]

New in version 1.4.

jdbc(urltablecolumn=NonelowerBound=NoneupperBound=NonenumPartitions=Nonepredicates=Noneproperties=None)[source]

Construct a DataFrame representing the database table named table accessible via JDBC URL url and connection properties.

Partitions of the table will be retrieved in parallel if either column or predicates is specified. lowerBound`, ``upperBound and numPartitions is needed when column is specified.

If both column and predicates are specified, column will be used.

Note


Don’t create too many partitions in parallel on a large cluster; otherwise Spark might crash your external database systems.

Parameters:url – a JDBC URL of the form jdbc:subprotocol:subname

table – the name of the table

column – the name of an integer column that will be used for partitioning; if this parameter is specified, then numPartitions, lowerBound (inclusive), and upperBound (exclusive) will form partition strides for generated WHERE clause expressions used to split the column column evenly

lowerBound – the minimum value of column used to decide partition stride

upperBound – the maximum value of column used to decide partition stride

numPartitions – the number of partitions

predicates – a list of expressions suitable for inclusion in WHERE clauses; each one defines one partition of the DataFrame

properties – a dictionary of JDBC database connection arguments. Normally at least properties “user” and “password” with their corresponding values. For example { ‘user’ : ‘SYSTEM’, ‘password’ : ‘mypassword’ }

Returns:a DataFrame

New in version 1.4.

json(pathschema=NoneprimitivesAsString=NoneprefersDecimal=NoneallowComments=NoneallowUnquotedFieldNames=NoneallowSingleQuotes=NoneallowNumericLeadingZero=NoneallowBackslashEscapingAnyCharacter=Nonemode=NonecolumnNameOfCorruptRecord=NonedateFormat=NonetimestampFormat=NonemultiLine=NoneallowUnquotedControlChars=None)[source]

Loads JSON files and returns the results as a DataFrame.

JSON Lines (newline-delimited JSON) is supported by default. For JSON (one record per file), set the multiLine parameter to true.

If the schema parameter is not specified, this function goes through the input once to determine the input schema.

Parameters:path – string represents path to the JSON dataset, or a list of paths, or RDD of Strings storing JSON objects.

schema – an optional pyspark.sql.types.StructType for the input schema or a DDL-formatted string (For example col0 INT, col1DOUBLE).

primitivesAsString – infers all primitive values as a string type. If None is set, it uses the default value, false.

prefersDecimal – infers all floating-point values as a decimal type. If the values do not fit in decimal, then it infers them as doubles. If None is set, it uses the default value, false.

allowComments – ignores Java/C++ style comment in JSON records. If None is set, it uses the default value, false.

allowUnquotedFieldNames – allows unquoted JSON field names. If None is set, it uses the default value, false.

allowSingleQuotes – allows single quotes in addition to double quotes. If None is set, it uses the default value, true.

allowNumericLeadingZero – allows leading zeros in numbers (e.g. 00012). If None is set, it uses the default value, false.

allowBackslashEscapingAnyCharacter – allows accepting quoting of all character using backslash quoting mechanism. If None is set, it uses the default value, false.

mode –

allows a mode for dealing with corrupt records during parsing. If None is

set, it uses the default value, PERMISSIVE.

PERMISSIVE : when it meets a corrupted record, puts the malformed string into a field configured by columnNameOfCorruptRecord, and sets other fields to null. To keep corrupt records, an user can set a string type field named columnNameOfCorruptRecord in an user-defined schema. If a schema does not have the field, it drops corrupt records during parsing. When inferring a schema, it implicitly adds a columnNameOfCorruptRecord field in an output schema.

DROPMALFORMED : ignores the whole corrupted records.

FAILFAST : throws an exception when it meets corrupted records.

columnNameOfCorruptRecord – allows renaming the new field having malformed string created by PERMISSIVE mode. This overrides spark.sql.columnNameOfCorruptRecord. If None is set, it uses the value specified in spark.sql.columnNameOfCorruptRecord.

dateFormat – sets the string that indicates a date format. Custom date formats follow the formats at java.text.SimpleDateFormat. This applies to date type. If None is set, it uses the default value, yyyy-MM-dd.

timestampFormat – sets the string that indicates a timestamp format. Custom date formats follow the formats at java.text.SimpleDateFormat. This applies to timestamp type. If None is set, it uses the default value, yyyy-MM-dd'T'HH:mm:ss.SSSXXX.

multiLine – parse one record, which may span multiple lines, per file. If None is set, it uses the default value, false.

allowUnquotedControlChars – allows JSON Strings to contain unquoted control characters (ASCII characters with value less than 32, including tab and line feed characters) or not.

>>> df1=spark.read.json('python/test_support/sql/people.json')>>> df1.dtypes[('age', 'bigint'), ('name', 'string')]>>> rdd=sc.textFile('python/test_support/sql/people.json')>>> df2=spark.read.json(rdd)>>> df2.dtypes[('age', 'bigint'), ('name', 'string')]

New in version 1.4.

load(path=Noneformat=Noneschema=None**options)[source]

Loads data from a data source and returns it as a :class`DataFrame`.

Parameters:path – optional string or a list of string for file-system backed data sources.

format – optional string for format of the data source. Default to ‘parquet’.

schema – optional pyspark.sql.types.StructType for the input schema or a DDL-formatted string (For example col0 INT, col1DOUBLE).

options – all other string options

>>> df=spark.read.format("parquet").load('python/test_support/sql/parquet_partitioned',... opt1=True,opt2=1,opt3='str')>>> df.dtypes[('name', 'string'), ('year', 'int'), ('month', 'int'), ('day', 'int')]

>>> df=spark.read.format('json').load(['python/test_support/sql/people.json',... 'python/test_support/sql/people1.json'])>>> df.dtypes[('age', 'bigint'), ('aka', 'string'), ('name', 'string')]

New in version 1.4.

option(keyvalue)[source]

Adds an input option for the underlying data source.

You can set the following option(s) for reading files:

timeZone: sets the string that indicates a timezone to be used to parse timestamps

in the JSON/CSV datasources or partition values. If it isn’t set, it uses the default value, session local timezone.

New in version 1.5.

options(**options)[source]

Adds input options for the underlying data source.

You can set the following option(s) for reading files:

timeZone: sets the string that indicates a timezone to be used to parse timestamps

in the JSON/CSV datasources or partition values. If it isn’t set, it uses the default value, session local timezone.

New in version 1.4.

orc(path)[source]

Loads ORC files, returning the result as a DataFrame.

Note


Currently ORC support is only available together with Hive support.

>>> df=spark.read.orc('python/test_support/sql/orc_partitioned')>>> df.dtypes[('a', 'bigint'), ('b', 'int'), ('c', 'int')]

New in version 1.5.

parquet(*paths)[source]

Loads Parquet files, returning the result as a DataFrame.

You can set the following Parquet-specific option(s) for reading Parquet files:

mergeSchema: sets whether we should merge schemas collected from all Parquet part-files. This will override spark.sql.parquet.mergeSchema. The default value is specified in spark.sql.parquet.mergeSchema.

>>> df=spark.read.parquet('python/test_support/sql/parquet_partitioned')>>> df.dtypes[('name', 'string'), ('year', 'int'), ('month', 'int'), ('day', 'int')]

New in version 1.4.

schema(schema)[source]

Specifies the input schema.

Some data sources (e.g. JSON) can infer the input schema automatically from data. By specifying the schema here, the underlying data source can skip the schema inference step, and thus speed up data loading.

Parameters:schema – a pyspark.sql.types.StructType object or a DDL-formatted string (For example col0 INT, col1 DOUBLE).

>>> s=spark.read.schema("col0 INT, col1 DOUBLE")

New in version 1.4.

table(tableName)[source]

Returns the specified table as a DataFrame.

Parameters:tableName – string, name of the table.

>>> df=spark.read.parquet('python/test_support/sql/parquet_partitioned')>>> df.createOrReplaceTempView('tmpTable')>>> spark.read.table('tmpTable').dtypes[('name', 'string'), ('year', 'int'), ('month', 'int'), ('day', 'int')]

New in version 1.4.

text(pathswholetext=False)[source]

Loads text files and returns a DataFrame whose schema starts with a string column named “value”, and followed by partitioned columns if there are any.

Each line in the text file is a new row in the resulting DataFrame.

Parameters:paths – string, or list of strings, for input path(s).

wholetext – if true, read each file from input path(s) as a single row.

>>> df=spark.read.text('python/test_support/sql/text-test.txt')>>> df.collect()[Row(value=u'hello'), Row(value=u'this')]>>> df=spark.read.text('python/test_support/sql/text-test.txt',wholetext=True)>>> df.collect()[Row(value=u'hello\nthis')]

New in version 1.6.

class pyspark.sql.DataFrameWriter(df)[source]

Interface used to write a DataFrame to external storage systems (e.g. file systems, key-value stores, etc). Use DataFrame.write() to access this.

New in version 1.4.

bucketBy(numBucketscol*cols)[source]

Buckets the output by the given columns.If specified, the output is laid out on the file system similar to Hive’s bucketing scheme.

Parameters:numBuckets – the number of buckets to save

col – a name of a column, or a list of names.

cols – additional names (optional). If col is a list it should be empty.

Note


Applicable for file-based data sources in combination with DataFrameWriter.saveAsTable().

>>> (df.write.format('parquet')... .bucketBy(100,'year','month')... .mode("overwrite")... .saveAsTable('bucketed_table'))

New in version 2.3.

csv(pathmode=Nonecompression=Nonesep=Nonequote=Noneescape=Noneheader=NonenullValue=NoneescapeQuotes=NonequoteAll=NonedateFormat=NonetimestampFormat=NoneignoreLeadingWhiteSpace=NoneignoreTrailingWhiteSpace=NonecharToEscapeQuoteEscaping=None)[source]

Saves the content of the DataFrame in CSV format at the specified path.

Parameters:path – the path in any Hadoop supported file system

mode –

specifies the behavior of the save operation when data already exists.

append: Append contents of this DataFrame to existing data.

overwrite: Overwrite existing data.

ignore: Silently ignore this operation if data already exists.

error or errorifexists (default case): Throw an exception if data already exists.

compression – compression codec to use when saving to file. This can be one of the known case-insensitive shorten names (none, bzip2, gzip, lz4, snappy and deflate).

sep – sets a single character as a separator for each field and value. If None is set, it uses the default value, ,.

quote – sets a single character used for escaping quoted values where the separator can be part of the value. If None is set, it uses the default value, ". If an empty string is set, it uses u0000 (null character).

escape – sets a single character used for escaping quotes inside an already quoted value. If None is set, it uses the default value, \

escapeQuotes – a flag indicating whether values containing quotes should always be enclosed in quotes. If None is set, it uses the default value true, escaping all values containing a quote character.

quoteAll – a flag indicating whether all values should always be enclosed in quotes. If None is set, it uses the default value false, only escaping values containing a quote character.

header – writes the names of columns as the first line. If None is set, it uses the default value, false.

nullValue – sets the string representation of a null value. If None is set, it uses the default value, empty string.

dateFormat – sets the string that indicates a date format. Custom date formats follow the formats at java.text.SimpleDateFormat. This applies to date type. If None is set, it uses the default value, yyyy-MM-dd.

timestampFormat – sets the string that indicates a timestamp format. Custom date formats follow the formats at java.text.SimpleDateFormat. This applies to timestamp type. If None is set, it uses the default value, yyyy-MM-dd'T'HH:mm:ss.SSSXXX.

ignoreLeadingWhiteSpace – a flag indicating whether or not leading whitespaces from values being written should be skipped. If None is set, it uses the default value, true.

ignoreTrailingWhiteSpace – a flag indicating whether or not trailing whitespaces from values being written should be skipped. If None is set, it uses the default value, true.

charToEscapeQuoteEscaping – sets a single character used for escaping the escape for the quote character. If None is set, the default value is escape character when escape and quote characters are different, \ otherwise..

>>> df.write.csv(os.path.join(tempfile.mkdtemp(),'data'))

New in version 2.0.

format(source)[source]

Specifies the underlying output data source.

Parameters:source – string, name of the data source, e.g. ‘json’, ‘parquet’.

>>> df.write.format('json').save(os.path.join(tempfile.mkdtemp(),'data'))

New in version 1.4.

insertInto(tableNameoverwrite=False)[source]

Inserts the content of the DataFrame to the specified table.

It requires that the schema of the class:DataFrame is the same as the schema of the table.

Optionally overwriting any existing data.

New in version 1.4.

jdbc(urltablemode=Noneproperties=None)[source]

Saves the content of the DataFrame to an external database table via JDBC.

Note


Don’t create too many partitions in parallel on a large cluster; otherwise Spark might crash your external database systems.

Parameters:url – a JDBC URL of the form jdbc:subprotocol:subname

table – Name of the table in the external database.

mode –

specifies the behavior of the save operation when data already exists.

append: Append contents of this DataFrame to existing data.

overwrite: Overwrite existing data.

ignore: Silently ignore this operation if data already exists.

error or errorifexists (default case): Throw an exception if data already exists.

properties – a dictionary of JDBC database connection arguments. Normally at least properties “user” and “password” with their corresponding values. For example { ‘user’ : ‘SYSTEM’, ‘password’ : ‘mypassword’ }

New in version 1.4.

json(pathmode=Nonecompression=NonedateFormat=NonetimestampFormat=None)[source]

Saves the content of the DataFrame in JSON format (JSON Lines text format or newline-delimited JSON) at the specified path.

Parameters:path – the path in any Hadoop supported file system

mode –

specifies the behavior of the save operation when data already exists.

append: Append contents of this DataFrame to existing data.

overwrite: Overwrite existing data.

ignore: Silently ignore this operation if data already exists.

error or errorifexists (default case): Throw an exception if data already exists.

compression – compression codec to use when saving to file. This can be one of the known case-insensitive shorten names (none, bzip2, gzip, lz4, snappy and deflate).

dateFormat – sets the string that indicates a date format. Custom date formats follow the formats at java.text.SimpleDateFormat. This applies to date type. If None is set, it uses the default value, yyyy-MM-dd.

timestampFormat – sets the string that indicates a timestamp format. Custom date formats follow the formats at java.text.SimpleDateFormat. This applies to timestamp type. If None is set, it uses the default value, yyyy-MM-dd'T'HH:mm:ss.SSSXXX.

>>> df.write.json(os.path.join(tempfile.mkdtemp(),'data'))

New in version 1.4.

mode(saveMode)[source]

Specifies the behavior when data or table already exists.

Options include:

append: Append contents of this DataFrame to existing data.

overwrite: Overwrite existing data.

error or errorifexists: Throw an exception if data already exists.

ignore: Silently ignore this operation if data already exists.

>>> df.write.mode('append').parquet(os.path.join(tempfile.mkdtemp(),'data'))

New in version 1.4.

option(keyvalue)[source]

Adds an output option for the underlying data source.

You can set the following option(s) for writing files:

timeZone: sets the string that indicates a timezone to be used to format

timestamps in the JSON/CSV datasources or partition values. If it isn’t set, it uses the default value, session local timezone.

New in version 1.5.

options(**options)[source]

Adds output options for the underlying data source.

You can set the following option(s) for writing files:

timeZone: sets the string that indicates a timezone to be used to format

timestamps in the JSON/CSV datasources or partition values. If it isn’t set, it uses the default value, session local timezone.

New in version 1.4.

orc(pathmode=NonepartitionBy=Nonecompression=None)[source]

Saves the content of the DataFrame in ORC format at the specified path.

Note


Currently ORC support is only available together with Hive support.

Parameters:path – the path in any Hadoop supported file system

mode –

specifies the behavior of the save operation when data already exists.

append: Append contents of this DataFrame to existing data.

overwrite: Overwrite existing data.

ignore: Silently ignore this operation if data already exists.

error or errorifexists (default case): Throw an exception if data already exists.

partitionBy – names of partitioning columns

compression – compression codec to use when saving to file. This can be one of the known case-insensitive shorten names (none, snappy, zlib, and lzo). This will override orc.compress and spark.sql.orc.compression.codec. If None is set, it uses the value specified in spark.sql.orc.compression.codec.

>>> orc_df=spark.read.orc('python/test_support/sql/orc_partitioned')>>> orc_df.write.orc(os.path.join(tempfile.mkdtemp(),'data'))

New in version 1.5.

parquet(pathmode=NonepartitionBy=Nonecompression=None)[source]

Saves the content of the DataFrame in Parquet format at the specified path.

Parameters:path – the path in any Hadoop supported file system

mode –

specifies the behavior of the save operation when data already exists.

append: Append contents of this DataFrame to existing data.

overwrite: Overwrite existing data.

ignore: Silently ignore this operation if data already exists.

error or errorifexists (default case): Throw an exception if data already exists.

partitionBy – names of partitioning columns

compression – compression codec to use when saving to file. This can be one of the known case-insensitive shorten names (none, snappy, gzip, and lzo). This will override spark.sql.parquet.compression.codec. If None is set, it uses the value specified inspark.sql.parquet.compression.codec.

>>> df.write.parquet(os.path.join(tempfile.mkdtemp(),'data'))

New in version 1.4.

partitionBy(*cols)[source]

Partitions the output by the given columns on the file system.

If specified, the output is laid out on the file system similar to Hive’s partitioning scheme.

Parameters:cols – name of columns

>>> df.write.partitionBy('year','month').parquet(os.path.join(tempfile.mkdtemp(),'data'))

New in version 1.4.

save(path=Noneformat=Nonemode=NonepartitionBy=None**options)[source]

Saves the contents of the DataFrame to a data source.

The data source is specified by the format and a set of options. If format is not specified, the default data source configured byspark.sql.sources.default will be used.

Parameters:path – the path in a Hadoop supported file system

format – the format used to save

mode –

specifies the behavior of the save operation when data already exists.

append: Append contents of this DataFrame to existing data.

overwrite: Overwrite existing data.

ignore: Silently ignore this operation if data already exists.

error or errorifexists (default case): Throw an exception if data already exists.

partitionBy – names of partitioning columns

options – all other string options

>>> df.write.mode('append').parquet(os.path.join(tempfile.mkdtemp(),'data'))

New in version 1.4.

saveAsTable(nameformat=Nonemode=NonepartitionBy=None**options)[source]

Saves the content of the DataFrame as the specified table.

In the case the table already exists, behavior of this function depends on the save mode, specified by the mode function (default to throwing an exception). When mode is Overwrite, the schema of the DataFrame does not need to be the same as that of the existing table.

append: Append contents of this DataFrame to existing data.

overwrite: Overwrite existing data.

error or errorifexists: Throw an exception if data already exists.

ignore: Silently ignore this operation if data already exists.

Parameters:name – the table name

format – the format used to save

mode – one of append, overwrite, error, errorifexists, ignore (default: error)

partitionBy – names of partitioning columns

options – all other string options

New in version 1.4.

sortBy(col*cols)[source]

Sorts the output in each bucket by the given columns on the file system.

Parameters:col – a name of a column, or a list of names.

cols – additional names (optional). If col is a list it should be empty.

>>> (df.write.format('parquet')... .bucketBy(100,'year','month')... .sortBy('day')... .mode("overwrite")... .saveAsTable('sorted_bucketed_table'))

New in version 2.3.

text(pathcompression=None)[source]

Saves the content of the DataFrame in a text file at the specified path.

Parameters:path – the path in any Hadoop supported file system

compression – compression codec to use when saving to file. This can be one of the known case-insensitive shorten names (none, bzip2, gzip, lz4, snappy and deflate).

The DataFrame must have only one column that is of string type. Each row becomes a new line in the output file.

New in version 1.6.

pyspark.sql.types module

class pyspark.sql.types.DataType[source]

Base class for data types.

fromInternal(obj)[source]

Converts an internal SQL object into a native Python object.

json()[source]

jsonValue()[source]

needConversion()[source]

Does this type need to conversion between Python object and internal SQL object.

This is used to avoid the unnecessary conversion for ArrayType/MapType/StructType.

simpleString()[source]

toInternal(obj)[source]

Converts a Python object into an internal SQL object.

classmethod typeName()[source]

class pyspark.sql.types.NullType[source]

Null type.

The data type representing None, used for the types that cannot be inferred.

class pyspark.sql.types.StringType[source]

String data type.

class pyspark.sql.types.BinaryType[source]

Binary (byte array) data type.

class pyspark.sql.types.BooleanType[source]

Boolean data type.

class pyspark.sql.types.DateType[source]

Date (datetime.date) data type.

EPOCH_ORDINAL = 719163

fromInternal(v)[source]

needConversion()[source]

toInternal(d)[source]

class pyspark.sql.types.TimestampType[source]

Timestamp (datetime.datetime) data type.

fromInternal(ts)[source]

needConversion()[source]

toInternal(dt)[source]

class pyspark.sql.types.DecimalType(precision=10scale=0)[source]

Decimal (decimal.Decimal) data type.

The DecimalType must have fixed precision (the maximum total number of digits) and scale (the number of digits on the right of dot). For example, (5, 2) can support the value from [-999.99 to 999.99].

The precision can be up to 38, the scale must less or equal to precision.

When create a DecimalType, the default precision and scale is (10, 0). When infer schema from decimal.Decimal objects, it will be DecimalType(38, 18).

Parameters:precision – the maximum total number of digits (default: 10)

scale – the number of digits on right side of dot. (default: 0)

jsonValue()[source]

simpleString()[source]

class pyspark.sql.types.DoubleType[source]

Double data type, representing double precision floats.

class pyspark.sql.types.FloatType[source]

Float data type, representing single precision floats.

class pyspark.sql.types.ByteType[source]

Byte data type, i.e. a signed integer in a single byte.

simpleString()[source]

class pyspark.sql.types.IntegerType[source]

Int data type, i.e. a signed 32-bit integer.

simpleString()[source]

class pyspark.sql.types.LongType[source]

Long data type, i.e. a signed 64-bit integer.

If the values are beyond the range of [-9223372036854775808, 9223372036854775807], please use DecimalType.

simpleString()[source]

class pyspark.sql.types.ShortType[source]

Short data type, i.e. a signed 16-bit integer.

simpleString()[source]

class pyspark.sql.types.ArrayType(elementTypecontainsNull=True)[source]

Array data type.

Parameters:elementType – DataType of each element in the array.

containsNull – boolean, whether the array can contain null (None) values.

fromInternal(obj)[source]

classmethod fromJson(json)[source]

jsonValue()[source]

needConversion()[source]

simpleString()[source]

toInternal(obj)[source]

class pyspark.sql.types.MapType(keyTypevalueTypevalueContainsNull=True)[source]

Map data type.

Parameters:keyType – DataType of the keys in the map.

valueType – DataType of the values in the map.

valueContainsNull – indicates whether values can contain null (None) values.

Keys in a map data type are not allowed to be null (None).

fromInternal(obj)[source]

classmethod fromJson(json)[source]

jsonValue()[source]

needConversion()[source]

simpleString()[source]

toInternal(obj)[source]

class pyspark.sql.types.StructField(namedataTypenullable=Truemetadata=None)[source]

A field in StructType.

Parameters:name – string, name of the field.

dataType – DataType of the field.

nullable – boolean, whether the field can be null (None) or not.

metadata – a dict from string to simple type that can be toInternald to JSON automatically

fromInternal(obj)[source]

classmethod fromJson(json)[source]

jsonValue()[source]

needConversion()[source]

simpleString()[source]

toInternal(obj)[source]

typeName()[source]

class pyspark.sql.types.StructType(fields=None)[source]

Struct type, consisting of a list of StructField.

This is the data type representing a Row.

Iterating a StructType will iterate its StructFields. A contained StructField can be accessed by name or position.

>>> struct1=StructType([StructField("f1",StringType(),True)])>>> struct1["f1"]StructField(f1,StringType,true)>>> struct1[0]StructField(f1,StringType,true)

add(fielddata_type=Nonenullable=Truemetadata=None)[source]

Construct a StructType by adding new elements to it to define the schema. The method accepts either:

A single parameter which is a StructField object.

Between 2 and 4 parameters as (name, data_type, nullable (optional), metadata(optional). The data_type parameter may be either a String or a DataType object.

>>> struct1=StructType().add("f1",StringType(),True).add("f2",StringType(),True,None)>>> struct2=StructType([StructField("f1",StringType(),True),\... StructField("f2",StringType(),True,None)])>>> struct1==struct2True>>> struct1=StructType().add(StructField("f1",StringType(),True))>>> struct2=StructType([StructField("f1",StringType(),True)])>>> struct1==struct2True>>> struct1=StructType().add("f1","string",True)>>> struct2=StructType([StructField("f1",StringType(),True)])>>> struct1==struct2True

Parameters:field – Either the name of the field or a StructField object

data_type – If present, the DataType of the StructField to create

nullable – Whether the field to add should be nullable (default True)

metadata – Any additional metadata (default None)

Returns:a new updated StructType

fieldNames()[source]

Returns all field names in a list.

>>> struct=StructType([StructField("f1",StringType(),True)])>>> struct.fieldNames()['f1']

fromInternal(obj)[source]

classmethod fromJson(json)[source]

jsonValue()[source]

needConversion()[source]

simpleString()[source]

toInternal(obj)[source]

pyspark.sql.functions module

A collections of builtin functions

pyspark.sql.functions.abs(col)

Computes the absolute value.

New in version 1.3.

pyspark.sql.functions.acos(col)

Returns:inverse cosine of col, as if computed by java.lang.Math.acos()

New in version 1.4.

pyspark.sql.functions.add_months(startmonths)[source]

Returns the date that is months months after start

>>> df=spark.createDataFrame([('2015-04-08',)],['dt'])>>> df.select(add_months(df.dt,1).alias('next_month')).collect()[Row(next_month=datetime.date(2015, 5, 8))]

New in version 1.5.

pyspark.sql.functions.approxCountDistinct(colrsd=None)[source]

Note


Deprecated in 2.1, use approx_count_distinct() instead.

New in version 1.3.

pyspark.sql.functions.approx_count_distinct(colrsd=None)[source]

Aggregate function: returns a new Column for approximate distinct count of column col.

Parameters:rsd – maximum estimation error allowed (default = 0.05). For rsd < 0.01, it is more efficient to use countDistinct()

>>> df.agg(approx_count_distinct(df.age).alias('distinct_ages')).collect()[Row(distinct_ages=2)]

New in version 2.1.

pyspark.sql.functions.array(*cols)[source]

Creates a new array column.

Parameters:cols – list of column names (string) or list of Column expressions that have the same data type.

>>> df.select(array('age','age').alias("arr")).collect()[Row(arr=[2, 2]), Row(arr=[5, 5])]>>> df.select(array([df.age,df.age]).alias("arr")).collect()[Row(arr=[2, 2]), Row(arr=[5, 5])]

New in version 1.4.

pyspark.sql.functions.array_contains(colvalue)[source]

Collection function: returns null if the array is null, true if the array contains the given value, and false otherwise.

Parameters:col – name of column containing array

value – value to check for in array

>>> df=spark.createDataFrame([(["a","b","c"],),([],)],['data'])>>> df.select(array_contains(df.data,"a")).collect()[Row(array_contains(data, a)=True), Row(array_contains(data, a)=False)]

New in version 1.5.

pyspark.sql.functions.asc(col)

Returns a sort expression based on the ascending order of the given column name.

New in version 1.3.

pyspark.sql.functions.ascii(col)

Computes the numeric value of the first character of the string column.

New in version 1.5.

pyspark.sql.functions.asin(col)

Returns:inverse sine of col, as if computed by java.lang.Math.asin()

New in version 1.4.

pyspark.sql.functions.atan(col)

Returns:inverse tangent of col, as if computed by java.lang.Math.atan()

New in version 1.4.

pyspark.sql.functions.atan2(col1col2)

Parameters:col1 – coordinate on y-axis

col2 – coordinate on x-axis

Returns:the theta component of the point (r, theta) in polar coordinates that corresponds to the point (x, y) in Cartesian coordinates, as if computed by java.lang.Math.atan2()

New in version 1.4.

pyspark.sql.functions.avg(col)

Aggregate function: returns the average of the values in a group.

New in version 1.3.

pyspark.sql.functions.base64(col)

Computes the BASE64 encoding of a binary column and returns it as a string column.

New in version 1.5.

pyspark.sql.functions.bin(col)[source]

Returns the string representation of the binary value of the given column.

>>> df.select(bin(df.age).alias('c')).collect()[Row(c=u'10'), Row(c=u'101')]

New in version 1.5.

pyspark.sql.functions.bitwiseNOT(col)

Computes bitwise not.

New in version 1.4.

pyspark.sql.functions.broadcast(df)[source]

Marks a DataFrame as small enough for use in broadcast joins.

New in version 1.6.

pyspark.sql.functions.bround(colscale=0)[source]

Round the given value to scale decimal places using HALF_EVEN rounding mode if scale >= 0 or at integral part when scale < 0.

>>> spark.createDataFrame([(2.5,)],['a']).select(bround('a',0).alias('r')).collect()[Row(r=2.0)]

New in version 2.0.

pyspark.sql.functions.cbrt(col)

Computes the cube-root of the given value.

New in version 1.4.

pyspark.sql.functions.ceil(col)

Computes the ceiling of the given value.

New in version 1.4.

pyspark.sql.functions.coalesce(*cols)[source]

Returns the first column that is not null.

>>> cDf=spark.createDataFrame([(None,None),(1,None),(None,2)],("a","b"))>>> cDf.show()+----+----+|  a|  b|+----+----+|null|null||  1|null||null|  2|+----+----+

>>> cDf.select(coalesce(cDf["a"],cDf["b"])).show()+--------------+|coalesce(a, b)|+--------------+|          null||            1||            2|+--------------+

>>> cDf.select('*',coalesce(cDf["a"],lit(0.0))).show()+----+----+----------------+|  a|  b|coalesce(a, 0.0)|+----+----+----------------+|null|null|            0.0||  1|null|            1.0||null|  2|            0.0|+----+----+----------------+

New in version 1.4.

pyspark.sql.functions.col(col)

Returns a Column based on the given column name.

New in version 1.3.

pyspark.sql.functions.collect_list(col)

Aggregate function: returns a list of objects with duplicates.

>>> df2=spark.createDataFrame([(2,),(5,),(5,)],('age',))>>> df2.agg(collect_list('age')).collect()[Row(collect_list(age)=[2, 5, 5])]

New in version 1.6.

pyspark.sql.functions.collect_set(col)

Aggregate function: returns a set of objects with duplicate elements eliminated.

>>> df2=spark.createDataFrame([(2,),(5,),(5,)],('age',))>>> df2.agg(collect_set('age')).collect()[Row(collect_set(age)=[5, 2])]

New in version 1.6.

pyspark.sql.functions.column(col)

Returns a Column based on the given column name.

New in version 1.3.

pyspark.sql.functions.concat(*cols)[source]

Concatenates multiple input columns together into a single column. If all inputs are binary, concat returns an output as binary. Otherwise, it returns as string.

>>> df=spark.createDataFrame([('abcd','123')],['s','d'])>>> df.select(concat(df.s,df.d).alias('s')).collect()[Row(s=u'abcd123')]

New in version 1.5.

pyspark.sql.functions.concat_ws(sep*cols)[source]

Concatenates multiple input string columns together into a single string column, using the given separator.

>>> df=spark.createDataFrame([('abcd','123')],['s','d'])>>> df.select(concat_ws('-',df.s,df.d).alias('s')).collect()[Row(s=u'abcd-123')]

New in version 1.5.

pyspark.sql.functions.conv(colfromBasetoBase)[source]

Convert a number in a string column from one base to another.

>>> df=spark.createDataFrame([("010101",)],['n'])>>> df.select(conv(df.n,2,16).alias('hex')).collect()[Row(hex=u'15')]

New in version 1.5.

pyspark.sql.functions.corr(col1col2)[source]

Returns a new Column for the Pearson Correlation Coefficient for col1 and col2.

>>> a=range(20)>>> b=[2*xforxinrange(20)]>>> df=spark.createDataFrame(zip(a,b),["a","b"])>>> df.agg(corr("a","b").alias('c')).collect()[Row(c=1.0)]

New in version 1.6.

pyspark.sql.functions.cos(col)

Parameters:col – angle in radians

Returns:cosine of the angle, as if computed by java.lang.Math.cos().

New in version 1.4.

pyspark.sql.functions.cosh(col)

Parameters:col – hyperbolic angle

Returns:hyperbolic cosine of the angle, as if computed by java.lang.Math.cosh()

New in version 1.4.

pyspark.sql.functions.count(col)

Aggregate function: returns the number of items in a group.

New in version 1.3.

pyspark.sql.functions.countDistinct(col*cols)[source]

Returns a new Column for distinct count of col or cols.

>>> df.agg(countDistinct(df.age,df.name).alias('c')).collect()[Row(c=2)]

>>> df.agg(countDistinct("age","name").alias('c')).collect()[Row(c=2)]

New in version 1.3.

pyspark.sql.functions.covar_pop(col1col2)[source]

Returns a new Column for the population covariance of col1 and col2.

>>> a=[1]*10>>> b=[1]*10>>> df=spark.createDataFrame(zip(a,b),["a","b"])>>> df.agg(covar_pop("a","b").alias('c')).collect()[Row(c=0.0)]

New in version 2.0.

pyspark.sql.functions.covar_samp(col1col2)[source]

Returns a new Column for the sample covariance of col1 and col2.

>>> a=[1]*10>>> b=[1]*10>>> df=spark.createDataFrame(zip(a,b),["a","b"])>>> df.agg(covar_samp("a","b").alias('c')).collect()[Row(c=0.0)]

New in version 2.0.

pyspark.sql.functions.crc32(col)[source]

Calculates the cyclic redundancy check value (CRC32) of a binary column and returns the value as a bigint.

>>> spark.createDataFrame([('ABC',)],['a']).select(crc32('a').alias('crc32')).collect()[Row(crc32=2743272264)]

New in version 1.5.

pyspark.sql.functions.create_map(*cols)[source]

Creates a new map column.

Parameters:cols – list of column names (string) or list of Column expressions that are grouped as key-value pairs, e.g. (key1, value1, key2, value2, …).

>>> df.select(create_map('name','age').alias("map")).collect()[Row(map={u'Alice': 2}), Row(map={u'Bob': 5})]>>> df.select(create_map([df.name,df.age]).alias("map")).collect()[Row(map={u'Alice': 2}), Row(map={u'Bob': 5})]

New in version 2.0.

pyspark.sql.functions.cume_dist()

Window function: returns the cumulative distribution of values within a window partition, i.e. the fraction of rows that are below the current row.

New in version 1.6.

pyspark.sql.functions.current_date()[source]

Returns the current date as a DateType column.

New in version 1.5.

pyspark.sql.functions.current_timestamp()[source]

Returns the current timestamp as a TimestampType column.

pyspark.sql.functions.date_add(startdays)[source]

Returns the date that is days days after start

>>> df=spark.createDataFrame([('2015-04-08',)],['dt'])>>> df.select(date_add(df.dt,1).alias('next_date')).collect()[Row(next_date=datetime.date(2015, 4, 9))]

New in version 1.5.

pyspark.sql.functions.date_format(dateformat)[source]

Converts a date/timestamp/string to a value of string in the format specified by the date format given by the second argument.

A pattern could be for instance dd.MM.yyyy and could return a string like ‘18.03.1993’. All pattern letters of the Java class java.text.SimpleDateFormat can be used.

Note


Use when ever possible specialized functions like year. These benefit from a specialized implementation.

>>> df=spark.createDataFrame([('2015-04-08',)],['dt'])>>> df.select(date_format('dt','MM/dd/yyy').alias('date')).collect()[Row(date=u'04/08/2015')]

New in version 1.5.

pyspark.sql.functions.date_sub(startdays)[source]

Returns the date that is days days before start

>>> df=spark.createDataFrame([('2015-04-08',)],['dt'])>>> df.select(date_sub(df.dt,1).alias('prev_date')).collect()[Row(prev_date=datetime.date(2015, 4, 7))]

New in version 1.5.

pyspark.sql.functions.date_trunc(formattimestamp)[source]

Returns timestamp truncated to the unit specified by the format.

Parameters:format – ‘year’, ‘yyyy’, ‘yy’, ‘month’, ‘mon’, ‘mm’, ‘day’, ‘dd’, ‘hour’, ‘minute’, ‘second’, ‘week’, ‘quarter’

>>> df=spark.createDataFrame([('1997-02-28 05:02:11',)],['t'])>>> df.select(date_trunc('year',df.t).alias('year')).collect()[Row(year=datetime.datetime(1997, 1, 1, 0, 0))]>>> df.select(date_trunc('mon',df.t).alias('month')).collect()[Row(month=datetime.datetime(1997, 2, 1, 0, 0))]

New in version 2.3.

pyspark.sql.functions.datediff(endstart)[source]

Returns the number of days from start to end.

>>> df=spark.createDataFrame([('2015-04-08','2015-05-10')],['d1','d2'])>>> df.select(datediff(df.d2,df.d1).alias('diff')).collect()[Row(diff=32)]

New in version 1.5.

pyspark.sql.functions.dayofmonth(col)[source]

Extract the day of the month of a given date as integer.

>>> df=spark.createDataFrame([('2015-04-08',)],['dt'])>>> df.select(dayofmonth('dt').alias('day')).collect()[Row(day=8)]

New in version 1.5.

pyspark.sql.functions.dayofweek(col)[source]

Extract the day of the week of a given date as integer.

>>> df=spark.createDataFrame([('2015-04-08',)],['dt'])>>> df.select(dayofweek('dt').alias('day')).collect()[Row(day=4)]

New in version 2.3.

pyspark.sql.functions.dayofyear(col)[source]

Extract the day of the year of a given date as integer.

>>> df=spark.createDataFrame([('2015-04-08',)],['dt'])>>> df.select(dayofyear('dt').alias('day')).collect()[Row(day=98)]

New in version 1.5.

pyspark.sql.functions.decode(colcharset)[source]

Computes the first argument into a string from a binary using the provided character set (one of ‘US-ASCII’, ‘ISO-8859-1’, ‘UTF-8’, ‘UTF-16BE’, ‘UTF-16LE’, ‘UTF-16’).

New in version 1.5.

pyspark.sql.functions.degrees(col)

Converts an angle measured in radians to an approximately equivalent angle measured in degrees. :param col: angle in radians :return: angle in degrees, as if computed by java.lang.Math.toDegrees()

New in version 2.1.

pyspark.sql.functions.dense_rank()

Window function: returns the rank of rows within a window partition, without any gaps.

The difference between rank and dense_rank is that dense_rank leaves no gaps in ranking sequence when there are ties. That is, if you were ranking a competition using dense_rank and had three people tie for second place, you would say that all three were in second place and that the next person came in third. Rank would give me sequential numbers, making the person that came in third place (after the ties) would register as coming in fifth.

This is equivalent to the DENSE_RANK function in SQL.

New in version 1.6.

pyspark.sql.functions.desc(col)

Returns a sort expression based on the descending order of the given column name.

New in version 1.3.

pyspark.sql.functions.encode(colcharset)[source]

Computes the first argument into a binary from a string using the provided character set (one of ‘US-ASCII’, ‘ISO-8859-1’, ‘UTF-8’, ‘UTF-16BE’, ‘UTF-16LE’, ‘UTF-16’).

New in version 1.5.

pyspark.sql.functions.exp(col)

Computes the exponential of the given value.

New in version 1.4.

pyspark.sql.functions.explode(col)[source]

Returns a new row for each element in the given array or map.

>>> frompyspark.sqlimportRow>>> eDF=spark.createDataFrame([Row(a=1,intlist=[1,2,3],mapfield={"a":"b"})])>>> eDF.select(explode(eDF.intlist).alias("anInt")).collect()[Row(anInt=1), Row(anInt=2), Row(anInt=3)]

>>> eDF.select(explode(eDF.mapfield).alias("key","value")).show()+---+-----+|key|value|+---+-----+|  a|    b|+---+-----+

New in version 1.4.

pyspark.sql.functions.explode_outer(col)[source]

Returns a new row for each element in the given array or map. Unlike explode, if the array/map is null or empty then null is produced.

>>> df=spark.createDataFrame(... [(1,["foo","bar"],{"x":1.0}),(2,[],{}),(3,None,None)],... ("id","an_array","a_map")... )>>> df.select("id","an_array",explode_outer("a_map")).show()+---+----------+----+-----+| id|  an_array| key|value|+---+----------+----+-----+|  1|[foo, bar]|  x|  1.0||  2|        []|null| null||  3|      null|null| null|+---+----------+----+-----+

>>> df.select("id","a_map",explode_outer("an_array")).show()+---+----------+----+| id|    a_map| col|+---+----------+----+|  1|[x -> 1.0]| foo||  1|[x -> 1.0]| bar||  2|        []|null||  3|      null|null|+---+----------+----+

New in version 2.3.

pyspark.sql.functions.expm1(col)

Computes the exponential of the given value minus one.

New in version 1.4.

pyspark.sql.functions.expr(str)[source]

Parses the expression string into the column that it represents

>>> df.select(expr("length(name)")).collect()[Row(length(name)=5), Row(length(name)=3)]

New in version 1.5.

pyspark.sql.functions.factorial(col)[source]

Computes the factorial of the given value.

>>> df=spark.createDataFrame([(5,)],['n'])>>> df.select(factorial(df.n).alias('f')).collect()[Row(f=120)]

New in version 1.5.

pyspark.sql.functions.first(colignorenulls=False)[source]

Aggregate function: returns the first value in a group.

The function by default returns the first values it sees. It will return the first non-null value it sees when ignoreNulls is set to true. If all values are null, then null is returned.

New in version 1.3.

pyspark.sql.functions.floor(col)

Computes the floor of the given value.

New in version 1.4.

pyspark.sql.functions.format_number(cold)[source]

Formats the number X to a format like ‘#,–#,–#.–’, rounded to d decimal places with HALF_EVEN round mode, and returns the result as a string.

Parameters:col – the column name of the numeric value to be formatted

d – the N decimal places

>>> spark.createDataFrame([(5,)],['a']).select(format_number('a',4).alias('v')).collect()[Row(v=u'5.0000')]

New in version 1.5.

pyspark.sql.functions.format_string(format*cols)[source]

Formats the arguments in printf-style and returns the result as a string column.

Parameters:col – the column name of the numeric value to be formatted

d – the N decimal places

>>> df=spark.createDataFrame([(5,"hello")],['a','b'])>>> df.select(format_string('%d %s',df.a,df.b).alias('v')).collect()[Row(v=u'5 hello')]

New in version 1.5.

pyspark.sql.functions.from_json(colschemaoptions={})[source]

Parses a column containing a JSON string into a StructType or ArrayType of StructTypes with the specified schema. Returns null, in the case of an unparseable string.

Parameters:col – string column in json format

schema – a StructType or ArrayType of StructType to use when parsing the json column.

options – options to control parsing. accepts the same options as the json datasource

Note


Since Spark 2.3, the DDL-formatted string or a JSON format string is also supported for schema.

>>> frompyspark.sql.typesimport*>>> data=[(1,'''{"a": 1}''')]>>> schema=StructType([StructField("a",IntegerType())])>>> df=spark.createDataFrame(data,("key","value"))>>> df.select(from_json(df.value,schema).alias("json")).collect()[Row(json=Row(a=1))]>>> df.select(from_json(df.value,"a INT").alias("json")).collect()[Row(json=Row(a=1))]>>> data=[(1,'''[{"a": 1}]''')]>>> schema=ArrayType(StructType([StructField("a",IntegerType())]))>>> df=spark.createDataFrame(data,("key","value"))>>> df.select(from_json(df.value,schema).alias("json")).collect()[Row(json=[Row(a=1)])]

New in version 2.1.

pyspark.sql.functions.from_unixtime(timestampformat='yyyy-MM-dd HH:mm:ss')[source]

Converts the number of seconds from unix epoch (1970-01-01 00:00:00 UTC) to a string representing the timestamp of that moment in the current system time zone in the given format.

>>> spark.conf.set("spark.sql.session.timeZone","America/Los_Angeles")>>> time_df=spark.createDataFrame([(1428476400,)],['unix_time'])>>> time_df.select(from_unixtime('unix_time').alias('ts')).collect()[Row(ts=u'2015-04-08 00:00:00')]>>> spark.conf.unset("spark.sql.session.timeZone")

New in version 1.5.

pyspark.sql.functions.from_utc_timestamp(timestamptz)[source]

Given a timestamp like ‘2017-07-14 02:40:00.0’, interprets it as a time in UTC, and renders that time as a timestamp in the given time zone. For example, ‘GMT+1’ would yield ‘2017-07-14 03:40:00.0’.

>>> df=spark.createDataFrame([('1997-02-28 10:30:00',)],['t'])>>> df.select(from_utc_timestamp(df.t,"PST").alias('local_time')).collect()[Row(local_time=datetime.datetime(1997, 2, 28, 2, 30))]

New in version 1.5.

pyspark.sql.functions.get_json_object(colpath)[source]

Extracts json object from a json string based on json path specified, and returns json string of the extracted json object. It will return null if the input json string is invalid.

Parameters:col – string column in json format

path – path to the json object to extract

>>> data=[("1",'''{"f1": "value1", "f2": "value2"}'''),("2",'''{"f1": "value12"}''')]>>> df=spark.createDataFrame(data,("key","jstring"))>>> df.select(df.key,get_json_object(df.jstring,'$.f1').alias("c0"),\... get_json_object(df.jstring,'$.f2').alias("c1")).collect()[Row(key=u'1', c0=u'value1', c1=u'value2'), Row(key=u'2', c0=u'value12', c1=None)]

New in version 1.6.

pyspark.sql.functions.greatest(*cols)[source]

Returns the greatest value of the list of column names, skipping null values. This function takes at least 2 parameters. It will return null iff all parameters are null.

>>> df=spark.createDataFrame([(1,4,3)],['a','b','c'])>>> df.select(greatest(df.a,df.b,df.c).alias("greatest")).collect()[Row(greatest=4)]

New in version 1.5.

pyspark.sql.functions.grouping(col)[source]

Aggregate function: indicates whether a specified column in a GROUP BY list is aggregated or not, returns 1 for aggregated or 0 for not aggregated in the result set.

>>> df.cube("name").agg(grouping("name"),sum("age")).orderBy("name").show()+-----+--------------+--------+| name|grouping(name)|sum(age)|+-----+--------------+--------+| null|            1|      7||Alice|            0|      2||  Bob|            0|      5|+-----+--------------+--------+

New in version 2.0.

pyspark.sql.functions.grouping_id(*cols)[source]

Aggregate function: returns the level of grouping, equals to

(grouping(c1) << (n-1)) + (grouping(c2) << (n-2)) + … + grouping(cn)

Note


The list of columns should match with grouping columns exactly, or empty (means all the grouping columns).

>>> df.cube("name").agg(grouping_id(),sum("age")).orderBy("name").show()+-----+-------------+--------+| name|grouping_id()|sum(age)|+-----+-------------+--------+| null|            1|      7||Alice|            0|      2||  Bob|            0|      5|+-----+-------------+--------+

New in version 2.0.

pyspark.sql.functions.hash(*cols)[source]

Calculates the hash code of given columns, and returns the result as an int column.

>>> spark.createDataFrame([('ABC',)],['a']).select(hash('a').alias('hash')).collect()[Row(hash=-757602832)]

New in version 2.0.

pyspark.sql.functions.hex(col)[source]

Computes hex value of the given column, which could be pyspark.sql.types.StringTypepyspark.sql.types.BinaryTypepyspark.sql.types.IntegerType orpyspark.sql.types.LongType.

>>> spark.createDataFrame([('ABC',3)],['a','b']).select(hex('a'),hex('b')).collect()[Row(hex(a)=u'414243', hex(b)=u'3')]

New in version 1.5.

pyspark.sql.functions.hour(col)[source]

Extract the hours of a given date as integer.

>>> df=spark.createDataFrame([('2015-04-08 13:08:15',)],['ts'])>>> df.select(hour('ts').alias('hour')).collect()[Row(hour=13)]

New in version 1.5.

pyspark.sql.functions.hypot(col1col2)

Computes sqrt(a^2 + b^2) without intermediate overflow or underflow.

New in version 1.4.

pyspark.sql.functions.initcap(col)[source]

Translate the first letter of each word to upper case in the sentence.

>>> spark.createDataFrame([('ab cd',)],['a']).select(initcap("a").alias('v')).collect()[Row(v=u'Ab Cd')]

New in version 1.5.

pyspark.sql.functions.input_file_name()[source]

Creates a string column for the file name of the current Spark task.

New in version 1.6.

pyspark.sql.functions.instr(strsubstr)[source]

Locate the position of the first occurrence of substr column in the given string. Returns null if either of the arguments are null.

Note


The position is not zero based, but 1 based index. Returns 0 if substr could not be found in str.

>>> df=spark.createDataFrame([('abcd',)],['s',])>>> df.select(instr(df.s,'b').alias('s')).collect()[Row(s=2)]

New in version 1.5.

pyspark.sql.functions.isnan(col)[source]

An expression that returns true iff the column is NaN.

>>> df=spark.createDataFrame([(1.0,float('nan')),(float('nan'),2.0)],("a","b"))>>> df.select(isnan("a").alias("r1"),isnan(df.a).alias("r2")).collect()[Row(r1=False, r2=False), Row(r1=True, r2=True)]

New in version 1.6.

pyspark.sql.functions.isnull(col)[source]

An expression that returns true iff the column is null.

>>> df=spark.createDataFrame([(1,None),(None,2)],("a","b"))>>> df.select(isnull("a").alias("r1"),isnull(df.a).alias("r2")).collect()[Row(r1=False, r2=False), Row(r1=True, r2=True)]

New in version 1.6.

pyspark.sql.functions.json_tuple(col*fields)[source]

Creates a new row for a json column according to the given field names.

Parameters:col – string column in json format

fields – list of fields to extract

>>> data=[("1",'''{"f1": "value1", "f2": "value2"}'''),("2",'''{"f1": "value12"}''')]>>> df=spark.createDataFrame(data,("key","jstring"))>>> df.select(df.key,json_tuple(df.jstring,'f1','f2')).collect()[Row(key=u'1', c0=u'value1', c1=u'value2'), Row(key=u'2', c0=u'value12', c1=None)]

New in version 1.6.

pyspark.sql.functions.kurtosis(col)

Aggregate function: returns the kurtosis of the values in a group.

New in version 1.6.

pyspark.sql.functions.lag(colcount=1default=None)[source]

Window function: returns the value that is offset rows before the current row, and defaultValue if there is less than offset rows before the current row. For example, an offset of one will return the previous row at any given point in the window partition.

This is equivalent to the LAG function in SQL.

Parameters:col – name of column or expression

count – number of row to extend

default – default value

New in version 1.4.

pyspark.sql.functions.last(colignorenulls=False)[source]

Aggregate function: returns the last value in a group.

The function by default returns the last values it sees. It will return the last non-null value it sees when ignoreNulls is set to true. If all values are null, then null is returned.

New in version 1.3.

pyspark.sql.functions.last_day(date)[source]

Returns the last day of the month which the given date belongs to.

>>> df=spark.createDataFrame([('1997-02-10',)],['d'])>>> df.select(last_day(df.d).alias('date')).collect()[Row(date=datetime.date(1997, 2, 28))]

New in version 1.5.

pyspark.sql.functions.lead(colcount=1default=None)[source]

Window function: returns the value that is offset rows after the current row, and defaultValue if there is less than offset rows after the current row. For example, an offset of one will return the next row at any given point in the window partition.

This is equivalent to the LEAD function in SQL.

Parameters:col – name of column or expression

count – number of row to extend

default – default value

New in version 1.4.

pyspark.sql.functions.least(*cols)[source]

Returns the least value of the list of column names, skipping null values. This function takes at least 2 parameters. It will return null iff all parameters are null.

>>> df=spark.createDataFrame([(1,4,3)],['a','b','c'])>>> df.select(least(df.a,df.b,df.c).alias("least")).collect()[Row(least=1)]

New in version 1.5.

pyspark.sql.functions.length(col)[source]

Computes the character length of string data or number of bytes of binary data. The length of character data includes the trailing spaces. The length of binary data includes binary zeros.

>>> spark.createDataFrame([('ABC ',)],['a']).select(length('a').alias('length')).collect()[Row(length=4)]

New in version 1.5.

pyspark.sql.functions.levenshtein(leftright)[source]

Computes the Levenshtein distance of the two given strings.

>>> df0=spark.createDataFrame([('kitten','sitting',)],['l','r'])>>> df0.select(levenshtein('l','r').alias('d')).collect()[Row(d=3)]

New in version 1.5.

pyspark.sql.functions.lit(col)

Creates a Column of literal value.

>>> df.select(lit(5).alias('height')).withColumn('spark_user',lit(True)).take(1)[Row(height=5, spark_user=True)]

New in version 1.3.

pyspark.sql.functions.locate(substrstrpos=1)[source]

Locate the position of the first occurrence of substr in a string column, after position pos.

Note


The position is not zero based, but 1 based index. Returns 0 if substr could not be found in str.

Parameters:substr – a string

str – a Column of pyspark.sql.types.StringType

pos – start position (zero based)

>>> df=spark.createDataFrame([('abcd',)],['s',])>>> df.select(locate('b',df.s,1).alias('s')).collect()[Row(s=2)]

New in version 1.5.

pyspark.sql.functions.log(arg1arg2=None)[source]

Returns the first argument-based logarithm of the second argument.

If there is only one argument, then this takes the natural logarithm of the argument.

>>> df.select(log(10.0,df.age).alias('ten')).rdd.map(lambdal:str(l.ten)[:7]).collect()['0.30102', '0.69897']

>>> df.select(log(df.age).alias('e')).rdd.map(lambdal:str(l.e)[:7]).collect()['0.69314', '1.60943']

New in version 1.5.

pyspark.sql.functions.log10(col)

Computes the logarithm of the given value in Base 10.

New in version 1.4.

pyspark.sql.functions.log1p(col)

Computes the natural logarithm of the given value plus one.

New in version 1.4.

pyspark.sql.functions.log2(col)[source]

Returns the base-2 logarithm of the argument.

>>> spark.createDataFrame([(4,)],['a']).select(log2('a').alias('log2')).collect()[Row(log2=2.0)]

New in version 1.5.

pyspark.sql.functions.lower(col)

Converts a string column to lower case.

New in version 1.5.

pyspark.sql.functions.lpad(collenpad)[source]

Left-pad the string column to width len with pad.

>>> df=spark.createDataFrame([('abcd',)],['s',])>>> df.select(lpad(df.s,6,'#').alias('s')).collect()[Row(s=u'##abcd')]

New in version 1.5.

pyspark.sql.functions.ltrim(col)

Trim the spaces from left end for the specified string value.

New in version 1.5.

pyspark.sql.functions.map_keys(col)[source]

Collection function: Returns an unordered array containing the keys of the map.

Parameters:col – name of column or expression

>>> frompyspark.sql.functionsimportmap_keys>>> df=spark.sql("SELECT map(1, 'a', 2, 'b') as data")>>> df.select(map_keys("data").alias("keys")).show()+------+|  keys|+------+|[1, 2]|+------+

New in version 2.3.

pyspark.sql.functions.map_values(col)[source]

Collection function: Returns an unordered array containing the values of the map.

Parameters:col – name of column or expression

>>> frompyspark.sql.functionsimportmap_values>>> df=spark.sql("SELECT map(1, 'a', 2, 'b') as data")>>> df.select(map_values("data").alias("values")).show()+------+|values|+------+|[a, b]|+------+

New in version 2.3.

pyspark.sql.functions.max(col)

Aggregate function: returns the maximum value of the expression in a group.

New in version 1.3.

pyspark.sql.functions.md5(col)[source]

Calculates the MD5 digest and returns the value as a 32 character hex string.

>>> spark.createDataFrame([('ABC',)],['a']).select(md5('a').alias('hash')).collect()[Row(hash=u'902fbdd2b1df0c4f70b4a5d23525e932')]

New in version 1.5.

pyspark.sql.functions.mean(col)

Aggregate function: returns the average of the values in a group.

New in version 1.3.

pyspark.sql.functions.min(col)

Aggregate function: returns the minimum value of the expression in a group.

New in version 1.3.

pyspark.sql.functions.minute(col)[source]

Extract the minutes of a given date as integer.

>>> df=spark.createDataFrame([('2015-04-08 13:08:15',)],['ts'])>>> df.select(minute('ts').alias('minute')).collect()[Row(minute=8)]

New in version 1.5.

pyspark.sql.functions.monotonically_increasing_id()[source]

A column that generates monotonically increasing 64-bit integers.

The generated ID is guaranteed to be monotonically increasing and unique, but not consecutive. The current implementation puts the partition ID in the upper 31 bits, and the record number within each partition in the lower 33 bits. The assumption is that the data frame has less than 1 billion partitions, and each partition has less than 8 billion records.

As an example, consider a DataFrame with two partitions, each with 3 records. This expression would return the following IDs: 0, 1, 2, 8589934592 (1L << 33), 8589934593, 8589934594.

>>> df0=sc.parallelize(range(2),2).mapPartitions(lambdax:[(1,),(2,),(3,)]).toDF(['col1'])>>> df0.select(monotonically_increasing_id().alias('id')).collect()[Row(id=0), Row(id=1), Row(id=2), Row(id=8589934592), Row(id=8589934593), Row(id=8589934594)]

New in version 1.6.

pyspark.sql.functions.month(col)[source]

Extract the month of a given date as integer.

>>> df=spark.createDataFrame([('2015-04-08',)],['dt'])>>> df.select(month('dt').alias('month')).collect()[Row(month=4)]

New in version 1.5.

pyspark.sql.functions.months_between(date1date2)[source]

Returns the number of months between date1 and date2.

>>> df=spark.createDataFrame([('1997-02-28 10:30:00','1996-10-30')],['date1','date2'])>>> df.select(months_between(df.date1,df.date2).alias('months')).collect()[Row(months=3.9495967...)]

New in version 1.5.

pyspark.sql.functions.nanvl(col1col2)[source]

Returns col1 if it is not NaN, or col2 if col1 is NaN.

Both inputs should be floating point columns (DoubleType or FloatType).

>>> df=spark.createDataFrame([(1.0,float('nan')),(float('nan'),2.0)],("a","b"))>>> df.select(nanvl("a","b").alias("r1"),nanvl(df.a,df.b).alias("r2")).collect()[Row(r1=1.0, r2=1.0), Row(r1=2.0, r2=2.0)]

New in version 1.6.

pyspark.sql.functions.next_day(datedayOfWeek)[source]

Returns the first date which is later than the value of the date column.

Day of the week parameter is case insensitive, and accepts:

“Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”, “Sun”.

>>> df=spark.createDataFrame([('2015-07-27',)],['d'])>>> df.select(next_day(df.d,'Sun').alias('date')).collect()[Row(date=datetime.date(2015, 8, 2))]

New in version 1.5.

pyspark.sql.functions.ntile(n)[source]

Window function: returns the ntile group id (from 1 to n inclusive) in an ordered window partition. For example, if n is 4, the first quarter of the rows will get value 1, the second quarter will get 2, the third quarter will get 3, and the last quarter will get 4.

This is equivalent to the NTILE function in SQL.

Parameters:n – an integer

New in version 1.4.

pyspark.sql.functions.pandas_udf(f=NonereturnType=NonefunctionType=None)[source]

Creates a vectorized user defined function (UDF).

Parameters:f – user-defined function. A python function if used as a standalone function

returnType – the return type of the user-defined function. The value can be either a pyspark.sql.types.DataType object or a DDL-formatted type string.

functionType – an enum value in pyspark.sql.functions.PandasUDFType. Default: SCALAR.

Note


Experimental

The function type of the UDF can be one of the following:

SCALAR

A scalar UDF defines a transformation: One or more pandas.Series -> A pandas.Series. The returnType should be a primitive data type, e.g., DoubleType(). The length of the returned pandas.Series must be of the same as the input pandas.Series.

Scalar UDFs are used with pyspark.sql.DataFrame.withColumn() and pyspark.sql.DataFrame.select().

>>> frompyspark.sql.functionsimportpandas_udf,PandasUDFType>>> frompyspark.sql.typesimportIntegerType,StringType>>> slen=pandas_udf(lambdas:s.str.len(),IntegerType())>>> :pandas_udf(StringType())... defto_upper(s):... returns.str.upper()...>>> :pandas_udf("integer",PandasUDFType.SCALAR)... defadd_one(x):... returnx+1...>>> df=spark.createDataFrame([(1,"John Doe",21)],... ("id","name","age"))>>> df.select(slen("name").alias("slen(name)"),to_upper("name"),add_one("age"))\... .show()+----------+--------------+------------+|slen(name)|to_upper(name)|add_one(age)|+----------+--------------+------------+|        8|      JOHN DOE|          22|+----------+--------------+------------+

Note


The length of pandas.Series within a scalar UDF is not that of the whole input column, but is the length of an internal batch used for each call to the function. Therefore, this can be used, for example, to ensure the length of each returned pandas.Series, and can not be used as the column length.

GROUPED_MAP

A grouped map UDF defines transformation: A pandas.DataFrame -> A pandas.DataFrame The returnType should be a StructType describing the schema of the returned pandas.DataFrame. The length of the returned pandas.DataFrame can be arbitrary and the columns must be indexed so that their position matches the corresponding field in the schema.

Grouped map UDFs are used with pyspark.sql.GroupedData.apply().

>>> frompyspark.sql.functionsimportpandas_udf,PandasUDFType>>> df=spark.createDataFrame(... [(1,1.0),(1,2.0),(2,3.0),(2,5.0),(2,10.0)],... ("id","v"))>>> :pandas_udf("id long, v double",PandasUDFType.GROUPED_MAP)... defnormalize(pdf):... v=pdf.v... returnpdf.assign(v=(v-v.mean())/v.std())>>> df.groupby("id").apply(normalize).show()+---+-------------------+| id|                  v|+---+-------------------+|  1|-0.7071067811865475||  1| 0.7071067811865475||  2|-0.8320502943378437||  2|-0.2773500981126146||  2| 1.1094003924504583|+---+-------------------+

Note


If returning a new pandas.DataFrame constructed with a dictionary, it is recommended to explicitly index the columns by name to ensure the positions are correct, or alternatively use an OrderedDict. For example, pd.DataFrame({‘id’: ids, ‘a’: data}, columns=[‘id’, ‘a’]) orpd.DataFrame(OrderedDict([(‘id’, ids), (‘a’, data)])).

See also


pyspark.sql.GroupedData.apply()

Note


The user-defined functions are considered deterministic by default. Due to optimization, duplicate invocations may be eliminated or the function may even be invoked more times than it is present in the query. If your function is not deterministic, call asNondeterministic on the user defined function. E.g.:

>>> :pandas_udf('double',PandasUDFType.SCALAR)... defrandom(v):... importnumpyasnp... importpandasaspd... returnpd.Series(np.random.randn(len(v))>>> random=random.asNondeterministic()

Note


The user-defined functions do not support conditional expressions or short circuiting in boolean expressions and it ends up with being executed all internally. If the functions can fail on special rows, the workaround is to incorporate the condition into the functions.

Note


The user-defined functions do not take keyword arguments on the calling side.

New in version 2.3.

pyspark.sql.functions.percent_rank()

Window function: returns the relative rank (i.e. percentile) of rows within a window partition.

New in version 1.6.

pyspark.sql.functions.posexplode(col)[source]

Returns a new row for each element with position in the given array or map.

>>> frompyspark.sqlimportRow>>> eDF=spark.createDataFrame([Row(a=1,intlist=[1,2,3],mapfield={"a":"b"})])>>> eDF.select(posexplode(eDF.intlist)).collect()[Row(pos=0, col=1), Row(pos=1, col=2), Row(pos=2, col=3)]

>>> eDF.select(posexplode(eDF.mapfield)).show()+---+---+-----+|pos|key|value|+---+---+-----+|  0|  a|    b|+---+---+-----+

New in version 2.1.

pyspark.sql.functions.posexplode_outer(col)[source]

Returns a new row for each element with position in the given array or map. Unlike posexplode, if the array/map is null or empty then the row (null, null) is produced.

>>> df=spark.createDataFrame(... [(1,["foo","bar"],{"x":1.0}),(2,[],{}),(3,None,None)],... ("id","an_array","a_map")... )>>> df.select("id","an_array",posexplode_outer("a_map")).show()+---+----------+----+----+-----+| id|  an_array| pos| key|value|+---+----------+----+----+-----+|  1|[foo, bar]|  0|  x|  1.0||  2|        []|null|null| null||  3|      null|null|null| null|+---+----------+----+----+-----+>>> df.select("id","a_map",posexplode_outer("an_array")).show()+---+----------+----+----+| id|    a_map| pos| col|+---+----------+----+----+|  1|[x -> 1.0]|  0| foo||  1|[x -> 1.0]|  1| bar||  2|        []|null|null||  3|      null|null|null|+---+----------+----+----+

New in version 2.3.

pyspark.sql.functions.pow(col1col2)

Returns the value of the first argument raised to the power of the second argument.

New in version 1.4.

pyspark.sql.functions.quarter(col)[source]

Extract the quarter of a given date as integer.

>>> df=spark.createDataFrame([('2015-04-08',)],['dt'])>>> df.select(quarter('dt').alias('quarter')).collect()[Row(quarter=2)]

New in version 1.5.

pyspark.sql.functions.radians(col)

Converts an angle measured in degrees to an approximately equivalent angle measured in radians. :param col: angle in degrees :return: angle in radians, as if computed by java.lang.Math.toRadians()

New in version 2.1.

pyspark.sql.functions.rand(seed=None)[source]

Generates a random column with independent and identically distributed (i.i.d.) samples from U[0.0, 1.0].

>>> df.withColumn('rand',rand(seed=42)*3).collect()[Row(age=2, name=u'Alice', rand=1.1568609015300986), Row(age=5, name=u'Bob', rand=1.403379671529166)]

New in version 1.4.

pyspark.sql.functions.randn(seed=None)[source]

Generates a column with independent and identically distributed (i.i.d.) samples from the standard normal distribution.

>>> df.withColumn('randn',randn(seed=42)).collect()[Row(age=2, name=u'Alice', randn=-0.7556247885860078),Row(age=5, name=u'Bob', randn=-0.0861619008451133)]

New in version 1.4.

pyspark.sql.functions.rank()

Window function: returns the rank of rows within a window partition.

The difference between rank and dense_rank is that dense_rank leaves no gaps in ranking sequence when there are ties. That is, if you were ranking a competition using dense_rank and had three people tie for second place, you would say that all three were in second place and that the next person came in third. Rank would give me sequential numbers, making the person that came in third place (after the ties) would register as coming in fifth.

This is equivalent to the RANK function in SQL.

New in version 1.6.

pyspark.sql.functions.regexp_extract(strpatternidx)[source]

Extract a specific group matched by a Java regex, from the specified string column. If the regex did not match, or the specified group did not match, an empty string is returned.

>>> df=spark.createDataFrame([('100-200',)],['str'])>>> df.select(regexp_extract('str','(\d+)-(\d+)',1).alias('d')).collect()[Row(d=u'100')]>>> df=spark.createDataFrame([('foo',)],['str'])>>> df.select(regexp_extract('str','(\d+)',1).alias('d')).collect()[Row(d=u'')]>>> df=spark.createDataFrame([('aaaac',)],['str'])>>> df.select(regexp_extract('str','(a+)(b)?(c)',2).alias('d')).collect()[Row(d=u'')]

New in version 1.5.

pyspark.sql.functions.regexp_replace(strpatternreplacement)[source]

Replace all substrings of the specified string value that match regexp with rep.

>>> df=spark.createDataFrame([('100-200',)],['str'])>>> df.select(regexp_replace('str','(\d+)','--').alias('d')).collect()[Row(d=u'-----')]

New in version 1.5.

pyspark.sql.functions.repeat(coln)[source]

Repeats a string column n times, and returns it as a new string column.

>>> df=spark.createDataFrame([('ab',)],['s',])>>> df.select(repeat(df.s,3).alias('s')).collect()[Row(s=u'ababab')]

New in version 1.5.

pyspark.sql.functions.reverse(col)

Reverses the string column and returns it as a new string column.

New in version 1.5.

pyspark.sql.functions.rint(col)

Returns the double value that is closest in value to the argument and is equal to a mathematical integer.

New in version 1.4.

pyspark.sql.functions.round(colscale=0)[source]

Round the given value to scale decimal places using HALF_UP rounding mode if scale >= 0 or at integral part when scale < 0.

>>> spark.createDataFrame([(2.5,)],['a']).select(round('a',0).alias('r')).collect()[Row(r=3.0)]

New in version 1.5.

pyspark.sql.functions.row_number()

Window function: returns a sequential number starting at 1 within a window partition.

New in version 1.6.

pyspark.sql.functions.rpad(collenpad)[source]

Right-pad the string column to width len with pad.

>>> df=spark.createDataFrame([('abcd',)],['s',])>>> df.select(rpad(df.s,6,'#').alias('s')).collect()[Row(s=u'abcd##')]

New in version 1.5.

pyspark.sql.functions.rtrim(col)

Trim the spaces from right end for the specified string value.

New in version 1.5.

pyspark.sql.functions.second(col)[source]

Extract the seconds of a given date as integer.

>>> df=spark.createDataFrame([('2015-04-08 13:08:15',)],['ts'])>>> df.select(second('ts').alias('second')).collect()[Row(second=15)]

New in version 1.5.

pyspark.sql.functions.sha1(col)[source]

Returns the hex string result of SHA-1.

>>> spark.createDataFrame([('ABC',)],['a']).select(sha1('a').alias('hash')).collect()[Row(hash=u'3c01bdbb26f358bab27f267924aa2c9a03fcfdb8')]

New in version 1.5.

pyspark.sql.functions.sha2(colnumBits)[source]

Returns the hex string result of SHA-2 family of hash functions (SHA-224, SHA-256, SHA-384, and SHA-512). The numBits indicates the desired bit length of the result, which must have a value of 224, 256, 384, 512, or 0 (which is equivalent to 256).

>>> digests=df.select(sha2(df.name,256).alias('s')).collect()>>> digests[0]Row(s=u'3bc51062973c458d5a6f2d8d64a023246354ad7e064b1e4e009ec8a0699a3043')>>> digests[1]Row(s=u'cd9fb1e148ccd8442e5aa74904cc73bf6fb54d1d54d333bd596aa9bb4bb4e961')

New in version 1.5.

pyspark.sql.functions.shiftLeft(colnumBits)[source]

Shift the given value numBits left.

>>> spark.createDataFrame([(21,)],['a']).select(shiftLeft('a',1).alias('r')).collect()[Row(r=42)]

New in version 1.5.

pyspark.sql.functions.shiftRight(colnumBits)[source]

(Signed) shift the given value numBits right.

>>> spark.createDataFrame([(42,)],['a']).select(shiftRight('a',1).alias('r')).collect()[Row(r=21)]

New in version 1.5.

pyspark.sql.functions.shiftRightUnsigned(colnumBits)[source]

Unsigned shift the given value numBits right.

>>> df=spark.createDataFrame([(-42,)],['a'])>>> df.select(shiftRightUnsigned('a',1).alias('r')).collect()[Row(r=9223372036854775787)]

New in version 1.5.

pyspark.sql.functions.signum(col)

Computes the signum of the given value.

New in version 1.4.

pyspark.sql.functions.sin(col)

Parameters:col – angle in radians

Returns:sine of the angle, as if computed by java.lang.Math.sin()

New in version 1.4.

pyspark.sql.functions.sinh(col)

Parameters:col – hyperbolic angle

Returns:hyperbolic sine of the given value, as if computed by java.lang.Math.sinh()

New in version 1.4.

pyspark.sql.functions.size(col)[source]

Collection function: returns the length of the array or map stored in the column.

Parameters:col – name of column or expression

>>> df=spark.createDataFrame([([1,2,3],),([1],),([],)],['data'])>>> df.select(size(df.data)).collect()[Row(size(data)=3), Row(size(data)=1), Row(size(data)=0)]

New in version 1.5.

pyspark.sql.functions.skewness(col)

Aggregate function: returns the skewness of the values in a group.

New in version 1.6.

pyspark.sql.functions.sort_array(colasc=True)[source]

Collection function: sorts the input array in ascending or descending order according to the natural ordering of the array elements.

Parameters:col – name of column or expression

>>> df=spark.createDataFrame([([2,1,3],),([1],),([],)],['data'])>>> df.select(sort_array(df.data).alias('r')).collect()[Row(r=[1, 2, 3]), Row(r=[1]), Row(r=[])]>>> df.select(sort_array(df.data,asc=False).alias('r')).collect()[Row(r=[3, 2, 1]), Row(r=[1]), Row(r=[])]

New in version 1.5.

pyspark.sql.functions.soundex(col)[source]

Returns the SoundEx encoding for a string

>>> df=spark.createDataFrame([("Peters",),("Uhrbach",)],['name'])>>> df.select(soundex(df.name).alias("soundex")).collect()[Row(soundex=u'P362'), Row(soundex=u'U612')]

New in version 1.5.

pyspark.sql.functions.spark_partition_id()[source]

A column for partition ID.

Note


This is indeterministic because it depends on data partitioning and task scheduling.

>>> df.repartition(1).select(spark_partition_id().alias("pid")).collect()[Row(pid=0), Row(pid=0)]

New in version 1.6.

pyspark.sql.functions.split(strpattern)[source]

Splits str around pattern (pattern is a regular expression).

Note


pattern is a string represent the regular expression.

>>> df=spark.createDataFrame([('ab12cd',)],['s',])>>> df.select(split(df.s,'[0-9]+').alias('s')).collect()[Row(s=[u'ab', u'cd'])]

New in version 1.5.

pyspark.sql.functions.sqrt(col)

Computes the square root of the specified float value.

New in version 1.3.

pyspark.sql.functions.stddev(col)

Aggregate function: returns the unbiased sample standard deviation of the expression in a group.

New in version 1.6.

pyspark.sql.functions.stddev_pop(col)

Aggregate function: returns population standard deviation of the expression in a group.

New in version 1.6.

pyspark.sql.functions.stddev_samp(col)

Aggregate function: returns the unbiased sample standard deviation of the expression in a group.

New in version 1.6.

pyspark.sql.functions.struct(*cols)[source]

Creates a new struct column.

Parameters:cols – list of column names (string) or list of Column expressions

>>> df.select(struct('age','name').alias("struct")).collect()[Row(struct=Row(age=2, name=u'Alice')), Row(struct=Row(age=5, name=u'Bob'))]>>> df.select(struct([df.age,df.name]).alias("struct")).collect()[Row(struct=Row(age=2, name=u'Alice')), Row(struct=Row(age=5, name=u'Bob'))]

New in version 1.4.

pyspark.sql.functions.substring(strposlen)[source]

Substring starts at pos and is of length len when str is String type or returns the slice of byte array that starts at pos in byte and is of length len when str is Binary type.

Note


The position is not zero based, but 1 based index.

>>> df=spark.createDataFrame([('abcd',)],['s',])>>> df.select(substring(df.s,1,2).alias('s')).collect()[Row(s=u'ab')]

New in version 1.5.

pyspark.sql.functions.substring_index(strdelimcount)[source]

Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything the left of the final delimiter (counting from left) is returned. If count is negative, every to the right of the final delimiter (counting from the right) is returned. substring_index performs a case-sensitive match when searching for delim.

>>> df=spark.createDataFrame([('a.b.c.d',)],['s'])>>> df.select(substring_index(df.s,'.',2).alias('s')).collect()[Row(s=u'a.b')]>>> df.select(substring_index(df.s,'.',-3).alias('s')).collect()[Row(s=u'b.c.d')]

New in version 1.5.

pyspark.sql.functions.sum(col)

Aggregate function: returns the sum of all values in the expression.

New in version 1.3.

pyspark.sql.functions.sumDistinct(col)

Aggregate function: returns the sum of distinct values in the expression.

New in version 1.3.

pyspark.sql.functions.tan(col)

Parameters:col – angle in radians

Returns:tangent of the given value, as if computed by java.lang.Math.tan()

New in version 1.4.

pyspark.sql.functions.tanh(col)

Parameters:col – hyperbolic angle

Returns:hyperbolic tangent of the given value, as if computed by java.lang.Math.tanh()

New in version 1.4.

pyspark.sql.functions.toDegrees(col)

Note


Deprecated in 2.1, use degrees() instead.

New in version 1.4.

pyspark.sql.functions.toRadians(col)

Note


Deprecated in 2.1, use radians() instead.

New in version 1.4.

pyspark.sql.functions.to_date(colformat=None)[source]

Converts a Column of pyspark.sql.types.StringType or pyspark.sql.types.TimestampType into pyspark.sql.types.DateType using the optionally specified format. Specify formats according to SimpleDateFormats. By default, it follows casting rules to pyspark.sql.types.DateType if the format is omitted (equivalent to col.cast("date")).

>>> df=spark.createDataFrame([('1997-02-28 10:30:00',)],['t'])>>> df.select(to_date(df.t).alias('date')).collect()[Row(date=datetime.date(1997, 2, 28))]

>>> df=spark.createDataFrame([('1997-02-28 10:30:00',)],['t'])>>> df.select(to_date(df.t,'yyyy-MM-dd HH:mm:ss').alias('date')).collect()[Row(date=datetime.date(1997, 2, 28))]

New in version 2.2.

pyspark.sql.functions.to_json(coloptions={})[source]

Converts a column containing a StructType, ArrayType of StructTypes, a MapType or ArrayType of MapTypes into a JSON string. Throws an exception, in the case of an unsupported type.

Parameters:col – name of column containing the struct, array of the structs, the map or array of the maps.

options – options to control converting. accepts the same options as the json datasource

>>> frompyspark.sqlimportRow>>> frompyspark.sql.typesimport*>>> data=[(1,Row(name='Alice',age=2))]>>> df=spark.createDataFrame(data,("key","value"))>>> df.select(to_json(df.value).alias("json")).collect()[Row(json=u'{"age":2,"name":"Alice"}')]>>> data=[(1,[Row(name='Alice',age=2),Row(name='Bob',age=3)])]>>> df=spark.createDataFrame(data,("key","value"))>>> df.select(to_json(df.value).alias("json")).collect()[Row(json=u'[{"age":2,"name":"Alice"},{"age":3,"name":"Bob"}]')]>>> data=[(1,{"name":"Alice"})]>>> df=spark.createDataFrame(data,("key","value"))>>> df.select(to_json(df.value).alias("json")).collect()[Row(json=u'{"name":"Alice"}')]>>> data=[(1,[{"name":"Alice"},{"name":"Bob"}])]>>> df=spark.createDataFrame(data,("key","value"))>>> df.select(to_json(df.value).alias("json")).collect()[Row(json=u'[{"name":"Alice"},{"name":"Bob"}]')]

New in version 2.1.

pyspark.sql.functions.to_timestamp(colformat=None)[source]

Converts a Column of pyspark.sql.types.StringType or pyspark.sql.types.TimestampType into pyspark.sql.types.DateType using the optionally specified format. Specify formats according to SimpleDateFormats. By default, it follows casting rules to pyspark.sql.types.TimestampType if the format is omitted (equivalent to col.cast("timestamp")).

>>> df=spark.createDataFrame([('1997-02-28 10:30:00',)],['t'])>>> df.select(to_timestamp(df.t).alias('dt')).collect()[Row(dt=datetime.datetime(1997, 2, 28, 10, 30))]

>>> df=spark.createDataFrame([('1997-02-28 10:30:00',)],['t'])>>> df.select(to_timestamp(df.t,'yyyy-MM-dd HH:mm:ss').alias('dt')).collect()[Row(dt=datetime.datetime(1997, 2, 28, 10, 30))]

New in version 2.2.

pyspark.sql.functions.to_utc_timestamp(timestamptz)[source]

Given a timestamp like ‘2017-07-14 02:40:00.0’, interprets it as a time in the given time zone, and renders that time as a timestamp in UTC. For example, ‘GMT+1’ would yield ‘2017-07-14 01:40:00.0’.

>>> df=spark.createDataFrame([('1997-02-28 10:30:00',)],['ts'])>>> df.select(to_utc_timestamp(df.ts,"PST").alias('utc_time')).collect()[Row(utc_time=datetime.datetime(1997, 2, 28, 18, 30))]

New in version 1.5.

pyspark.sql.functions.translate(srcColmatchingreplace)[source]

A function translate any character in the srcCol by a character in matching. The characters in replace is corresponding to the characters in matching. The translate will happen when any character in the string matching with the character in the matching.

>>> spark.createDataFrame([('translate',)],['a']).select(translate('a',"rnlt","123")\... .alias('r')).collect()[Row(r=u'1a2s3ae')]

New in version 1.5.

pyspark.sql.functions.trim(col)

Trim the spaces from both ends for the specified string column.

New in version 1.5.

pyspark.sql.functions.trunc(dateformat)[source]

Returns date truncated to the unit specified by the format.

Parameters:format – ‘year’, ‘yyyy’, ‘yy’ or ‘month’, ‘mon’, ‘mm’

>>> df=spark.createDataFrame([('1997-02-28',)],['d'])>>> df.select(trunc(df.d,'year').alias('year')).collect()[Row(year=datetime.date(1997, 1, 1))]>>> df.select(trunc(df.d,'mon').alias('month')).collect()[Row(month=datetime.date(1997, 2, 1))]

New in version 1.5.

pyspark.sql.functions.udf(f=NonereturnType=StringType)[source]

Creates a user defined function (UDF).

Note


The user-defined functions are considered deterministic by default. Due to optimization, duplicate invocations may be eliminated or the function may even be invoked more times than it is present in the query. If your function is not deterministic, call asNondeterministic on the user defined function. E.g.:

>>> frompyspark.sql.typesimportIntegerType>>> importrandom>>> random_udf=udf(lambda:int(random.random()*100),IntegerType()).asNondeterministic()

Note


The user-defined functions do not support conditional expressions or short circuiting in boolean expressions and it ends up with being executed all internally. If the functions can fail on special rows, the workaround is to incorporate the condition into the functions.

Note


The user-defined functions do not take keyword arguments on the calling side.

Parameters:f – python function if used as a standalone function

returnType – the return type of the user-defined function. The value can be either a pyspark.sql.types.DataType object or a DDL-formatted type string.

>>> frompyspark.sql.typesimportIntegerType>>> slen=udf(lambdas:len(s),IntegerType())>>> :udf... defto_upper(s):... ifsisnotNone:... returns.upper()...>>> :udf(returnType=IntegerType())... defadd_one(x):... ifxisnotNone:... returnx+1...>>> df=spark.createDataFrame([(1,"John Doe",21)],("id","name","age"))>>> df.select(slen("name").alias("slen(name)"),to_upper("name"),add_one("age")).show()+----------+--------------+------------+|slen(name)|to_upper(name)|add_one(age)|+----------+--------------+------------+|        8|      JOHN DOE|          22|+----------+--------------+------------+

New in version 1.3.

pyspark.sql.functions.unbase64(col)

Decodes a BASE64 encoded string column and returns it as a binary column.

New in version 1.5.

pyspark.sql.functions.unhex(col)[source]

Inverse of hex. Interprets each pair of characters as a hexadecimal number and converts to the byte representation of number.

>>> spark.createDataFrame([('414243',)],['a']).select(unhex('a')).collect()[Row(unhex(a)=bytearray(b'ABC'))]

New in version 1.5.

pyspark.sql.functions.unix_timestamp(timestamp=Noneformat='yyyy-MM-dd HH:mm:ss')[source]

Convert time string with given pattern (‘yyyy-MM-dd HH:mm:ss’, by default) to Unix time stamp (in seconds), using the default timezone and the default locale, return null if fail.

if timestamp is None, then it returns current timestamp.

>>> spark.conf.set("spark.sql.session.timeZone","America/Los_Angeles")>>> time_df=spark.createDataFrame([('2015-04-08',)],['dt'])>>> time_df.select(unix_timestamp('dt','yyyy-MM-dd').alias('unix_time')).collect()[Row(unix_time=1428476400)]>>> spark.conf.unset("spark.sql.session.timeZone")

New in version 1.5.

pyspark.sql.functions.upper(col)

Converts a string column to upper case.

New in version 1.5.

pyspark.sql.functions.var_pop(col)

Aggregate function: returns the population variance of the values in a group.

New in version 1.6.

pyspark.sql.functions.var_samp(col)

Aggregate function: returns the unbiased variance of the values in a group.

New in version 1.6.

pyspark.sql.functions.variance(col)

Aggregate function: returns the population variance of the values in a group.

New in version 1.6.

pyspark.sql.functions.weekofyear(col)[source]

Extract the week number of a given date as integer.

>>> df=spark.createDataFrame([('2015-04-08',)],['dt'])>>> df.select(weekofyear(df.dt).alias('week')).collect()[Row(week=15)]

New in version 1.5.

pyspark.sql.functions.when(conditionvalue)[source]

Evaluates a list of conditions and returns one of multiple possible result expressions. If Column.otherwise() is not invoked, None is returned for unmatched conditions.

Parameters:condition – a boolean Column expression.

value – a literal value, or a Column expression.

>>> df.select(when(df['age']==2,3).otherwise(4).alias("age")).collect()[Row(age=3), Row(age=4)]

>>> df.select(when(df.age==2,df.age+1).alias("age")).collect()[Row(age=3), Row(age=None)]

New in version 1.4.

pyspark.sql.functions.window(timeColumnwindowDurationslideDuration=NonestartTime=None)[source]

Bucketize rows into one or more time windows given a timestamp specifying column. Window starts are inclusive but the window ends are exclusive, e.g. 12:05 will be in the window [12:05,12:10) but not in [12:00,12:05). Windows can support microsecond precision. Windows in the order of months are not supported.

The time column must be of pyspark.sql.types.TimestampType.

Durations are provided as strings, e.g. ‘1 second’, ‘1 day 12 hours’, ‘2 minutes’. Valid interval strings are ‘week’, ‘day’, ‘hour’, ‘minute’, ‘second’, ‘millisecond’, ‘microsecond’. If the slideDuration is not provided, the windows will be tumbling windows.

The startTime is the offset with respect to 1970-01-01 00:00:00 UTC with which to start window intervals. For example, in order to have hourly tumbling windows that start 15 minutes past the hour, e.g. 12:15-13:15, 13:15-14:15… provide startTime as 15 minutes.

The output column will be a struct called ‘window’ by default with the nested columns ‘start’ and ‘end’, where ‘start’ and ‘end’ will be of pyspark.sql.types.TimestampType.

>>> df=spark.createDataFrame([("2016-03-11 09:00:07",1)]).toDF("date","val")>>> w=df.groupBy(window("date","5 seconds")).agg(sum("val").alias("sum"))>>> w.select(w.window.start.cast("string").alias("start"),... w.window.end.cast("string").alias("end"),"sum").collect()[Row(start=u'2016-03-11 09:00:05', end=u'2016-03-11 09:00:10', sum=1)]

New in version 2.0.

pyspark.sql.functions.year(col)[source]

Extract the year of a given date as integer.

>>> df=spark.createDataFrame([('2015-04-08',)],['dt'])>>> df.select(year('dt').alias('year')).collect()[Row(year=2015)]

New in version 1.5.

pyspark.sql.streaming module

class pyspark.sql.streaming.StreamingQuery(jsq)[source]

A handle to a query that is executing continuously in the background as new data arrives. All these methods are thread-safe.

Note


Evolving

New in version 2.0.

awaitTermination(timeout=None)[source]

Waits for the termination of this query, either by query.stop() or by an exception. If the query has terminated with an exception, then the exception will be thrown. If timeout is set, it returns whether the query has terminated or not within the timeout seconds.

If the query has terminated, then all subsequent calls to this method will either return immediately (if the query was terminated by stop()), or throw the exception immediately (if the query has terminated with exception).

throws StreamingQueryException, if this query has terminated with an exception

New in version 2.0.

exception()[source]

Returns:the StreamingQueryException if the query was terminated by an exception, or None.

New in version 2.1.

explain(extended=False)[source]

Prints the (logical and physical) plans to the console for debugging purpose.

Parameters:extended – boolean, default False. If False, prints only the physical plan.

>>> sq=sdf.writeStream.format('memory').queryName('query_explain').start()>>> sq.processAllAvailable()# Wait a bit to generate the runtime plans.>>> sq.explain()== Physical Plan ==...>>> sq.explain(True)== Parsed Logical Plan ==...== Analyzed Logical Plan ==...== Optimized Logical Plan ==...== Physical Plan ==...>>> sq.stop()

New in version 2.1.

id

Returns the unique id of this query that persists across restarts from checkpoint data. That is, this id is generated when a query is started for the first time, and will be the same every time it is restarted from checkpoint data. There can only be one query with the same id active in a Spark cluster. Also see, runId.

New in version 2.0.

isActive

Whether this streaming query is currently active or not.

New in version 2.0.

lastProgress

Returns the most recent StreamingQueryProgress update of this streaming query or None if there were no progress updates :return: a map

New in version 2.1.

name

Returns the user-specified name of the query, or null if not specified. This name can be specified in the org.apache.spark.sql.streaming.DataStreamWriter as dataframe.writeStream.queryName(“query”).start(). This name, if set, must be unique across all active queries.

New in version 2.0.

processAllAvailable()[source]

Blocks until all available data in the source has been processed and committed to the sink. This method is intended for testing.

Note


In the case of continually arriving data, this method may block forever. Additionally, this method is only guaranteed to block until data that has been synchronously appended data to a stream source prior to invocation. (i.e. getOffset must immediately reflect the addition).

New in version 2.0.

recentProgress

Returns an array of the most recent [[StreamingQueryProgress]] updates for this query. The number of progress updates retained for each stream is configured by Spark session configuration spark.sql.streaming.numRecentProgressUpdates.

New in version 2.1.

runId

Returns the unique id of this query that does not persist across restarts. That is, every query that is started (or restarted from checkpoint) will have a different runId.

New in version 2.1.

status

Returns the current status of the query.

New in version 2.1.

stop()[source]

Stop this streaming query.

New in version 2.0.

class pyspark.sql.streaming.StreamingQueryManager(jsqm)[source]

A class to manage all the StreamingQuery StreamingQueries active.

Note


Evolving

New in version 2.0.

active

Returns a list of active queries associated with this SQLContext

>>> sq=sdf.writeStream.format('memory').queryName('this_query').start()>>> sqm=spark.streams>>> # get the list of active streaming queries>>> [q.nameforqinsqm.active][u'this_query']>>> sq.stop()

New in version 2.0.

awaitAnyTermination(timeout=None)[source]

Wait until any of the queries on the associated SQLContext has terminated since the creation of the context, or since resetTerminated() was called. If any query was terminated with an exception, then the exception will be thrown. If timeout is set, it returns whether the query has terminated or not within the timeout seconds.

If a query has terminated, then subsequent calls to awaitAnyTermination() will either return immediately (if the query was terminated by query.stop()), or throw the exception immediately (if the query was terminated with exception). Use resetTerminated() to clear past terminations and wait for new terminations.

In the case where multiple queries have terminated since resetTermination() was called, if any query has terminated with exception, then awaitAnyTermination() will throw any of the exception. For correctly documenting exceptions across multiple queries, users need to stop all of them after any of them terminates with exception, and then check the query.exception() for each query.

throws StreamingQueryException, if this query has terminated with an exception

New in version 2.0.

get(id)[source]

Returns an active query from this SQLContext or throws exception if an active query with this name doesn’t exist.

>>> sq=sdf.writeStream.format('memory').queryName('this_query').start()>>> sq.nameu'this_query'>>> sq=spark.streams.get(sq.id)>>> sq.isActiveTrue>>> sq=sqlContext.streams.get(sq.id)>>> sq.isActiveTrue>>> sq.stop()

New in version 2.0.

resetTerminated()[source]

Forget about past terminated queries so that awaitAnyTermination() can be used again to wait for new terminations.

>>> spark.streams.resetTerminated()

New in version 2.0.

class pyspark.sql.streaming.DataStreamReader(spark)[source]

Interface used to load a streaming DataFrame from external storage systems (e.g. file systems, key-value stores, etc). Use spark.readStream() to access this.

Note


Evolving.

New in version 2.0.

csv(pathschema=Nonesep=Noneencoding=Nonequote=Noneescape=Nonecomment=Noneheader=NoneinferSchema=NoneignoreLeadingWhiteSpace=NoneignoreTrailingWhiteSpace=NonenullValue=NonenanValue=NonepositiveInf=NonenegativeInf=NonedateFormat=NonetimestampFormat=NonemaxColumns=NonemaxCharsPerColumn=NonemaxMalformedLogPerPartition = Nonemode = NonecolumnNameOfCorruptRecord = NonemultiLine = NonecharToEscapeQuoteEscaping = None [source]

加载CSV文件流并将结果作为一个返回 DataFrame。

如果inferSchema启用,此函数将通过一次输入来确定输入模式 。为了避免一次查看整个数据,请禁用 inferSchema选项或明确指定模式schema。

注意


进化。

参数:路径 - 字符串或字符串列表,用于输入路径。

模式 - pyspark.sql.types.StructType输入模式或DDL格式字符串的可选项(例如)。col0 INT, col1 DOUBLE

sep – sets a single character as a separator for each field and value. If None is set, it uses the default value, ,.

encoding – decodes the CSV files by the given encoding type. If None is set, it uses the default value, UTF-8.

quote – sets a single character used for escaping quoted values where the separator can be part of the value. If None is set, it uses the default value, ". If you would like to turn off quotations, you need to set an empty string.

escape – sets a single character used for escaping quotes inside an already quoted value. If None is set, it uses the default value, \.

comment – sets a single character used for skipping lines beginning with this character. By default (None), it is disabled.

标题 - 使用第一行作为列的名称。如果设置无,则使用默认值,false。

inferSchema - 从数据中自动推断输入模式。它需要额外的数据传递。如果设置无,则使用默认值,false。

ignoreLeadingWhiteSpace - 一个标志,指示是否应该跳过正在读取值的前导空白。如果设置无,则使用默认值,false。

ignoreTrailingWhiteSpace - 一个标志,指示是否应该跳过正在读取的值的尾部空白。如果设置无,则使用默认值,false。

nullValue - 设置空值的字符串表示形式。如果设置无,则使用默认值,即空字符串。从2.0.1开始,此nullValue参数适用于所有支持的类型,包括字符串类型。

nanValue - 设置非数字值的字符串表示形式。如果设置无,则使用默认值,NaN。

positiveInf - 设置正无穷大值的字符串表示形式。如果设置无,则使用默认值,Inf。

negativeInf - 设置负无穷大值的字符串表示形式。如果设置无,则使用默认值,Inf。

dateFormat - 设置表示日期格式的字符串。自定义日期格式遵循格式java.text.SimpleDateFormat。这适用于日期类型。如果设置无,则使用默认值,yyyy-MM-dd。

timestampFormat - 设置指示时间戳格式的字符串。自定义日期格式遵循格式java.text.SimpleDateFormat。这适用于时间戳类型。如果设置无,则使用默认值,yyyy-MM-dd'T'HH:mm:ss.SSSXXX。

maxColumns - 定义记录可以有多少列的硬限制。如果设置无,则使用默认值,20480。

maxCharsPerColumn - 定义读取任何给定值所允许的最大字符数。如果设置为None,则使用默认值, -1即无限长度。

maxMalformedLogPerPartition - 自Spark 2.2.0以来不再使用此参数。如果指定,它将被忽略。

模式 -

允许在解析期间处理损坏记录的模式。如果没有

设置,它使用默认值,PERMISSIVE。

PERMISSIVE:遇到损坏的记录时,将格式错误的字符串放入配置的字段中columnNameOfCorruptRecord,并将其他字段设置为null。为了保持损坏的记录,用户可以设置columnNameOfCorruptRecord用户定义架构中命名的字符串类型字段。如果模式不具有该字段,则会在分析过程中删除损坏的记录。具有比模式更少/更多令牌的记录不是对CSV的损坏记录。当它满足记录少于模式长度的记号时,设置null为额外字段。当记录有更多的令牌比模式的长度时,它会丢弃额外的令牌。

DROPMALFORMED :忽略整个损坏的记录。

FAILFAST :遇到损坏的记录时抛出异常。

columnNameOfCorruptRecord - 允许重命名由PERMISSIVE模式创建格式不正确的新字段。这覆盖 spark.sql.columnNameOfCorruptRecord。如果设置无,则使用在中指定的值 spark.sql.columnNameOfCorruptRecord。

multiLine - 解析一条记录,该记录可能跨越多行。如果设置无,则使用默认值,false。

charToEscapeQuoteEscaping - 设置用于转义引号字符的单个字符。如果设置无,则当转义和引号字符不同时,默认值为转义字符,\否则。

>>> csv_sdf = 火花。readStream 。CSV (临时文件。mkdtemp (),模式= sdf_schema )>>> csv_sdf 。isStreaming True >>> csv_sdf 。模式== sdf_schema 真

2.0版本中的新功能。

format(来源[来源]

指定输入数据源格式。

注意


进化。

参数: - 字符串,数据源的名称,例如'json','parquet'。

>>> s = 火花。readStream 。格式(“文本” )

2.0版本中的新功能。

json(pathschema=NoneprimitivesAsString=NoneprefersDecimal=NoneallowComments=NoneallowUnquotedFieldNames=NoneallowSingleQuotes=NoneallowNumericLeadingZero=NoneallowBackslashEscapingAnyCharacter=Nonemode=NonecolumnNameOfCorruptRecord=NonedateFormat=NonetimestampFormat=NonemultiLine=NoneallowUnquotedControlChars=None)[source]

Loads a JSON file stream and returns the results as a DataFrame.

JSON Lines (newline-delimited JSON) is supported by default. For JSON (one record per file), set the multiLine parameter to true.

If the schema parameter is not specified, this function goes through the input once to determine the input schema.

Note


Evolving.

Parameters:path – string represents path to the JSON dataset, or RDD of Strings storing JSON objects.

schema – an optional pyspark.sql.types.StructType for the input schema or a DDL-formatted string (For example col0 INT, col1DOUBLE).

primitivesAsString – infers all primitive values as a string type. If None is set, it uses the default value, false.

prefersDecimal – infers all floating-point values as a decimal type. If the values do not fit in decimal, then it infers them as doubles. If None is set, it uses the default value, false.

allowComments – ignores Java/C++ style comment in JSON records. If None is set, it uses the default value, false.

allowUnquotedFieldNames – allows unquoted JSON field names. If None is set, it uses the default value, false.

allowSingleQuotes – allows single quotes in addition to double quotes. If None is set, it uses the default value, true.

allowNumericLeadingZero – allows leading zeros in numbers (e.g. 00012). If None is set, it uses the default value, false.

allowBackslashEscapingAnyCharacter – allows accepting quoting of all character using backslash quoting mechanism. If None is set, it uses the default value, false.

mode –

allows a mode for dealing with corrupt records during parsing. If None is

set, it uses the default value, PERMISSIVE.

PERMISSIVE : when it meets a corrupted record, puts the malformed string into a field configured by columnNameOfCorruptRecord, and sets other fields to null. To keep corrupt records, an user can set a string type field named columnNameOfCorruptRecord in an user-defined schema. If a schema does not have the field, it drops corrupt records during parsing. When inferring a schema, it implicitly adds a columnNameOfCorruptRecord field in an output schema.

DROPMALFORMED : ignores the whole corrupted records.

FAILFAST : throws an exception when it meets corrupted records.

columnNameOfCorruptRecord – allows renaming the new field having malformed string created by PERMISSIVE mode. This overrides spark.sql.columnNameOfCorruptRecord. If None is set, it uses the value specified in spark.sql.columnNameOfCorruptRecord.

dateFormat – sets the string that indicates a date format. Custom date formats follow the formats at java.text.SimpleDateFormat. This applies to date type. If None is set, it uses the default value, yyyy-MM-dd.

timestampFormat – sets the string that indicates a timestamp format. Custom date formats follow the formats at java.text.SimpleDateFormat. This applies to timestamp type. If None is set, it uses the default value, yyyy-MM-dd'T'HH:mm:ss.SSSXXX.

multiLine – parse one record, which may span multiple lines, per file. If None is set, it uses the default value, false.

allowUnquotedControlChars – allows JSON Strings to contain unquoted control characters (ASCII characters with value less than 32, including tab and line feed characters) or not.

>>> json_sdf=spark.readStream.json(tempfile.mkdtemp(),schema=sdf_schema)>>> json_sdf.isStreamingTrue>>> json_sdf.schema==sdf_schemaTrue

New in version 2.0.

load(path=Noneformat=Noneschema=None**options)[source]

Loads a data stream from a data source and returns it as a :class`DataFrame`.

Note


Evolving.

Parameters:path – optional string for file-system backed data sources.

format – optional string for format of the data source. Default to ‘parquet’.

schema – optional pyspark.sql.types.StructType for the input schema or a DDL-formatted string (For example col0 INT, col1DOUBLE).

options – all other string options

>>> json_sdf=spark.readStream.format("json")\... .schema(sdf_schema)\... .load(tempfile.mkdtemp())>>> json_sdf.isStreamingTrue>>> json_sdf.schema==sdf_schemaTrue

New in version 2.0.

option(keyvalue)[source]

Adds an input option for the underlying data source.

You can set the following option(s) for reading files:

timeZone: sets the string that indicates a timezone to be used to parse timestamps

in the JSON/CSV datasources or partition values. If it isn’t set, it uses the default value, session local timezone.

Note


Evolving.

>>> s=spark.readStream.option("x",1)

New in version 2.0.

options(**options)[source]

Adds input options for the underlying data source.

You can set the following option(s) for reading files:

timeZone: sets the string that indicates a timezone to be used to parse timestamps

in the JSON/CSV datasources or partition values. If it isn’t set, it uses the default value, session local timezone.

Note


Evolving.

>>> s=spark.readStream.options(x="1",y=2)

New in version 2.0.

orc(path)[source]

Loads a ORC file stream, returning the result as a DataFrame.

Note


Evolving.

>>> orc_sdf=spark.readStream.schema(sdf_schema).orc(tempfile.mkdtemp())>>> orc_sdf.isStreamingTrue>>> orc_sdf.schema==sdf_schemaTrue

New in version 2.3.

parquet(path)[source]

Loads a Parquet file stream, returning the result as a DataFrame.

You can set the following Parquet-specific option(s) for reading Parquet files:

mergeSchema: sets whether we should merge schemas collected from all Parquet part-files. This will override spark.sql.parquet.mergeSchema. The default value is specified in spark.sql.parquet.mergeSchema.

Note


Evolving.

>>> parquet_sdf=spark.readStream.schema(sdf_schema).parquet(tempfile.mkdtemp())>>> parquet_sdf.isStreamingTrue>>> parquet_sdf.schema==sdf_schemaTrue

New in version 2.0.

schema(schema)[source]

Specifies the input schema.

Some data sources (e.g. JSON) can infer the input schema automatically from data. By specifying the schema here, the underlying data source can skip the schema inference step, and thus speed up data loading.

Note


Evolving.

Parameters:schema – a pyspark.sql.types.StructType object or a DDL-formatted string (For example col0 INT, col1 DOUBLE).

>>> s=spark.readStream.schema(sdf_schema)>>> s=spark.readStream.schema("col0 INT, col1 DOUBLE")

New in version 2.0.

text(path)[source]

Loads a text file stream and returns a DataFrame whose schema starts with a string column named “value”, and followed by partitioned columns if there are any.

Each line in the text file is a new row in the resulting DataFrame.

Note


Evolving.

Parameters:paths – string, or list of strings, for input path(s).

>>> text_sdf=spark.readStream.text(tempfile.mkdtemp())>>> text_sdf.isStreamingTrue>>> "value"instr(text_sdf.schema)True

New in version 2.0.

class pyspark.sql.streaming.DataStreamWriter(df)[source]

Interface used to write a streaming DataFrame to external storage systems (e.g. file systems, key-value stores, etc). Use DataFrame.writeStream() to access this.

Note


Evolving.

New in version 2.0.

format(source)[source]

Specifies the underlying output data source.

Note


Evolving.

Parameters:source – string, name of the data source, which for now can be ‘parquet’.

>>> writer=sdf.writeStream.format('json')

New in version 2.0.

option(keyvalue)[source]

Adds an output option for the underlying data source.

You can set the following option(s) for writing files:

timeZone: sets the string that indicates a timezone to be used to format

timestamps in the JSON/CSV datasources or partition values. If it isn’t set, it uses the default value, session local timezone.

Note


Evolving.

New in version 2.0.

options(**options)[source]

Adds output options for the underlying data source.

You can set the following option(s) for writing files:

timeZone: sets the string that indicates a timezone to be used to format

timestamps in the JSON/CSV datasources or partition values. If it isn’t set, it uses the default value, session local timezone.

Note


Evolving.

New in version 2.0.

outputMode(outputMode)[source]

Specifies how data of a streaming DataFrame/Dataset is written to a streaming sink.

Options include:

append:Only the new rows in the streaming DataFrame/Dataset will be written to

the sink

complete:All the rows in the streaming DataFrame/Dataset will be written to the sink

every time these is some updates

更新:只有在数据流DataFrame / Dataset中更新的行才会被更新

每次有一些更新时写入接收器。如果查询不包含聚合,则它将等同于追加模式。

注意


进化。

>>> writer = sdf 。writeStream 。outputMode ('append' )

2.0版本中的新功能。

partitionBy(* cols [source]

按文件系统上的给定列对输出进行分区。

如果指定,则输出将在文件系统上进行布局,类似于Hive的分区方案。

注意


进化。

参数:cols - 列的名称

2.0版本中的新功能。

queryName(queryName [source]

指定StreamingQuery可以以其开始 的名称start()。该名称在关联的SparkSession中的所有当前活动查询中必须是唯一的。

注意


进化。

参数:queryName - 查询的唯一名称

>>> writer = sdf 。writeStream 。queryName ('streaming_query' )

2.0版本中的新功能。

start(path = Noneformat = NoneoutputMode = NonepartitionBy = NonequeryName = None** options [source]

DataFrame将数据流的内容流式传输到数据源。

数据源由format和一组指定options。如果format未指定,spark.sql.sources.default则将使用由其配置的默认数据源 。

注意


进化。

参数:路径 - Hadoop支持的文件系统中的路径

格式 - 用于保存的格式

outputMode -

指定如何将流式DataFrame / Dataset的数据写入a

流水槽。

追加:只有流式DataFrame / Dataset中的新行才会写入接收器

完成:流式DataFrame / Dataset中的所有行都将写入接收器

每次这些都是一些更新

更新:每次有更新时,只有流式DataFrame / Dataset中更新的行才写入接收器。如果查询不包含聚合,则它将等同于追加模式。

partitionBy - 分区列的名称

queryName - 查询的唯一名称

选项 - 所有其他字符串选项。您可能需要 为大多数流提供检查点位置,但对于内存流不是必需的。

>>> sq = sdf 。writeStream 。格式('记忆' )。queryName ('this_query' )。start ()>>> sq 。isActive True >>> sq 。名字u'this_query' >>> sq 。stop ()>>> sq 。isActive False >>> sq = sdf 。writeStream 。'5秒' )。start (... queryName = 'that_query' ,outputMode = “append” ,format = 'memory' )>>> sq 。名字u'that_query' >>> sq 。isActive True >>> sq 。停止()

2.0版本中的新功能。

trigger(* args** kwargs [源代码]

设置流查询的触发器。如果未设置,它将尽可能快地运行查询,这相当于将触发设置为。processingTime='0 seconds'

注意


进化。

参数:processingTime - 作为一个字符串的处理时间间隔,例如'5秒','1分钟'。根据处理时间定期设置运行查询的触发器。只能设置一个触发器。

一次 - 如果设置为True,则设置一个只处理流式查询中的一批数据的触发器,然后终止查询。只能设置一个触发器。

>>> #每5秒触发查询执行>>> writer = sdf 。writeStream 。触发器(processingTime = '5秒' )>>> #只触发一次批处理数据的查询>>> writer = sdf 。writeStream 。trigger (once = True )>>> #每5秒触发一次执行查询>>> writer = sdf 。writeStream 。= '5秒' )

2.0版本中的新功能。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,290评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,399评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,021评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,034评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,412评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,651评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,902评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,605评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,339评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,586评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,076评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,400评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,060评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,851评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,685评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,595评论 2 270

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,106评论 0 10
  • Spark SQL, DataFrames and Datasets Guide Overview SQL Dat...
    草里有只羊阅读 18,230评论 0 85
  • 一、补习学校 涉水河畔 补习学校高三四班 铁打的营房流水的兵 这里注定是一个不一样的地方,补习过的人说补习学校是“...
    鸿蒙岁月阅读 683评论 0 2
  • 0、简单的说一句 autorelease 已经在 iOS 界叱咤风云这么多年,现在网上也有很多类似的文章,今天也来...
    CoderHG阅读 975评论 1 7
  • 绑架 一 他浑身发麻,脑子里一片空白,左右两边各被一人钳住手臂,动弹不得。 “找找他手机里,有没有号码!”前头副驾...
    xhy0606阅读 320评论 0 6