ENH add drop_na argument to pivot_table · pandas-dev/pandas@2d63a71 (original) (raw)
1
1
`import unittest
`
2
2
``
3
3
`import numpy as np
`
``
4
`+
from numpy.testing import assert_equal
`
4
5
``
5
``
`-
from pandas import DataFrame, Series, Index
`
``
6
`+
from pandas import DataFrame, Series, Index, MultiIndex
`
6
7
`from pandas.tools.merge import concat
`
7
8
`from pandas.tools.pivot import pivot_table, crosstab
`
8
9
`import pandas.util.testing as tm
`
`@@ -62,6 +63,22 @@ def test_pivot_table_nocols(self):
`
62
63
`xp = df.pivot_table(rows='cols', aggfunc={'values': 'mean'}).T
`
63
64
`tm.assert_frame_equal(rs, xp)
`
64
65
``
``
66
`+
def test_pivot_table_dropna(self):
`
``
67
`+
df = DataFrame({'amount': {0: 60000, 1: 100000, 2: 50000, 3: 30000},
`
``
68
`+
'customer': {0: 'A', 1: 'A', 2: 'B', 3: 'C'},
`
``
69
`+
'month': {0: 201307, 1: 201309, 2: 201308, 3: 201310},
`
``
70
`+
'product': {0: 'a', 1: 'b', 2: 'c', 3: 'd'},
`
``
71
`+
'quantity': {0: 2000000, 1: 500000, 2: 1000000, 3: 1000000}})
`
``
72
`+
pv_col = df.pivot_table('quantity', 'month', ['customer', 'product'], dropna=False)
`
``
73
`+
pv_ind = df.pivot_table('quantity', ['customer', 'product'], 'month', dropna=False)
`
``
74
+
``
75
`+
m = MultiIndex.from_tuples([(u'A', u'a'), (u'A', u'b'), (u'A', u'c'), (u'A', u'd'),
`
``
76
`+
(u'B', u'a'), (u'B', u'b'), (u'B', u'c'), (u'B', u'd'),
`
``
77
`+
(u'C', u'a'), (u'C', u'b'), (u'C', u'c'), (u'C', u'd')])
`
``
78
+
``
79
`+
assert_equal(pv_col.columns.values, m.values)
`
``
80
`+
assert_equal(pv_ind.index.values, m.values)
`
``
81
+
65
82
``
66
83
`def test_pass_array(self):
`
67
84
`result = self.data.pivot_table('D', rows=self.data.A, cols=self.data.C)
`
`@@ -374,6 +391,16 @@ def test_crosstab_pass_values(self):
`
374
391
`aggfunc=np.sum)
`
375
392
`tm.assert_frame_equal(table, expected)
`
376
393
``
``
394
`+
def test_crosstab_dropna(self):
`
``
395
`+
GH 3820
`
``
396
`+
a = np.array(['foo', 'foo', 'foo', 'bar', 'bar', 'foo', 'foo'], dtype=object)
`
``
397
`+
b = np.array(['one', 'one', 'two', 'one', 'two', 'two', 'two'], dtype=object)
`
``
398
`+
c = np.array(['dull', 'dull', 'dull', 'dull', 'dull', 'shiny', 'shiny'], dtype=object)
`
``
399
`+
res = crosstab(a, [b, c], rownames=['a'], colnames=['b', 'c'], dropna=False)
`
``
400
`+
m = MultiIndex.from_tuples([('one', 'dull'), ('one', 'shiny'),
`
``
401
`+
('two', 'dull'), ('two', 'shiny')])
`
``
402
`+
assert_equal(res.columns.values, m.values)
`
``
403
+
377
404
`if name == 'main':
`
378
405
`import nose
`
379
406
`nose.runmodule(argv=[file, '-vvs', '-x', '--pdb', '--pdb-failure'],
`