The documentation for Pyspark shows DataFrames being constructed from sqlContext
, sqlContext.read()
, and a variety of other methods.
(See https://spark.apache.org/docs/1.6.2/api/python/pyspark.sql.html)
Is it possible to subclass Dataframe and instantiate it independently? I would like to add methods and functionality to the base DataFrame class.
In PySpark, you can cast or change the DataFrame column data type using cast() function of Column class, in this article, I will be using withColumn(), selectExpr() , and SQL expression to cast the from String to Int (Integer Type), String to Boolean e.t.c using PySpark examples.
While Pyspark derives its basic data types from Python, its own data structures are limited to RDD, Dataframes, Graphframes. These data frames are immutable and offer reduced flexibility during row/column level handling, as compared to Python.
Due to parallel execution on all cores on multiple machines, PySpark runs operations faster than Pandas, hence we often required to covert Pandas DataFrame to PySpark (Spark with Python) for better performance. This is one of the major differences between Pandas vs PySpark DataFrame.
It really depends on your goals.
Technically speaking it is possible. pyspark.sql.DataFrame
is just a plain Python class. You can extend it or monkey-patch if you need.
from pyspark.sql import DataFrame
class DataFrameWithZipWithIndex(DataFrame):
def __init__(self, df):
super(self.__class__, self).__init__(df._jdf, df.sql_ctx)
def zipWithIndex(self):
return (self.rdd
.zipWithIndex()
.map(lambda row: (row[1], ) + row[0])
.toDF(["_idx"] + self.columns))
Example usage:
df = sc.parallelize([("a", 1)]).toDF(["foo", "bar"])
with_zipwithindex = DataFrameWithZipWithIndex(df)
isinstance(with_zipwithindex, DataFrame)
True
with_zipwithindex.zipWithIndex().show()
+----+---+---+
|_idx|foo|bar|
+----+---+---+
| 0| a| 1|
+----+---+---+
Practically speaking you won't be able to do much here. DataFrame
is an thin wrapper around JVM object and doesn't do much beyond providing docstrings, converting arguments to the form required natively, calling JVM methods, and wrapping the results using Python adapters if necessary.
With plain Python code you won't be able to even go near DataFrame
/ Dataset
internals or modify its core behavior. If you're looking for standalone, Python only Spark DataFrame
implementation it is not possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With