test_pivot.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import numpy as np
  2. import pandas as pd
  3. import pandas.util.testing as tm
  4. class TestPivotTable(object):
  5. def setup_method(self, method):
  6. self.dense = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar',
  7. 'foo', 'bar', 'foo', 'foo'],
  8. 'B': ['one', 'one', 'two', 'three',
  9. 'two', 'two', 'one', 'three'],
  10. 'C': np.random.randn(8),
  11. 'D': np.random.randn(8),
  12. 'E': [np.nan, np.nan, 1, 2,
  13. np.nan, 1, np.nan, np.nan]})
  14. self.sparse = self.dense.to_sparse()
  15. def test_pivot_table(self):
  16. res_sparse = pd.pivot_table(self.sparse, index='A', columns='B',
  17. values='C')
  18. res_dense = pd.pivot_table(self.dense, index='A', columns='B',
  19. values='C')
  20. tm.assert_frame_equal(res_sparse, res_dense)
  21. res_sparse = pd.pivot_table(self.sparse, index='A', columns='B',
  22. values='E')
  23. res_dense = pd.pivot_table(self.dense, index='A', columns='B',
  24. values='E')
  25. tm.assert_frame_equal(res_sparse, res_dense)
  26. res_sparse = pd.pivot_table(self.sparse, index='A', columns='B',
  27. values='E', aggfunc='mean')
  28. res_dense = pd.pivot_table(self.dense, index='A', columns='B',
  29. values='E', aggfunc='mean')
  30. tm.assert_frame_equal(res_sparse, res_dense)
  31. # ToDo: sum doesn't handle nan properly
  32. # res_sparse = pd.pivot_table(self.sparse, index='A', columns='B',
  33. # values='E', aggfunc='sum')
  34. # res_dense = pd.pivot_table(self.dense, index='A', columns='B',
  35. # values='E', aggfunc='sum')
  36. # tm.assert_frame_equal(res_sparse, res_dense)
  37. def test_pivot_table_multi(self):
  38. res_sparse = pd.pivot_table(self.sparse, index='A', columns='B',
  39. values=['D', 'E'])
  40. res_dense = pd.pivot_table(self.dense, index='A', columns='B',
  41. values=['D', 'E'])
  42. res_dense = res_dense.apply(lambda x: x.astype("Sparse[float64]"))
  43. tm.assert_frame_equal(res_sparse, res_dense)