django-pandasjsonfield
A custom django model field to handle pandas Series and DataFrame.
Pandas objects are saved in JSON format in a Django TextField.
Compatibility
Tested with :
- Python 2.7, 3.4, and 3.5 pandas supported versions
django>=1.4
(django>=1.5
forpython>=3
)pandas>=0.16
Usage
-
Installation
pip install pandasjsonfield
-
Create a model:
from pandasjsonfield import PandasJSONField class MyModel(models.Model): serie = PandasJSONField(typ="serie", null=True) dataframe = PandasJSONField(typ="frame", null=True)
and migrate your database with usual django commands.
-
Manipulate it as usual:
import pandas as pd s = pd.Series([1,2,3,4]) df = pd.DataFrame({"a":[1,2,3], "b":[11,12,13]}) m = MyModel(serie=s, dataframe=df) m.save() m = MyModel.objects.get(pk=1) print m.dataframe.describe() # m.dataframe is a pandas.DataFrame
Changelog
1.0 => 1.?
-
Support for
django-1.9
andpandas-0.18
-
Change the method for object json-serialization from
orient="index"
toorient="split"
(see pandas doc for more informations):- allows to save the
Series
name - fixes a bug in the data types in deserialization (see this issue)
- If you are upgrading from version
1.0
, the first time you read your object, they will be deserialized correctly and re-serialized with the new method. This should be a smooth transition but please report any issue you may have in the issue tracker.
- allows to save the
Inspiration
I have taken (a lot of) inspiration or technical solutions from the following repos:
Thanks to their authors.
Want to contribute?
Do not hesitate to open an issue or fork!