test_clip.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import numpy as np
  2. from pandas.compat import WARNING_CHECK_DISABLED
  3. from pandas import (
  4. DataFrame,
  5. option_context,
  6. )
  7. import pandas._testing as tm
  8. from pandas.tests.copy_view.util import get_array
  9. def test_clip_inplace_reference(using_copy_on_write, warn_copy_on_write):
  10. df = DataFrame({"a": [1.5, 2, 3]})
  11. df_copy = df.copy()
  12. arr_a = get_array(df, "a")
  13. view = df[:]
  14. if warn_copy_on_write:
  15. with tm.assert_cow_warning():
  16. df.clip(lower=2, inplace=True)
  17. else:
  18. df.clip(lower=2, inplace=True)
  19. if using_copy_on_write:
  20. assert not np.shares_memory(get_array(df, "a"), arr_a)
  21. assert df._mgr._has_no_reference(0)
  22. assert view._mgr._has_no_reference(0)
  23. tm.assert_frame_equal(df_copy, view)
  24. else:
  25. assert np.shares_memory(get_array(df, "a"), arr_a)
  26. def test_clip_inplace_reference_no_op(using_copy_on_write):
  27. df = DataFrame({"a": [1.5, 2, 3]})
  28. df_copy = df.copy()
  29. arr_a = get_array(df, "a")
  30. view = df[:]
  31. df.clip(lower=0, inplace=True)
  32. assert np.shares_memory(get_array(df, "a"), arr_a)
  33. if using_copy_on_write:
  34. assert not df._mgr._has_no_reference(0)
  35. assert not view._mgr._has_no_reference(0)
  36. tm.assert_frame_equal(df_copy, view)
  37. def test_clip_inplace(using_copy_on_write):
  38. df = DataFrame({"a": [1.5, 2, 3]})
  39. arr_a = get_array(df, "a")
  40. df.clip(lower=2, inplace=True)
  41. assert np.shares_memory(get_array(df, "a"), arr_a)
  42. if using_copy_on_write:
  43. assert df._mgr._has_no_reference(0)
  44. def test_clip(using_copy_on_write):
  45. df = DataFrame({"a": [1.5, 2, 3]})
  46. df_orig = df.copy()
  47. df2 = df.clip(lower=2)
  48. assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
  49. if using_copy_on_write:
  50. assert df._mgr._has_no_reference(0)
  51. tm.assert_frame_equal(df_orig, df)
  52. def test_clip_no_op(using_copy_on_write):
  53. df = DataFrame({"a": [1.5, 2, 3]})
  54. df2 = df.clip(lower=0)
  55. if using_copy_on_write:
  56. assert not df._mgr._has_no_reference(0)
  57. assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
  58. else:
  59. assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
  60. def test_clip_chained_inplace(using_copy_on_write):
  61. df = DataFrame({"a": [1, 4, 2], "b": 1})
  62. df_orig = df.copy()
  63. if using_copy_on_write:
  64. with tm.raises_chained_assignment_error():
  65. df["a"].clip(1, 2, inplace=True)
  66. tm.assert_frame_equal(df, df_orig)
  67. with tm.raises_chained_assignment_error():
  68. df[["a"]].clip(1, 2, inplace=True)
  69. tm.assert_frame_equal(df, df_orig)
  70. else:
  71. with tm.assert_produces_warning(
  72. FutureWarning if not WARNING_CHECK_DISABLED else None,
  73. match="inplace method",
  74. ):
  75. df["a"].clip(1, 2, inplace=True)
  76. with tm.assert_produces_warning(None):
  77. with option_context("mode.chained_assignment", None):
  78. df[["a"]].clip(1, 2, inplace=True)
  79. with tm.assert_produces_warning(None):
  80. with option_context("mode.chained_assignment", None):
  81. df[df["a"] > 1].clip(1, 2, inplace=True)