test_clip.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import numpy as np
  2. from pandas import DataFrame
  3. import pandas._testing as tm
  4. from pandas.tests.copy_view.util import get_array
  5. def test_clip_inplace_reference(using_copy_on_write):
  6. df = DataFrame({"a": [1.5, 2, 3]})
  7. df_copy = df.copy()
  8. arr_a = get_array(df, "a")
  9. view = df[:]
  10. df.clip(lower=2, inplace=True)
  11. # Clip not actually inplace right now but could be
  12. assert not np.shares_memory(get_array(df, "a"), arr_a)
  13. if using_copy_on_write:
  14. assert df._mgr._has_no_reference(0)
  15. assert view._mgr._has_no_reference(0)
  16. tm.assert_frame_equal(df_copy, view)
  17. def test_clip_inplace_reference_no_op(using_copy_on_write):
  18. df = DataFrame({"a": [1.5, 2, 3]})
  19. df_copy = df.copy()
  20. arr_a = get_array(df, "a")
  21. view = df[:]
  22. df.clip(lower=0, inplace=True)
  23. if using_copy_on_write:
  24. assert np.shares_memory(get_array(df, "a"), arr_a)
  25. assert not df._mgr._has_no_reference(0)
  26. assert not view._mgr._has_no_reference(0)
  27. tm.assert_frame_equal(df_copy, view)
  28. else:
  29. assert not np.shares_memory(get_array(df, "a"), arr_a)
  30. def test_clip_inplace(using_copy_on_write):
  31. df = DataFrame({"a": [1.5, 2, 3]})
  32. arr_a = get_array(df, "a")
  33. df.clip(lower=2, inplace=True)
  34. # Clip not actually inplace right now but could be
  35. assert not np.shares_memory(get_array(df, "a"), arr_a)
  36. if using_copy_on_write:
  37. assert df._mgr._has_no_reference(0)
  38. def test_clip(using_copy_on_write):
  39. df = DataFrame({"a": [1.5, 2, 3]})
  40. df_orig = df.copy()
  41. df2 = df.clip(lower=2)
  42. assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
  43. if using_copy_on_write:
  44. assert df._mgr._has_no_reference(0)
  45. tm.assert_frame_equal(df_orig, df)
  46. def test_clip_no_op(using_copy_on_write):
  47. df = DataFrame({"a": [1.5, 2, 3]})
  48. df2 = df.clip(lower=0)
  49. if using_copy_on_write:
  50. assert not df._mgr._has_no_reference(0)
  51. assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
  52. else:
  53. assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))