test_internals.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import numpy as np
  2. import pytest
  3. import pandas.util._test_decorators as td
  4. import pandas as pd
  5. from pandas import DataFrame
  6. import pandas._testing as tm
  7. from pandas.tests.copy_view.util import get_array
  8. @td.skip_array_manager_invalid_test
  9. def test_consolidate(using_copy_on_write):
  10. # create unconsolidated DataFrame
  11. df = DataFrame({"a": [1, 2, 3], "b": [0.1, 0.2, 0.3]})
  12. df["c"] = [4, 5, 6]
  13. # take a viewing subset
  14. subset = df[:]
  15. # each block of subset references a block of df
  16. assert all(blk.refs.has_reference() for blk in subset._mgr.blocks)
  17. # consolidate the two int64 blocks
  18. subset._consolidate_inplace()
  19. # the float64 block still references the parent one because it still a view
  20. assert subset._mgr.blocks[0].refs.has_reference()
  21. # equivalent of assert np.shares_memory(df["b"].values, subset["b"].values)
  22. # but avoids caching df["b"]
  23. assert np.shares_memory(get_array(df, "b"), get_array(subset, "b"))
  24. # the new consolidated int64 block does not reference another
  25. assert not subset._mgr.blocks[1].refs.has_reference()
  26. # the parent dataframe now also only is linked for the float column
  27. assert not df._mgr.blocks[0].refs.has_reference()
  28. assert df._mgr.blocks[1].refs.has_reference()
  29. assert not df._mgr.blocks[2].refs.has_reference()
  30. # and modifying subset still doesn't modify parent
  31. if using_copy_on_write:
  32. subset.iloc[0, 1] = 0.0
  33. assert not df._mgr.blocks[1].refs.has_reference()
  34. assert df.loc[0, "b"] == 0.1
  35. @pytest.mark.single_cpu
  36. @td.skip_array_manager_invalid_test
  37. def test_switch_options():
  38. # ensure we can switch the value of the option within one session
  39. # (assuming data is constructed after switching)
  40. # using the option_context to ensure we set back to global option value
  41. # after running the test
  42. with pd.option_context("mode.copy_on_write", False):
  43. df = DataFrame({"a": [1, 2, 3], "b": [0.1, 0.2, 0.3]})
  44. subset = df[:]
  45. subset.iloc[0, 0] = 0
  46. # df updated with CoW disabled
  47. assert df.iloc[0, 0] == 0
  48. pd.options.mode.copy_on_write = True
  49. df = DataFrame({"a": [1, 2, 3], "b": [0.1, 0.2, 0.3]})
  50. subset = df[:]
  51. subset.iloc[0, 0] = 0
  52. # df not updated with CoW enabled
  53. assert df.iloc[0, 0] == 1
  54. pd.options.mode.copy_on_write = False
  55. df = DataFrame({"a": [1, 2, 3], "b": [0.1, 0.2, 0.3]})
  56. subset = df[:]
  57. subset.iloc[0, 0] = 0
  58. # df updated with CoW disabled
  59. assert df.iloc[0, 0] == 0
  60. @td.skip_array_manager_invalid_test
  61. @pytest.mark.parametrize("dtype", [np.intp, np.int8])
  62. @pytest.mark.parametrize(
  63. "locs, arr",
  64. [
  65. ([0], np.array([-1, -2, -3])),
  66. ([1], np.array([-1, -2, -3])),
  67. ([5], np.array([-1, -2, -3])),
  68. ([0, 1], np.array([[-1, -2, -3], [-4, -5, -6]]).T),
  69. ([0, 2], np.array([[-1, -2, -3], [-4, -5, -6]]).T),
  70. ([0, 1, 2], np.array([[-1, -2, -3], [-4, -5, -6], [-4, -5, -6]]).T),
  71. ([1, 2], np.array([[-1, -2, -3], [-4, -5, -6]]).T),
  72. ([1, 3], np.array([[-1, -2, -3], [-4, -5, -6]]).T),
  73. ([1, 3], np.array([[-1, -2, -3], [-4, -5, -6]]).T),
  74. ],
  75. )
  76. def test_iset_splits_blocks_inplace(using_copy_on_write, locs, arr, dtype):
  77. # Nothing currently calls iset with
  78. # more than 1 loc with inplace=True (only happens with inplace=False)
  79. # but ensure that it works
  80. df = DataFrame(
  81. {
  82. "a": [1, 2, 3],
  83. "b": [4, 5, 6],
  84. "c": [7, 8, 9],
  85. "d": [10, 11, 12],
  86. "e": [13, 14, 15],
  87. "f": ["a", "b", "c"],
  88. },
  89. )
  90. arr = arr.astype(dtype)
  91. df_orig = df.copy()
  92. df2 = df.copy(deep=None) # Trigger a CoW (if enabled, otherwise makes copy)
  93. df2._mgr.iset(locs, arr, inplace=True)
  94. tm.assert_frame_equal(df, df_orig)
  95. if using_copy_on_write:
  96. for i, col in enumerate(df.columns):
  97. if i not in locs:
  98. assert np.shares_memory(get_array(df, col), get_array(df2, col))
  99. else:
  100. for col in df.columns:
  101. assert not np.shares_memory(get_array(df, col), get_array(df2, col))