Skip to main content

Named Data Frames

Project description

namedframes

Basic type annotation support for Pandas and Spark data frames. The goal is to provide a convenient way to specify a name-to-type mapping. The assurance that the columns conform to the types is left to the user, i.e. this provides named data frames, not typed data frames.

Installation

pip install namedframes

Usage

Pandas

import pandas as pd
from namedframes import PandasNamedFrame

class InputDF(PandasNamedFrame):
    x: float

class OutputDF(InputDF):
    blah: bool


def transform(input_data: InputDF) -> OutputDF:
    return OutputDF(input_data.assign(blah = True))

input_df = InputDF(pd.DataFrame({"x": [1.1, 2.2]}))

output = transform(input_df)

isinstance(input_df, InputDF)
True

isinstance(output, OutputDF)
True

If a column is missing, a validation error occurs,

OutputDF(input_df)

ValueError: missing columns: [('blah', <class 'bool'>)]

Spark

namedframes includes an option for pyspark dataframes. Using it requires installation of pyspark. You can install this separately or with the [pyspark] flag to namedframes, i.e.,

pip install namedframes[pyspark]

Example usage:

import pandas as pd 
from pyspark.sql import SparkSession
from namedframes import SparkNamedFrame 

class InputDF(SparkNamedFrame): 
    x: float 

spark = SparkSession.builder.getOrCreate()
spark_df = spark.createDataFrame(pd.DataFrame({"x": [1.1, 2.2]}))                                                      
input_df = InputDF(spark_df)

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

namedframes-0.1.4.tar.gz (2.9 kB view hashes)

Uploaded Source

Built Distribution

namedframes-0.1.4-py2.py3-none-any.whl (4.3 kB view hashes)

Uploaded Python 2 Python 3

Supported by

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