@@ -14,24 +14,40 @@ def to_pivot_table(self, fieldnames=(), verbose=True,
14
14
as a DataFrame
15
15
Parameters
16
16
----------
17
- fieldnames: The model field names to utilise in creating the frame.
18
- to span a relationship, just use the field name of related
19
- fields across models, separated by double underscores,
20
- values : column to aggregate, optional
21
- rows : list of column names or arrays to group on
22
- Keys to group on the x-axis of the pivot table
23
- cols : list of column names or arrays to group on
24
- Keys to group on the y-axis of the pivot table
25
- aggfunc : function, default numpy.mean, or list of functions
26
- If list of functions passed, the resulting pivot table will have
27
- hierarchical columns whose top level are the function names
28
- (inferred from the function objects themselves)
29
- fill_value : scalar, default None
30
- Value to replace missing values with
31
- margins : boolean, default False
32
- Add all row / columns (e.g. for subtotal / grand totals)
33
- dropna : boolean, default True
34
- Do not include columns whose entries are all NaN
17
+ fieldnames: The model field names(columns) to utilise in creating
18
+ the DataFrame. You can span a relationships in the usual
19
+ Django ORM way by using the foreign key field name
20
+ separated by double underscores and refer to a field
21
+ in a related model.
22
+
23
+ values: The field to use to calculate the values to aggregate.
24
+
25
+ rows: The list of field names to group on
26
+ Keys to group on the x-axis of the pivot table
27
+
28
+ cols: The list of column names or arrays to group on
29
+ Keys to group on the y-axis of the pivot table
30
+
31
+ aggfunc: How to arregate the values. By default this would be
32
+ ``numpy.mean``. A list of aggregates functions can be passed
33
+ In this case the resulting pivot table will have
34
+ hierarchical columns whose top level are the function names
35
+ (inferred from the function objects themselves)
36
+
37
+ fill_value: A scalar value to replace the missing values with
38
+
39
+ margins: Boolean, default False Add all row / columns
40
+ (e.g. for subtotal / grand totals)
41
+
42
+ dropna: Boolean, default True.
43
+ Do not include columns whose entries are all NaN
44
+
45
+ verbose: If this is ``True`` then populate the DataFrame with the
46
+ human readable versions for foreign key fields else
47
+ the primary keys values will be used for foreign key fields.
48
+ The human readable version of the foreign key field is
49
+ defined in the ``__unicode__`` or ``__str__``
50
+ methods of the related class definition
35
51
"""
36
52
df = self .to_dataframe (fieldnames , verbose = verbose )
37
53
@@ -44,38 +60,70 @@ def to_timeseries(self, fieldnames=(), verbose=True,
44
60
values = None , pivot_columns = None , freq = None ,
45
61
rs_kwargs = None ):
46
62
"""
47
- A convenience method for creating a time series i.e the
48
- DataFrame index is instance of a DateTime or PeriodIndex
63
+ A convenience method for creating a time series DataFrame i.e the
64
+ DataFrame index will be an instance of DateTime or PeriodIndex
49
65
50
66
Parameters
51
67
----------
52
68
53
- fieldnames: The model field names to utilise in creating the frame.
54
- to span a relationship, just use the field name of related
55
- fields across models, separated by double underscores,
56
-
57
- index: specify the field to use for the index. If the index
58
- field is not in the field list it will be appended. This
59
- is mandatory.
60
-
61
- storage: Specify if the queryset uses the `wide` or `long` format
62
- for data.
63
-
64
- pivot_column: Required once the you specify `long` format
65
- storage. This could either be a list or string identifying
66
- the field name or combination of field. If the pivot_column
67
- is a single column then the unique values in this column become
68
- a new columns in the DataFrame
69
- If the pivot column is a list the values in these columns are
70
- concatenated (using the '-' as a separator)
71
- and these values are used for the new timeseries columns
72
-
73
- values: Also required if you utilize the `long` storage the
74
- values column name is use for populating new frame values
75
-
76
- freq: the offset string or object representing a target conversion
77
-
78
- rs_kwargs: Arguments based on pandas.DataFrame.resample
69
+ fieldnames: The model field names(columns) to utilise in creating
70
+ the DataFrame. You can span a relationships in the usual
71
+ Django ORM way by using the foreign key field name
72
+ separated by double underscores and refer to a field
73
+ in a related model.
74
+
75
+ index: specify the field to use for the index. If the index
76
+ field is not in fieldnames it will be appended. This
77
+ is mandatory for timeseries.
78
+
79
+ storage: Specify if the queryset uses the
80
+ ``wide`` format
81
+
82
+ date | col1| col2| col3|
83
+ -----------|------|-----|-----|
84
+ 2001-01-01-| 100.5| 23.3| 2.2|
85
+ 2001-02-01-| 106.3| 17.0| 4.6|
86
+ 2001-03-01-| 111.7| 11.1| 0.7|
87
+
88
+ or the `long` format.
89
+
90
+ date |values| names|
91
+ -----------|------|------|
92
+ 2001-01-01-| 100.5| col1|
93
+ 2001-02-01-| 106.3| col1|
94
+ 2001-03-01-| 111.7| col1|
95
+ 2001-01-01-| 23.3| col2|
96
+ 2001-02-01-| 17.0| col2|
97
+ 2001-01-01-| 23.3| col2|
98
+ 2001-02-01-| 2.2| col3|
99
+ 2001-03-01-| 4.6| col3|
100
+ 2001-03-01-| 0.7| col3|
101
+
102
+
103
+ pivot_column: Required once the you specify `long` format
104
+ storage. This could either be a list or string
105
+ identifying the field name or combination of field.
106
+ If the pivot_column is a single column then the
107
+ unique values in this column become a new columns in
108
+ the DataFrame If the pivot column is a list the values
109
+ in these columns are concatenated (using the '-'
110
+ as a separator) and these values are used for the new
111
+ timeseries columns
112
+
113
+ values: Also required if you utilize the `long` storage the
114
+ values column name is use for populating new frame values
115
+
116
+ freq: The offset string or object representing a target conversion
117
+
118
+ rs_kwargs: A dictonary of keyword arguments based on the
119
+ ``pandas.DataFrame.resample`` method
120
+
121
+ verbose: If this is ``True`` then populate the DataFrame with the
122
+ human readable versions of any foreign key fields else use
123
+ the primary keys values.
124
+ The human readable version of the foreign key field is
125
+ defined in the ``__unicode__`` or ``__str__``
126
+ methods of the related class definition
79
127
"""
80
128
if index is None :
81
129
raise AssertionError ('You must supply an index field' )
@@ -114,40 +162,38 @@ def to_timeseries(self, fieldnames=(), verbose=True,
114
162
115
163
return df
116
164
117
- def to_dataframe (self , fieldnames = (), verbose = True , index = None , fill_na = None ,
165
+ def to_dataframe (self , fieldnames = (), verbose = True , index = None ,
118
166
coerce_float = False ):
119
167
"""
120
168
Returns a DataFrame from the queryset
121
169
122
170
Paramaters
123
171
-----------
124
172
125
- fieldnames: The model fields to utilise in creating the frame.
126
- to span a relationship, just use the field name of related
127
- fields across models, separated by double underscores,
173
+ fieldnames: The model field names(columns) to utilise in creating
174
+ the DataFrame. You can span a relationships in the usual
175
+ Django ORM way by using the foreign key field name
176
+ separated by double underscores and refer to a field
177
+ in a related model.
128
178
129
179
130
- index: specify the field to use for the index. If the index
131
- field is not in the field list it will be appended
180
+ index: specify the field to use for the index. If the index
181
+ field is not in fieldnames it will be appended. This
182
+ is mandatory for timeseries.
132
183
133
- fill_na: fill in missing observations using one of the following
134
- this is a string specifying a pandas fill method
135
- {'backfill, 'bill', 'pad', 'ffill'} or a scalar value
184
+ verbose: If this is ``True`` then populate the DataFrame with the
185
+ human readable versions for foreign key fields else
186
+ the primary keys values will be used for foreign key fields.
187
+ The human readable version of the foreign key field is
188
+ defined in the ``__unicode__`` or ``__str__``
189
+ methods of the related class definition
136
190
137
- coerce_float: Attempt to convert the numeric non-string data
138
- like object, decimal etc. to float if possible
139
191
"""
140
192
141
193
df = read_frame (self , fieldnames = fieldnames , verbose = verbose ,
142
194
index_col = index ,
143
195
coerce_float = coerce_float )
144
196
145
- if fill_na is not None :
146
- if fill_na not in ('backfill' , 'bfill' , 'pad' , 'ffill' ):
147
- df = df .fillna (value = fill_na )
148
- else :
149
- df = df .fillna (method = fill_na )
150
-
151
197
return df
152
198
153
199
0 commit comments