Skip to main content

Enhanced Python Dataframes for Spark/PySpark

Project description

Enhanced Python Dataframes for Spark/PySpark

Motivation

Spark DataFrames provide a nice interface to datasets that have a schema. Getting data from your code into a DataFrame in Python means creating a Row() object with field names and respective values. Given that you already have a schema with data types per field, it would be nice to easily take an object that represents the row and create the Row() object automatically.

This was written when creating Row()’s with Long datatypes and finding that Spark did not handle converting integers as longs when passing values to the JVM. I needed a consistent manner to create Row() for all of my DataFrames.

The Row() object also allows you to reference the fields by name (e.g. myRow.firstName), this allows you to create an object that can now be transformed into a Row() object.

Example

Simply create a class that extends from SmartFrame and define the schema as a sorted list of StructFields. It’s important that the schema is sorted as Spark gets upset if the Row() object is created with fields that are in a different order. Strange, but true.

The skipSelectedFields is a list of field names that you normally would not select when creating a select() statement.

class SimpleTable(SmartFrames):
    schema = StructType( sorted(
        [
        StructField("pk_id", IntegerType()),
        StructField("first_name", StringType()),
        ],
        key = lambda x: x.name))
    skipSelectedFields = []

...

        s1 = SimpleTable()
        s1.pk_id = 1
        s1.first_name = 'Don'

        s2 = SimpleTable()
        s2.pk_id = 2
        s2.first_name = 'Dan'
        df = self.sqlCtx.createDataFrame(self.sc.parallelize([s1.createRow(), s2.createRow()]), s1.schema)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smartframes-1.0.1.tar.gz (7.8 kB view details)

Uploaded Source

File details

Details for the file smartframes-1.0.1.tar.gz.

File metadata

  • Download URL: smartframes-1.0.1.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for smartframes-1.0.1.tar.gz
Algorithm Hash digest
SHA256 e763308f6eed6cd4651df932f3da62b71f4bf0ea8db4b6bf24b29e333a9b1910
MD5 aad7e7307ec70d550fa81d9d5c9546b8
BLAKE2b-256 df401bd0196bb87c5c793d48c2c4b1a4964f95ccf40ac70d68796d8272ba0ac8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page