test_format.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # -*- coding: utf-8 -*-
  2. from __future__ import print_function
  3. import numpy as np
  4. from pandas.compat import is_platform_32bit, is_platform_windows
  5. import pandas as pd
  6. from pandas.core.config import option_context
  7. import pandas.util.testing as tm
  8. use_32bit_repr = is_platform_windows() or is_platform_32bit()
  9. class TestSparseSeriesFormatting(object):
  10. @property
  11. def dtype_format_for_platform(self):
  12. return '' if use_32bit_repr else ', dtype=int32'
  13. def test_sparse_max_row(self):
  14. s = pd.Series([1, np.nan, np.nan, 3, np.nan]).to_sparse()
  15. result = repr(s)
  16. dfm = self.dtype_format_for_platform
  17. exp = ("0 1.0\n1 NaN\n2 NaN\n3 3.0\n"
  18. "4 NaN\ndtype: Sparse[float64, nan]\nBlockIndex\n"
  19. "Block locations: array([0, 3]{0})\n"
  20. "Block lengths: array([1, 1]{0})".format(dfm))
  21. assert result == exp
  22. def test_sparsea_max_row_truncated(self):
  23. s = pd.Series([1, np.nan, np.nan, 3, np.nan]).to_sparse()
  24. dfm = self.dtype_format_for_platform
  25. with option_context("display.max_rows", 3):
  26. # GH 10560
  27. result = repr(s)
  28. exp = ("0 1.0\n ... \n4 NaN\n"
  29. "Length: 5, dtype: Sparse[float64, nan]\nBlockIndex\n"
  30. "Block locations: array([0, 3]{0})\n"
  31. "Block lengths: array([1, 1]{0})".format(dfm))
  32. assert result == exp
  33. def test_sparse_mi_max_row(self):
  34. idx = pd.MultiIndex.from_tuples([('A', 0), ('A', 1), ('B', 0),
  35. ('C', 0), ('C', 1), ('C', 2)])
  36. s = pd.Series([1, np.nan, np.nan, 3, np.nan, np.nan],
  37. index=idx).to_sparse()
  38. result = repr(s)
  39. dfm = self.dtype_format_for_platform
  40. exp = ("A 0 1.0\n 1 NaN\nB 0 NaN\n"
  41. "C 0 3.0\n 1 NaN\n 2 NaN\n"
  42. "dtype: Sparse[float64, nan]\nBlockIndex\n"
  43. "Block locations: array([0, 3]{0})\n"
  44. "Block lengths: array([1, 1]{0})".format(dfm))
  45. assert result == exp
  46. with option_context("display.max_rows", 3,
  47. "display.show_dimensions", False):
  48. # GH 13144
  49. result = repr(s)
  50. exp = ("A 0 1.0\n ... \nC 2 NaN\n"
  51. "dtype: Sparse[float64, nan]\nBlockIndex\n"
  52. "Block locations: array([0, 3]{0})\n"
  53. "Block lengths: array([1, 1]{0})".format(dfm))
  54. assert result == exp
  55. def test_sparse_bool(self):
  56. # GH 13110
  57. s = pd.SparseSeries([True, False, False, True, False, False],
  58. fill_value=False)
  59. result = repr(s)
  60. dtype = '' if use_32bit_repr else ', dtype=int32'
  61. exp = ("0 True\n1 False\n2 False\n"
  62. "3 True\n4 False\n5 False\n"
  63. "dtype: Sparse[bool, False]\nBlockIndex\n"
  64. "Block locations: array([0, 3]{0})\n"
  65. "Block lengths: array([1, 1]{0})".format(dtype))
  66. assert result == exp
  67. with option_context("display.max_rows", 3):
  68. result = repr(s)
  69. exp = ("0 True\n ... \n5 False\n"
  70. "Length: 6, dtype: Sparse[bool, False]\nBlockIndex\n"
  71. "Block locations: array([0, 3]{0})\n"
  72. "Block lengths: array([1, 1]{0})".format(dtype))
  73. assert result == exp
  74. def test_sparse_int(self):
  75. # GH 13110
  76. s = pd.SparseSeries([0, 1, 0, 0, 1, 0], fill_value=False)
  77. result = repr(s)
  78. dtype = '' if use_32bit_repr else ', dtype=int32'
  79. exp = ("0 0\n1 1\n2 0\n3 0\n4 1\n"
  80. "5 0\ndtype: Sparse[int64, False]\nBlockIndex\n"
  81. "Block locations: array([1, 4]{0})\n"
  82. "Block lengths: array([1, 1]{0})".format(dtype))
  83. assert result == exp
  84. with option_context("display.max_rows", 3,
  85. "display.show_dimensions", False):
  86. result = repr(s)
  87. exp = ("0 0\n ..\n5 0\n"
  88. "dtype: Sparse[int64, False]\nBlockIndex\n"
  89. "Block locations: array([1, 4]{0})\n"
  90. "Block lengths: array([1, 1]{0})".format(dtype))
  91. assert result == exp
  92. class TestSparseDataFrameFormatting(object):
  93. def test_sparse_frame(self):
  94. # GH 13110
  95. df = pd.DataFrame({'A': [True, False, True, False, True],
  96. 'B': [True, False, True, False, True],
  97. 'C': [0, 0, 3, 0, 5],
  98. 'D': [np.nan, np.nan, np.nan, 1, 2]})
  99. sparse = df.to_sparse()
  100. assert repr(sparse) == repr(df)
  101. with option_context("display.max_rows", 3):
  102. assert repr(sparse) == repr(df)
  103. def test_sparse_repr_after_set(self):
  104. # GH 15488
  105. sdf = pd.SparseDataFrame([[np.nan, 1], [2, np.nan]])
  106. res = sdf.copy()
  107. # Ignore the warning
  108. with pd.option_context('mode.chained_assignment', None):
  109. sdf[0][1] = 2 # This line triggers the bug
  110. repr(sdf)
  111. tm.assert_sp_frame_equal(sdf, res)