Skip to main content

Pandas dataframes with object oriented programming style

Project description

image

Pandas-Oop

(Also known as Poop), is a package that uses Pandas dataframes with object oriented programming style

Installation:

  pip install pandas-oop

Some examples

from pandas_oop import models
from pandas_oop.fields import StringColumn, IntegerColumn, FloatColumn, DateColumn, BoolColumn
DB_CONNECTION = models.Connection('sqlite:///pandas_oop.db') # this is the same con_string for sqlalchemy engine
@models.sql(table='people', con=DB_CONNECTION) # Use this decorator if you want to connect your class to a database
@models.Data
class People(models.DataFrame):
    name = StringColumn(unique=True)
    age = IntegerColumn()
    money = FloatColumn(target_name="coins") # target_name if the name in the csv or table is coins and you want to have a different variable name
    insertion_date = DateColumn(format='%d-%m-%Y')
    is_staff = BoolColumn(true='yes', false='no')

Now when instantiating this class, it will return a custom dataframe with all the functionalities of a Pandas dataframe and some others

people = People()
"""-----------------------------------------------------------"""
people = People(from_csv=DATA_FILE, delimiter=";")
"""-----------------------------------------------------------"""
people = People(from_sql_query='select * from people')
"""-----------------------------------------------------------"""
people = People(from_df=some_dataframe)
"""-----------------------------------------------------------"""
people = People(from_iterator=some_function_that_yield_values)
"""-----------------------------------------------------------""" 
for people_chunk in People(from_csv=DATA_FILE, delimiter=";", chunksize=10):
    ...

example of function that yield values:

def some_function_that_yield_values():
    while something:
        ...
        yield name, age, money, insertion_date, is_staff

image

You can also save it to the database with the save() method (if the dtypes of the columns change, this will raise a ValidationError):

people.save()

You can upsert to the database and this will automatically look at the unique fields that were declared in the class

people.save(if_row_exists='update')
or
people.save(if_row_exists='ignore')

If you want to revalidate your dataframe (convert the columns dtypes to the type that was declared in the class), you can call the validate() method:

people.validate()

You can also validate from another class. For example, you can do something like this:

people = People(from_csv=DATA_FILE)
jobs = Jobs(from_sql_query='select * from jobs')
people_with_jobs = people.merge(jobs, on='name').validate(from_class=PeopleWithJobs)

This is the list of the overriten methods that return a pandas_oop custom dataframe

  • 'isnull'
  • 'head'
  • 'abs'
  • 'merge'
  • 'loc' and dataframe slicing

I will add more and more methods on this list.

New features

Alembic Database migration support added:

  • On your main application package, import Base (this is a declarative_base from sqlalchemy)
from pandas_oop import Base
  • Add this configuration on the env.py file of your alembic config
from your_app import Base
target_metadata = Base.metadata
  • And finaly, update your database url on your alembic.ini file

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

pandas-oop-0.9.6.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

pandas_oop-0.9.6-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file pandas-oop-0.9.6.tar.gz.

File metadata

  • Download URL: pandas-oop-0.9.6.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7

File hashes

Hashes for pandas-oop-0.9.6.tar.gz
Algorithm Hash digest
SHA256 7501790f174a3b5ff701d364d8a3e96687c37150968217d01923fd147a3125f9
MD5 cceab5d88b0124bd520813b33ffb43d9
BLAKE2b-256 fe1f79b07eaa823e7e11f6cabdeeb9218da9f1feb614ecd80d247b7f4138cdd4

See more details on using hashes here.

File details

Details for the file pandas_oop-0.9.6-py3-none-any.whl.

File metadata

  • Download URL: pandas_oop-0.9.6-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7

File hashes

Hashes for pandas_oop-0.9.6-py3-none-any.whl
Algorithm Hash digest
SHA256 89fcf42854fbd15ed02bad4304ed1a73e81c4c76080a739b2f8d4e42218a2d6f
MD5 14c7c85291cb989256486c39d1a27407
BLAKE2b-256 3b3d9209e57e7b466390662fbd519dbfa1b9c2d021d5b0e702569efc9bfb38c8

See more details on using hashes here.

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