Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import and Export Schema Objects to JSON #17875

Open
jrasband-dev opened this issue Jul 25, 2024 · 0 comments
Open

Import and Export Schema Objects to JSON #17875

jrasband-dev opened this issue Jul 25, 2024 · 0 comments
Labels
enhancement New feature or an improvement of an existing feature

Comments

@jrasband-dev
Copy link

jrasband-dev commented Jul 25, 2024

Description

From an operational perspective it would be nice to read and write schemas.

Use Cases:

  • Documentation and Versioning
  • Reuse in other scripts
  • Integration with other tools

Example solution for exporting schema

import json 
import polars as pl


custom_schema = pl.Schema({'Employee ID':pl.Int64
           ,'First Name':pl.String
           ,'Last Name':pl.String
           ,'Gender':pl.String
           ,'Date of Birth':pl.Date
           ,'Department':pl.String
           ,'Position':pl.String
           ,'Salary ($)':pl.Float64
           ,'Email':pl.String
           ,'Phone':pl.String})

def write_schema(fp):
    string_values = [str(value) for value in custom_schema.dtypes()]
    schema_dict = dict(zip(custom_schema.names(),string_values))
    f = open(fp,'w')
    f.write(json.dumps(schema_dict))
    f.close()

fp = 'employees_schema.json'
write_schema(fp)

Example solution for importing schemas

import json 

fp = 'employees_schema.json'

def read_schema(fp):
    f = open(fp,'r')
    schema = json.load(f)
    f.close()
    names = list(schema.keys())
    data_types = []
    for key, value in schema.items():
        if value == 'Int64':
            value = pl.Int64
            data_types.append(value)
        if value == 'String':
            value = pl.String
            data_types.append(value)
        if value == 'Date':
            value = pl.Date
            data_types.append(value)
        else:
            pass

    schema_dict = dict(zip(names,data_types))
    schema_object = pl.Schema(schema_dict)
    return schema_object

schema = read_schema(fp)
schema

@ritchie46

@jrasband-dev jrasband-dev added the enhancement New feature or an improvement of an existing feature label Jul 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature
Projects
None yet
Development

No branches or pull requests

1 participant