Pivot 1.0.2

Pivot 1.0.2 – Display Rotation. Pivot is the ultimate menu bar tool to rotate your display with only one click. FEATURES: Pivot enhances your workflow, because you can now rotate the screen of your display with only one click. You can also assign a keyboard shortcut or select the desired rotation from the menu.

Latest version

Released:

Create pivot tables and histograms from ORM querysets

Project description

This package provides utilities for turning Django Querysets intoPivot-Tables and Histogramsby letting your database do all the heavy lifting.

Examples

I am going to shamelessly lift examples from the wikipedia page referenced in the header.Here is part of the table of shirt sales:

RegionGenderStyleShip DateUnitsPriceCost
EastBoyTee1/31/20051211.0410.42
EastBoyGolf1/31/2005121312.6
EastBoyFancy1/31/20051211.9611.74
EastGirlTee1/31/20051011.2710.56
EastGirlGolf1/31/20051012.1211.95
EastGirlFancy1/31/20051013.7413.33
WestBoyTee1/31/20051111.4410.94
WestBoyGolf1/31/20051112.6311.73
WestBoyFancy1/31/20051112.0611.51
WestGirlTee1/31/20051513.4213.29
WestGirlGolf1/31/20051511.4810.67
Etc.

We might want to know how many Units did we sell in each Region for every Ship Date?And get a result like:

Region1/31/20052/1/20052/2/20052/3/20052/4/2005
East668010293114
North86919588107
South7378847691
West92103111104123

It takes 3 quantities to pivot the original table into the summary result, two columns andan aggregate of a third column. In this case the two columns are Region and Ship Date, thethird column is Units and the aggregate is Sum

Basic usage

The pivot function

Pivot tables are generated by the pivot function, which takes a Model and 3 attribute names,to make a pivot table like the example above:

The result is a ValuesQuerySet, which means the objects returned are dictionaries. Eachdictionary has a key for the row (‘shipped’ dates in this case) and a key for every valueof the column (‘region’ in this case).

The first argument can be a Model, QuerySet, or Manager. This allows you to generate a pivottable filtered by another column. For example, you may want to know how many units were soldin each region for every shipped date, but only for Golf shirts:

The pivot function takes an optional parameter for how to aggregate the data. For example,instead of the total units sold in each region for every ship date, we might be interested inthe average number of units per order. Then we can pass the Avg aggregation function

If your data is stored across multiple tables, use Django’s double underscore notationto traverse foreign key relationships. For example, instead of the ShirtSales model havinga region attribute, it might have a foreign key to a Store model, which in turn has aforeign key to a Region model, which has an attribute called name. Then our pivot call lookslike

It’s also possible that the data column we are aggregating over should be a computed column.In our example ShirtSales model we are storing the number of units and the price perunit, but not the total cost of the order. If we want to know the average order size indollars in each region for every ship date, we can pivot the ShirtSales table:

If the rows should be grouped on a compound column, for example, you want to know how manyUnits were sold on each ship date not just split by region, but the combination of regionand gender, you can pass a list to the first argument:

1.0.2

To change the way the row keys are displayed, a display_transform function can be passed tothe pivot function. display_transform is a function that takes a string and returns a string.For example, instead of getting the results with North, East, South, and West for the regionsyou want them all lower cased, you can do the following

If there are no records in the original data table for a particular cell in the pivot result,SQL will return NULL and this gets translated to None in python. If you want to get zero, orsome other default, you can pass that as a parameter to pivot:

The above call ensures that when there are no units sold in a particular region on a particulardate, we get zero as the result instead of None. However, the results will only containshipped dates if at least one region had sales on that date. If it’s necessary to get resultsfor all dates in a range including dates where there are no ShirtSales records, we can passa target row_range:

Will output a result with every shipped date from Jan 1st to February 28th whether there aresales on those days or not.

The histogram function

This library also supports creating histograms from a single column of data with thehistogram function, which takes a Model, a single attribute name and an iterable of left edgesof bins.

Like pivot, the first argument can be a Model, QuerySet, or Manager. The result is alist of dictionaries:

It’s also possible to get several histograms from a single query by slicing the data on oneof the columns. For example, instead of the histogram above, we might want two histograms,one for boys and one for girls. The gender column of ShirtSales has two values,'Boy' and 'Girl'. Passing the gender column as a 4th optional parameter to histogramwill slice the data on that column.

The result is a ValuesQuerySet where each row corresponds to one bin

Installation

Just:

put django_pivot in installed apps in your settings file, and then you:

And off you go.

Tests

The test suite is run by Traviswith Django versions 1.11, 2.0 and 2.1 and backends sqlite, MySQL, and Postgres. If youwant to run the test suite locally, from the root directory:

That will use sqlite as the backend and whatever version of Django you havein your current environment.

License

MIT

Copyright 2017 Brad Martsberger

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Release historyRelease notifications

1.8.1

1.8.0

1.7

1.0.2

1.6.1

1.6

1.5

1.4.1

1.4

1.3

1.2

1.0.2

1.0.1

1.0

Pivot 1.0.2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-pivot, version 1.8.1
Filename, sizeFile typePython versionUpload dateHashes
Filename, size django_pivot-1.8.1-py3-none-any.whl (14.2 kB) File type Wheel Python version py3 Upload dateHashes
Filename, size django-pivot-1.8.1.tar.gz (14.1 kB) File type Source Python version None Upload dateHashes
Close

Pivot 1.0.2 Minecraft

Hashes for django_pivot-1.8.1-py3-none-any.whl

Pivot 1.0.2 Pack

Hashes for django_pivot-1.8.1-py3-none-any.whl
AlgorithmHash digest
SHA2569bf83b2b61d4dc95c01e5b7a595ee223c5c1f08a4590733673a306b1513174d4
MD5aefdcc0063851e9a6901a43c8c05eb30
BLAKE2-256e9a959f23651ef9c2ac663016103e00ca765d1e92b4d278ec7eeed140321e273
Close

Pivot 1.0.2 Version

Hashes for django-pivot-1.8.1.tar.gz

Pivot 1.0.2 Pro

Hashes for django-pivot-1.8.1.tar.gz
AlgorithmHash digest
SHA2567184d3e3f5e96003150428bea106a9963f49f0431fa56f93595316c9b42bcca6
MD587aac3f02a0c392ff0569628d17f0116
BLAKE2-256418dc799ab511ea700e714b93b533fc45e7a4c8b4ea780cfedae7cc60f8849fe