test_unary.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import operator
  2. import numpy as np
  3. import pytest
  4. import pandas as pd
  5. import pandas._testing as tm
  6. from pandas.core.arrays import SparseArray
  7. @pytest.mark.filterwarnings("ignore:invalid value encountered in cast:RuntimeWarning")
  8. @pytest.mark.parametrize("fill_value", [0, np.nan])
  9. @pytest.mark.parametrize("op", [operator.pos, operator.neg])
  10. def test_unary_op(op, fill_value):
  11. arr = np.array([0, 1, np.nan, 2])
  12. sparray = SparseArray(arr, fill_value=fill_value)
  13. result = op(sparray)
  14. expected = SparseArray(op(arr), fill_value=op(fill_value))
  15. tm.assert_sp_array_equal(result, expected)
  16. @pytest.mark.parametrize("fill_value", [True, False])
  17. def test_invert(fill_value):
  18. arr = np.array([True, False, False, True])
  19. sparray = SparseArray(arr, fill_value=fill_value)
  20. result = ~sparray
  21. expected = SparseArray(~arr, fill_value=not fill_value)
  22. tm.assert_sp_array_equal(result, expected)
  23. result = ~pd.Series(sparray)
  24. expected = pd.Series(expected)
  25. tm.assert_series_equal(result, expected)
  26. result = ~pd.DataFrame({"A": sparray})
  27. expected = pd.DataFrame({"A": expected})
  28. tm.assert_frame_equal(result, expected)
  29. class TestUnaryMethods:
  30. @pytest.mark.filterwarnings(
  31. "ignore:invalid value encountered in cast:RuntimeWarning"
  32. )
  33. def test_neg_operator(self):
  34. arr = SparseArray([-1, -2, np.nan, 3], fill_value=np.nan, dtype=np.int8)
  35. res = -arr
  36. exp = SparseArray([1, 2, np.nan, -3], fill_value=np.nan, dtype=np.int8)
  37. tm.assert_sp_array_equal(exp, res)
  38. arr = SparseArray([-1, -2, 1, 3], fill_value=-1, dtype=np.int8)
  39. res = -arr
  40. exp = SparseArray([1, 2, -1, -3], fill_value=1, dtype=np.int8)
  41. tm.assert_sp_array_equal(exp, res)
  42. @pytest.mark.filterwarnings(
  43. "ignore:invalid value encountered in cast:RuntimeWarning"
  44. )
  45. def test_abs_operator(self):
  46. arr = SparseArray([-1, -2, np.nan, 3], fill_value=np.nan, dtype=np.int8)
  47. res = abs(arr)
  48. exp = SparseArray([1, 2, np.nan, 3], fill_value=np.nan, dtype=np.int8)
  49. tm.assert_sp_array_equal(exp, res)
  50. arr = SparseArray([-1, -2, 1, 3], fill_value=-1, dtype=np.int8)
  51. res = abs(arr)
  52. exp = SparseArray([1, 2, 1, 3], fill_value=1, dtype=np.int8)
  53. tm.assert_sp_array_equal(exp, res)
  54. def test_invert_operator(self):
  55. arr = SparseArray([False, True, False, True], fill_value=False, dtype=np.bool_)
  56. exp = SparseArray(
  57. np.invert([False, True, False, True]), fill_value=True, dtype=np.bool_
  58. )
  59. res = ~arr
  60. tm.assert_sp_array_equal(exp, res)
  61. arr = SparseArray([0, 1, 0, 2, 3, 0], fill_value=0, dtype=np.int32)
  62. res = ~arr
  63. exp = SparseArray([-1, -2, -1, -3, -4, -1], fill_value=-1, dtype=np.int32)
  64. tm.assert_sp_array_equal(exp, res)