Django-Pandas Documentation: Release 0.0.2
Django-Pandas Documentation: Release 0.0.2
Release 0.0.2
Christopher Clarke
1 Contributors 3
2 What’s New 5
3 Dependencies 7
4 Contributing 9
5 Installation 11
6 Usage 13
6.1 IO Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
6.2 DataFrameManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
6.3 to_timeseries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
6.4 to_pivot_table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
i
ii
django-pandas Documentation, Release 0.0.2
Contents 1
django-pandas Documentation, Release 0.0.2
2 Contents
CHAPTER 1
Contributors
• Christopher Clarke
• Bertrand Bordage
• Guillaume Thomas
• Parbhat Puri
• Fredrik Burman (coachHIPPO)
• Safe Hammad
• Jeff Sternber
• @MiddleFork
• Daniel Andrlik
• Kevin Abbot
• Yousuf Jawwad
• @henhuy
• Hélio Meira Lins
• @utpyngo
• Anthony Monthe
3
django-pandas Documentation, Release 0.0.2
4 Chapter 1. Contributors
CHAPTER 2
What’s New
5
django-pandas Documentation, Release 0.0.2
Dependencies
django-pandas supports Django (>=1.4.5) or later and requires django-model-utils (>= 1.4.0) and Pandas (>=
0.12.0). Note because of problems with the requires directive of setuptools you probably need to install numpy in
your virtualenv before you install this package or if you want to run the test suite
Some pandas functionality requires parts of the Scipy stack. You may wish to consult http://www.scipy.org/install.
html for more information on installing the Scipy stack.
You need to install your preferred version of Django. as that Django 2 does not support Python 2.
7
django-pandas Documentation, Release 0.0.2
8 Chapter 3. Dependencies
CHAPTER 4
Contributing
Please file bugs and send pull requests to the GitHub repository and issue tracker.
9
django-pandas Documentation, Release 0.0.2
10 Chapter 4. Contributing
CHAPTER 5
Installation
mkvirtualenv myproject
You may want to consult the scipy documentation for more information on installing the Scipy stack.
Finally, install django-pandas using pip:
11
django-pandas Documentation, Release 0.0.2
12 Chapter 5. Installation
CHAPTER 6
Usage
6.1 IO Module
The django-pandas.io module provides some convenience methods to facilitate the creation of DataFrames from
Django QuerySets.
6.1.1 read_frame
Parameters
• qs: A Django QuerySet.
• fieldnames: A list of model field names to use in creating the DataFrame. You can span a relationship in
the usual Django way by using double underscores to specify a related field in another model
• index_col: Use specify the field name to use for the DataFrame index. If the index field is not in the field
list it will be appended
• coerce_float [Boolean, defaults to True] Attempt to convert values to non-string, non-numeric objects (like
decimal.Decimal) to floating point.
• verbose: If this is True then populate the DataFrame with the human readable versions of any foreign key
or choice fields else use the actual values set in the model.
6.1.2 Examples
class MyModel(models.Model):
full_name = models.CharField(max_length=25)
age = models.IntegerField()
(continues on next page)
13
django-pandas Documentation, Release 0.0.2
df = read_frame(qs)
The df will contain human readable column values for foreign key and choice fields. The DataFrame will include all
the fields in the underlying model including the primary key. To create a DataFrame using specified field names:
qs.filter(age__gt=20, department='IT').to_dataframe(index='full_name')
6.2 DataFrameManager
django-pandas provides a custom manager to use with models that you want to render as Pandas Dataframes. The
DataFrameManager manager provides the to_dataframe method 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
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()
14 Chapter 6. Usage
django-pandas Documentation, Release 0.0.2
6.2.1 to_dataframe
6.2.2 Examples
qs = MyModel.objects.all()
df = qs.to_dataframe()
This will include your primary key. To create a DataFrame using specified field names:
qs.filter(age__gt=20, department='IT').to_dataframe(index='full_name')
6.3 to_timeseries
A convenience method for creating a time series i.e the DataFrame index is instance of a DateTime or PeriodIndex
Parameters
• fieldnames: 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 index field is not in the field list it will be appended. This
is mandatory.
• storage: Specify if the queryset uses the wide or long format for data.
• pivot_columns: Required once the you specify long format storage. This could either be a list or string iden-
tifying 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
columns
• values: Also required if you utilize the long storage the values column name is use for populating new frame
values
6.3. to_timeseries 15
django-pandas Documentation, Release 0.0.2
6.3.1 Examples
#models.py
class LongTimeSeries(models.Model):
date_ix = models.DateTimeField()
series_name = models.CharField(max_length=100)
value = models.FloatField()
objects = DataFrameManager()
Create a QuerySet
qs = LongTimeSeries.objects.filter(date_ix__year__gte=2010)
df = qs.to_timeseries(index='date_ix',
pivot_columns='series_name',
values='value',
storage='long')
df.head()
16 Chapter 6. Usage
django-pandas Documentation, Release 0.0.2
class 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()
6.4 to_pivot_table
# 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)
(continues on next page)
6.4. to_pivot_table 17
django-pandas Documentation, Release 0.0.2
objects = DataFrameManager()
Usage
18 Chapter 6. Usage