test_indexing.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import numpy as np
  2. import pytest
  3. from pandas import Series, SparseSeries
  4. from pandas.util import testing as tm
  5. pytestmark = pytest.mark.skip("Wrong SparseBlock initialization (GH 17386)")
  6. @pytest.mark.parametrize('data', [
  7. [1, 1, 2, 2, 3, 3, 4, 4, 0, 0],
  8. [1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0, np.nan, np.nan],
  9. [
  10. 1.0, 1.0 + 1.0j,
  11. 2.0 + 2.0j, 2.0,
  12. 3.0, 3.0 + 3.0j,
  13. 4.0 + 4.0j, 4.0,
  14. np.nan, np.nan
  15. ]
  16. ])
  17. @pytest.mark.xfail(reason='Wrong SparseBlock initialization (GH#17386)')
  18. def test_where_with_numeric_data(data):
  19. # GH 17386
  20. lower_bound = 1.5
  21. sparse = SparseSeries(data)
  22. result = sparse.where(sparse > lower_bound)
  23. dense = Series(data)
  24. dense_expected = dense.where(dense > lower_bound)
  25. sparse_expected = SparseSeries(dense_expected)
  26. tm.assert_series_equal(result, dense_expected)
  27. tm.assert_sp_series_equal(result, sparse_expected)
  28. @pytest.mark.parametrize('data', [
  29. [1, 1, 2, 2, 3, 3, 4, 4, 0, 0],
  30. [1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0, np.nan, np.nan],
  31. [
  32. 1.0, 1.0 + 1.0j,
  33. 2.0 + 2.0j, 2.0,
  34. 3.0, 3.0 + 3.0j,
  35. 4.0 + 4.0j, 4.0,
  36. np.nan, np.nan
  37. ]
  38. ])
  39. @pytest.mark.parametrize('other', [
  40. True,
  41. -100,
  42. 0.1,
  43. 100.0 + 100.0j
  44. ])
  45. @pytest.mark.skip(reason='Wrong SparseBlock initialization '
  46. '(Segfault) '
  47. '(GH 17386)')
  48. def test_where_with_numeric_data_and_other(data, other):
  49. # GH 17386
  50. lower_bound = 1.5
  51. sparse = SparseSeries(data)
  52. result = sparse.where(sparse > lower_bound, other)
  53. dense = Series(data)
  54. dense_expected = dense.where(dense > lower_bound, other)
  55. sparse_expected = SparseSeries(dense_expected, fill_value=other)
  56. tm.assert_series_equal(result, dense_expected)
  57. tm.assert_sp_series_equal(result, sparse_expected)
  58. @pytest.mark.xfail(reason='Wrong SparseBlock initialization (GH#17386)')
  59. def test_where_with_bool_data():
  60. # GH 17386
  61. data = [False, False, True, True, False, False]
  62. cond = True
  63. sparse = SparseSeries(data)
  64. result = sparse.where(sparse == cond)
  65. dense = Series(data)
  66. dense_expected = dense.where(dense == cond)
  67. sparse_expected = SparseSeries(dense_expected)
  68. tm.assert_series_equal(result, dense_expected)
  69. tm.assert_sp_series_equal(result, sparse_expected)
  70. @pytest.mark.parametrize('other', [
  71. True,
  72. 0,
  73. 0.1,
  74. 100.0 + 100.0j
  75. ])
  76. @pytest.mark.skip(reason='Wrong SparseBlock initialization '
  77. '(Segfault) '
  78. '(GH 17386)')
  79. def test_where_with_bool_data_and_other(other):
  80. # GH 17386
  81. data = [False, False, True, True, False, False]
  82. cond = True
  83. sparse = SparseSeries(data)
  84. result = sparse.where(sparse == cond, other)
  85. dense = Series(data)
  86. dense_expected = dense.where(dense == cond, other)
  87. sparse_expected = SparseSeries(dense_expected, fill_value=other)
  88. tm.assert_series_equal(result, dense_expected)
  89. tm.assert_sp_series_equal(result, sparse_expected)