Package: django-pandas
Tools for working withpandasin your Django
projectsContributorsChristopher ClarkeBertrand BordageGuillaume ThomasParbhat PuriFredrik Burman (coachHIPPO)Safe HammadJeff Sternber@MiddleForkDaniel AndrlikKevin AbbotYousuf Jawwad@henhuyHélio Meira Lins@utpyngoAnthony MontheVincent ToupetAnton Ian SiposThomas GraingerWhat’s NewVersion 0.6.5 added support for Pandas >= 1.3
and fixes a number of other issues.Dependenciesdjango-pandassupportsDjango(>=1.4.5) or later
and requiresdjango-model-utils(>= 1.4.0) andPandas(>= 0.12.0).Notebecause of problems with therequiresdirective of setuptools
you probably need to installnumpyin your virtualenv  before you install
this package or if you want to run the test suitepip install numpy
pip install -e .[test]
python runtests.pySomepandasfunctionality requires parts of the Scipy stack.
You may wish to consulthttp://www.scipy.org/install.htmlfor more information on installing theScipystack.You need to install your preferred version of Django.
as that Django 2 does not support Python 2.ContributingPlease file bugs and send pull requests to theGitHub repositoryandissue
tracker.InstallationStart by creating a newvirtualenvfor your projectmkvirtualenv myprojectNext installnumpyandpandasand optionallyscipypip install numpy
pip install pandasYou may want to consult  thescipy documentationfor more information
on installing theScipystack.Finally, installdjango-pandasusingpip:pip install django-pandasor install the development version fromgithubpip install https://github.com/chrisdev/django-pandas/tarball/masterUsageIO ModuleThedjango-pandas.iomodule provides some convenience methods to
facilitate the creation of DataFrames from Django QuerySets.read_frameParametersqs: A Django QuerySet.fieldnames: A list of model field names to use in creating theDataFrame.You can span a relationship in the usual Django way
by using  double underscores to specify a related field
in another modelindex_col: Use specify the field name to use  for theDataFrameindex.If the index
field is not in the field list it will be appendedcoerce_floatBoolean, defaults to TrueAttempt to convert values to non-string,
non-numeric objects (like decimal.Decimal)
to floating point.verbose:  If  this isTruethen populate the DataFrame with thehuman readable versions of any foreign key or choice fields
else use the actual values set in the model.column_names: If not None, use to override the column names in theDateFrameExamplesAssume that this is your model:class MyModel(models.Model):

    full_name = models.CharField(max_length=25)
    age = models.IntegerField()
    department = models.CharField(max_length=3)
    wage = models.FloatField()First create a query set:from django_pandas.io import read_frame
qs = MyModel.objects.all()To create a dataframe using all the fields in the underlying modeldf = read_frame(qs)Thedfwill contain human readable column values for foreign key and choice
fields. TheDataFramewill include all the fields in the underlying
model including the primary key.
To create a DataFrame using specified field names:df = read_frame(qs, fieldnames=['age', 'wage', 'full_name'])To setfull_nameas theDataFrameindexqs.to_dataframe(['age', 'wage'], index_col='full_name'])You can use filters and excludesqs.filter(age__gt=20, department='IT').to_dataframe(index_col='full_name')DataFrameManagerdjango-pandasprovides a custom manager to use with models that
you want to render as Pandas Dataframes. TheDataFrameManagermanager provides theto_dataframemethod that returns
your models queryset as a Pandas DataFrame. To use the DataFrameManager, first
override the default manager (objects) in your model’s definition
as shown in the example below#models.py

from django_pandas.managers import DataFrameManager

class MyModel(models.Model):

    full_name = models.CharField(max_length=25)
    age = models.IntegerField()
    department = models.CharField(max_length=3)
    wage = models.FloatField()

    objects = DataFrameManager()This will give you access to the following QuerySet methods:to_dataframeto_timeseriesto_pivot_tableto_dataframeReturns a DataFrame from the QuerySetParametersfieldnames:  The model field names to utilise in creating the frame.to span a relationship, use the field name of related
fields across models, separated by double underscores,index: specify the field to use  for the index. If the indexfield is not in the field list it will be appendedcoerce_float: Attempt to convert the numeric non-string datalike object, decimal etc. to float if possibleverbose:  If  this isTruethen populate the DataFrame with thehuman readable versions of any foreign key or choice fields
else use the actual value set in the model.ExamplesCreate a dataframe using all the fields  in your model as followsqs = MyModel.objects.all()

df = qs.to_dataframe()This will include your primary key. To create a DataFrame using specified
field names:df = qs.to_dataframe(fieldnames=['age', 'department', 'wage'])To setfull_nameas the indexqs.to_dataframe(['age', 'department', 'wage'], index='full_name'])You can use filters and excludesqs.filter(age__gt=20, department='IT').to_dataframe(index='full_name')to_timeseriesA convenience method for creating a time series i.e the
DataFrame index is instance of a DateTime or PeriodIndexParametersfieldnames:  The model field names to utilise in creating the frame.to span a relationship, just use the field name of related
fields across models, separated by double underscores,index: specify the field to use  for the index. If the indexfield is not in the field list it will be appended. This
is mandatory.storage:  Specify if the queryset uses thewideorlongformatfor data.pivot_columns: Required once the you specifylongformatstorage. This could either be a list or string identifying
the field name or combination of field. If the pivot_column
is a single column then the unique values in this column become
a new columns in the DataFrame
If the pivot column is a list the values in these columns are
concatenated (using the ‘-’ as a separator)
and these values are used for the new timeseries columnsvalues: Also required if you utilize thelongstorage thevalues column name is use for populating new frame valuesfreq: the offset string or object representing a target conversionrs_kwargs: Arguments based on pandas.DataFrame.resampleverbose:  If  this isTruethen populate the DataFrame with thehuman readable versions of any foreign key or choice fields
else use the actual value set in the model.ExamplesUsing alongstorage format#models.py

class LongTimeSeries(models.Model):
    date_ix = models.DateTimeField()
    series_name = models.CharField(max_length=100)
    value = models.FloatField()

    objects = DataFrameManager()Some sample data::========   =====       =====
date_ix    series_name value
========   =====       ======
2010-01-01  gdp        204699

2010-01-01  inflation  2.0

2010-01-01  wages      100.7

2010-02-01  gdp        204704

2010-02-01  inflation  2.4

2010-03-01  wages      100.4

2010-02-01  gdp        205966

2010-02-01  inflation  2.5

2010-03-01  wages      100.5
==========  ========== ======Create a QuerySetqs = LongTimeSeries.objects.filter(date_ix__year__gte=2010)Create a timeseries dataframedf = qs.to_timeseries(index='date_ix',
                      pivot_columns='series_name',
                      values='value',
                      storage='long')
df.head()

date_ix      gdp     inflation     wages

2010-01-01   204966     2.0       100.7

2010-02-01   204704      2.4       100.4

2010-03-01   205966      2.5       100.5Using awidestorage formatclass WideTimeSeries(models.Model):
    date_ix = models.DateTimeField()
    col1 = models.FloatField()
    col2 = models.FloatField()
    col3 = models.FloatField()
    col4 = models.FloatField()

    objects = DataFrameManager()

qs = WideTimeSeries.objects.all()

rs_kwargs = {'how': 'sum', 'kind': 'period'}
df = qs.to_timeseries(index='date_ix', pivot_columns='series_name',
                      values='value', storage='long',
                      freq='M', rs_kwargs=rs_kwargs)to_pivot_tableA convenience method for creating a pivot table from a QuerySetParametersfieldnames:  The model field names to utilise in creating the frame.to span a relationship, just use the field name of related
fields across models, separated by double underscores,values : column to aggregate, optionalrowslist of column names or arrays to group onKeys to group on the x-axis of the pivot tablecolslist of column names or arrays to group onKeys to group on the y-axis of the pivot tableaggfuncfunction, default numpy.mean, or list of functionsIf list of functions passed, the resulting pivot table will have
hierarchical columns whose top level are the function names
(inferred from the function objects themselves)fill_valuescalar, default NoneValue to replace missing values withmarginsboolean, default FalseAdd all row / columns (e.g. for subtotal / grand totals)dropna : boolean, default TrueExample# models.py
class PivotData(models.Model):
    row_col_a = models.CharField(max_length=15)
    row_col_b = models.CharField(max_length=15)
    row_col_c = models.CharField(max_length=15)
    value_col_d = models.FloatField()
    value_col_e = models.FloatField()
    value_col_f = models.FloatField()

    objects = DataFrameManager()Usagerows = ['row_col_a', 'row_col_b']
cols = ['row_col_c']

pt = qs.to_pivot_table(values='value_col_d', rows=rows, cols=cols)CHANGES0.6.6 (2012-10-27)The main feature of this is release in the use of a GHA to
automate the publishing of the package to PYPI as per PR#146(again much thanks @graingert). Several other minor issues have also
been addressed.0.6.5 (2021-10-15)This version added support for Pandas >=1.3 (thanks to @graingert)Other Changes:Migrated from Travis to Github Actions for CI (also @graingert)Avoid the use of deprecated methods#139and#142(again much thanks @graingert)Fix for issue#135(Thanks @Yonimdo)Silence Django 3.2 errors on testing on etc.#133thanks @whyscream.0.6.4 (2021-02-08)Bumped version number as the previous release was incorrectly uploaded
to pypi0.6.1 (2020-05-26)Supports the latest release of Pandas 1.0.30.6.0 (2019-01-11)Removes compatibility with Django versions < 1.80.5.2 (2019-01-3)This is the last version that supports Django < 1.8Improved coerce_float option (thanks @ZuluPro )Ensure compatibility with legacy versions of Django ( < 1.8)Test pass with Django 2+ and python 3.70.5.1 (2018-01-26)Address Unicode decode error when installing with pip3 on docker (Thanks @utapyngo)0.5.0 (2018-01-20)Django 2.0 compatibility (Thanks @meirains)0.4.5 (2017-10-4)A Fix for fieldname deduplication bug thanks to @kgabbott0.4.4 (2017-07-16)Theverboseargument now handles more use cases (Thanks to @henhuy and
Kevin Abbott)Corece float argument add to`to_timeseries()`method (Thanks Yousuf Jawwad)0.4.3 (2017-06-02)Fix doc typos and formattingPrevent column duplication in read_frame (Thanks Kevin Abbott)0.4.2 (2017-05-22)Compatibility withpandas 0.20.1Support for Python 2.7 and 3.5 with Django versions 1.8+Suport for Python 3.6 and Django 1.11We still support legacy versions (Python 2.7 and Django 1.4)0.4.1 (2016-02-05)Address the incompatibility with Django 1.9 due to the removal of
specialized query sets like theValuesQuerySetAddress the removal of thePassThrougManagerfromdjango-model-utilsversion2.4.  We’ve removed the dependency on django-model-utils and
included the PassThroughManger (which was always a standalone tool
distributed a part of django-model-utils) for compatibility with
earlier versions of Django (<= 1.8). For more recent versions of
Django we’re using Django’s built inQuerySet.as_manager().Now supports Pandas 0.14.1 and aboveThe fall in Coverage in this release largely reflects the integration of
the PassThroughManager into the code base. We’ll add the required test
coverage for the PassThroughManager in subsequent releases.0.3.1 (2015-10-25)Extends the ability to span a ForeignKey relationship with double underscores
to OneToOneField too thanks to Safe HammadProvide better support for  ManyToMany and OneToMany relations thanks to
Jeff Sternberg and @MiddleFork0.3.0 (2015-06-16)This version supports Django 1.8Support for Pandas 0.160.2.2 (2015-03-02)Added Support for Django 1.70.2.1 (2015-01-28)Added Support for Values QuerySetsSupport for Python 2.6Note we still have limited support for Django 1.7 but this will be coming in
the next release0.2.0 (2014-06-15)Added theiomodule so that DataFrames can be created from any
queryset so you don’t need to to add aDataFrame managerto your
models. This is good for working with legacy projects.added a Booleanverboseargument to all methods (which defaults toTrue)
This populate the DataFrame columns with the human readable versions of
foreign key or choice fields.Improved the performance DataFrame creation by removing dependency onnp.core.records.fromrecordsLoads of bug fixes, more tests and improved coverage and better
documentation