pandas dataframe as field in django pandas dataframe as field in django pandas pandas

pandas dataframe as field in django


You're likely gonna want to use a PickleField to store your numpy or Pandas dataframe. The PickleField will serialize the data into a text field for the database storage and convert it back upon retrieval.

from django.db import modelsfrom picklefield.fields import PickledObjectFieldimport numpyclass DatafileModel(models.Model)    data = PickledObjectField()

And then just create or manipulate your model instances with numpy or pandas objects:

datafile = DatafileModel()datafile.data = numpy.array((1000,1000))datafile.save()


In order not to create a necessity to create model instances separately one could use the following simple approach

import pandas as pdfrom django.contrib.postgres.fields import JSONFieldclass StoredDataFrame(Model):    data = JSONField()    @classmethod    def putframe(cls, dataframe):        storeddataframe = cls(data=dataframe.to_json(orient='split'))        storeddataframe.save()        return storeddataframe    def loadframe(self):        return pd.read_json(self.data, orient='split')

Sample use:

    df = pd.DataFrame(np.random.randn(6,4), index=list('qwerty'), columns=list('ABCD'))    storeddata = StoredDataFrame.putframe(df)    retrieveddataframe = storeddata.loadframe()


Late to the discussion here, but if you're using PostgreSQL you could leverage it's JSON support via django_pgjsonb and convert back-and-forth between the DataFrame and JSON representations of the content.

Something like this should work (you'll need to play with the 'orient' parameter):

import pandas as pdfrom django_pgjsonb import JSONFieldclass MyModel(models.Model):    dfjson = JSONField()    @property    def store_dataframe(self, dataframe):        self.dfjson = dataframe.to_json()    @property    def load_dataframe(self):        return pandas.read_json(self.json)    my_instance = MyModel()my_instance.store_dataframe(df)df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))my_instance.store_dataframe(df)my_instance.save()new_df = my_instance.load_dataframe()#hopefully new_df and df are equivalent

https://github.com/yjmade/django-pgjsonb
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_json.htmlhttp://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_json.html