1
1
# pylint: disable=E1103
2
2
3
+ import warnings
4
+
3
5
from pandas import Series , DataFrame
4
6
from pandas .core .index import MultiIndex
5
7
from pandas .tools .merge import concat
10
12
import numpy as np
11
13
12
14
13
- def pivot_table (data , values = None , rows = None , cols = None , aggfunc = 'mean' ,
14
- fill_value = None , margins = False , dropna = True ):
15
+ def pivot_table (data , values = None , index = None , columns = None , aggfunc = 'mean' ,
16
+ fill_value = None , margins = False , dropna = True , ** kwarg ):
15
17
"""
16
18
Create a spreadsheet-style pivot table as a DataFrame. The levels in the
17
19
pivot table will be stored in MultiIndex objects (hierarchical indexes) on
@@ -21,9 +23,9 @@ def pivot_table(data, values=None, rows=None, cols=None, aggfunc='mean',
21
23
----------
22
24
data : DataFrame
23
25
values : column to aggregate, optional
24
- rows : list of column names or arrays to group on
26
+ index : list of column names or arrays to group on
25
27
Keys to group on the x-axis of the pivot table
26
- cols : list of column names or arrays to group on
28
+ columns : list of column names or arrays to group on
27
29
Keys to group on the y-axis of the pivot table
28
30
aggfunc : function, default numpy.mean, or list of functions
29
31
If list of functions passed, the resulting pivot table will have
@@ -35,6 +37,8 @@ def pivot_table(data, values=None, rows=None, cols=None, aggfunc='mean',
35
37
Add all row / columns (e.g. for subtotal / grand totals)
36
38
dropna : boolean, default True
37
39
Do not include columns whose entries are all NaN
40
+ rows : kwarg only alias of index [deprecated]
41
+ cols : kwarg only alias of columns [deprecated]
38
42
39
43
Examples
40
44
--------
@@ -50,8 +54,8 @@ def pivot_table(data, values=None, rows=None, cols=None, aggfunc='mean',
50
54
7 bar two small 6
51
55
8 bar two large 7
52
56
53
- >>> table = pivot_table(df, values='D', rows =['A', 'B'],
54
- ... cols =['C'], aggfunc=np.sum)
57
+ >>> table = pivot_table(df, values='D', index =['A', 'B'],
58
+ ... columns =['C'], aggfunc=np.sum)
55
59
>>> table
56
60
small large
57
61
foo one 1 4
@@ -63,21 +67,43 @@ def pivot_table(data, values=None, rows=None, cols=None, aggfunc='mean',
63
67
-------
64
68
table : DataFrame
65
69
"""
66
- rows = _convert_by (rows )
67
- cols = _convert_by (cols )
70
+ # Parse old-style keyword arguments
71
+ rows = kwarg .pop ('rows' , None )
72
+ if rows is not None :
73
+ warnings .warn ("rows is deprecated, use index" , FutureWarning )
74
+ if index is None :
75
+ index = rows
76
+ else :
77
+ msg = "Can only specify either 'rows' or 'index'"
78
+ raise TypeError (msg )
79
+
80
+ cols = kwarg .pop ('cols' , None )
81
+ if cols is not None :
82
+ warnings .warn ("cols is deprecated, use columns" , FutureWarning )
83
+ if columns is None :
84
+ columns = cols
85
+ else :
86
+ msg = "Can only specify either 'cols' or 'columns'"
87
+ raise TypeError (msg )
88
+
89
+ if kwarg :
90
+ raise TypeError ("Unexpected argument(s): %s" % kwarg .keys ())
91
+
92
+ index = _convert_by (index )
93
+ columns = _convert_by (columns )
68
94
69
95
if isinstance (aggfunc , list ):
70
96
pieces = []
71
97
keys = []
72
98
for func in aggfunc :
73
- table = pivot_table (data , values = values , rows = rows , cols = cols ,
99
+ table = pivot_table (data , values = values , index = index , columns = columns ,
74
100
fill_value = fill_value , aggfunc = func ,
75
101
margins = margins )
76
102
pieces .append (table )
77
103
keys .append (func .__name__ )
78
104
return concat (pieces , keys = keys , axis = 1 )
79
105
80
- keys = rows + cols
106
+ keys = index + columns
81
107
82
108
values_passed = values is not None
83
109
if values_passed :
@@ -106,7 +132,7 @@ def pivot_table(data, values=None, rows=None, cols=None, aggfunc='mean',
106
132
table = agged
107
133
if table .index .nlevels > 1 :
108
134
to_unstack = [agged .index .names [i ]
109
- for i in range (len (rows ), len (keys ))]
135
+ for i in range (len (index ), len (keys ))]
110
136
table = agged .unstack (to_unstack )
111
137
112
138
if not dropna :
@@ -132,14 +158,14 @@ def pivot_table(data, values=None, rows=None, cols=None, aggfunc='mean',
132
158
table = table .fillna (value = fill_value , downcast = 'infer' )
133
159
134
160
if margins :
135
- table = _add_margins (table , data , values , rows = rows ,
136
- cols = cols , aggfunc = aggfunc )
161
+ table = _add_margins (table , data , values , rows = index ,
162
+ cols = columns , aggfunc = aggfunc )
137
163
138
164
# discard the top level
139
165
if values_passed and not values_multi :
140
166
table = table [values [0 ]]
141
167
142
- if len (rows ) == 0 and len (cols ) > 0 :
168
+ if len (index ) == 0 and len (columns ) > 0 :
143
169
table = table .T
144
170
145
171
return table
@@ -299,18 +325,18 @@ def _convert_by(by):
299
325
return by
300
326
301
327
302
- def crosstab (rows , cols , values = None , rownames = None , colnames = None ,
303
- aggfunc = None , margins = False , dropna = True ):
328
+ def crosstab (index , columns , values = None , rownames = None , colnames = None ,
329
+ aggfunc = None , margins = False , dropna = True , ** kwarg ):
304
330
"""
305
331
Compute a simple cross-tabulation of two (or more) factors. By default
306
332
computes a frequency table of the factors unless an array of values and an
307
333
aggregation function are passed
308
334
309
335
Parameters
310
336
----------
311
- rows : array-like, Series, or list of arrays/Series
337
+ index : array-like, Series, or list of arrays/Series
312
338
Values to group by in the rows
313
- cols : array-like, Series, or list of arrays/Series
339
+ columns : array-like, Series, or list of arrays/Series
314
340
Values to group by in the columns
315
341
values : array-like, optional
316
342
Array of values to aggregate according to the factors
@@ -324,6 +350,8 @@ def crosstab(rows, cols, values=None, rownames=None, colnames=None,
324
350
Add row/column margins (subtotals)
325
351
dropna : boolean, default True
326
352
Do not include columns whose entries are all NaN
353
+ rows : kwarg only alias of index [deprecated]
354
+ cols : kwarg only alias of columns [deprecated]
327
355
328
356
Notes
329
357
-----
@@ -353,26 +381,48 @@ def crosstab(rows, cols, values=None, rownames=None, colnames=None,
353
381
-------
354
382
crosstab : DataFrame
355
383
"""
356
- rows = com ._maybe_make_list (rows )
357
- cols = com ._maybe_make_list (cols )
384
+ # Parse old-style keyword arguments
385
+ rows = kwarg .pop ('rows' , None )
386
+ if rows is not None :
387
+ warnings .warn ("rows is deprecated, use index" , FutureWarning )
388
+ if index is None :
389
+ index = rows
390
+ else :
391
+ msg = "Can only specify either 'rows' or 'index'"
392
+ raise TypeError (msg )
393
+
394
+ cols = kwarg .pop ('cols' , None )
395
+ if cols is not None :
396
+ warnings .warn ("cols is deprecated, use columns" , FutureWarning )
397
+ if columns is None :
398
+ columns = cols
399
+ else :
400
+ msg = "Can only specify either 'cols' or 'columns'"
401
+ raise TypeError (msg )
402
+
403
+ if kwarg :
404
+ raise TypeError ("Unexpected argument(s): %s" % kwarg .keys ())
405
+
406
+ index = com ._maybe_make_list (index )
407
+ columns = com ._maybe_make_list (columns )
358
408
359
- rownames = _get_names (rows , rownames , prefix = 'row' )
360
- colnames = _get_names (cols , colnames , prefix = 'col' )
409
+ rownames = _get_names (index , rownames , prefix = 'row' )
410
+ colnames = _get_names (columns , colnames , prefix = 'col' )
361
411
362
412
data = {}
363
- data .update (zip (rownames , rows ))
364
- data .update (zip (colnames , cols ))
413
+ data .update (zip (rownames , index ))
414
+ data .update (zip (colnames , columns ))
365
415
366
416
if values is None :
367
417
df = DataFrame (data )
368
418
df ['__dummy__' ] = 0
369
- table = df .pivot_table ('__dummy__' , rows = rownames , cols = colnames ,
419
+ table = df .pivot_table ('__dummy__' , index = rownames , columns = colnames ,
370
420
aggfunc = len , margins = margins , dropna = dropna )
371
421
return table .fillna (0 ).astype (np .int64 )
372
422
else :
373
423
data ['__dummy__' ] = values
374
424
df = DataFrame (data )
375
- table = df .pivot_table ('__dummy__' , rows = rownames , cols = colnames ,
425
+ table = df .pivot_table ('__dummy__' , index = rownames , columns = colnames ,
376
426
aggfunc = aggfunc , margins = margins , dropna = dropna )
377
427
return table
378
428
0 commit comments