test_managers.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """
  2. Testing interaction between the different managers (BlockManager, ArrayManager)
  3. """
  4. import os
  5. import subprocess
  6. import sys
  7. import pytest
  8. from pandas.core.dtypes.missing import array_equivalent
  9. import pandas as pd
  10. import pandas._testing as tm
  11. from pandas.core.internals import (
  12. ArrayManager,
  13. BlockManager,
  14. SingleArrayManager,
  15. SingleBlockManager,
  16. )
  17. def test_dataframe_creation():
  18. msg = "data_manager option is deprecated"
  19. with tm.assert_produces_warning(FutureWarning, match=msg):
  20. with pd.option_context("mode.data_manager", "block"):
  21. df_block = pd.DataFrame(
  22. {"a": [1, 2, 3], "b": [0.1, 0.2, 0.3], "c": [4, 5, 6]}
  23. )
  24. assert isinstance(df_block._mgr, BlockManager)
  25. with tm.assert_produces_warning(FutureWarning, match=msg):
  26. with pd.option_context("mode.data_manager", "array"):
  27. df_array = pd.DataFrame(
  28. {"a": [1, 2, 3], "b": [0.1, 0.2, 0.3], "c": [4, 5, 6]}
  29. )
  30. assert isinstance(df_array._mgr, ArrayManager)
  31. # also ensure both are seen as equal
  32. tm.assert_frame_equal(df_block, df_array)
  33. # conversion from one manager to the other
  34. result = df_block._as_manager("block")
  35. assert isinstance(result._mgr, BlockManager)
  36. result = df_block._as_manager("array")
  37. assert isinstance(result._mgr, ArrayManager)
  38. tm.assert_frame_equal(result, df_block)
  39. assert all(
  40. array_equivalent(left, right)
  41. for left, right in zip(result._mgr.arrays, df_array._mgr.arrays)
  42. )
  43. result = df_array._as_manager("array")
  44. assert isinstance(result._mgr, ArrayManager)
  45. result = df_array._as_manager("block")
  46. assert isinstance(result._mgr, BlockManager)
  47. tm.assert_frame_equal(result, df_array)
  48. assert len(result._mgr.blocks) == 2
  49. def test_series_creation():
  50. msg = "data_manager option is deprecated"
  51. with tm.assert_produces_warning(FutureWarning, match=msg):
  52. with pd.option_context("mode.data_manager", "block"):
  53. s_block = pd.Series([1, 2, 3], name="A", index=["a", "b", "c"])
  54. assert isinstance(s_block._mgr, SingleBlockManager)
  55. with tm.assert_produces_warning(FutureWarning, match=msg):
  56. with pd.option_context("mode.data_manager", "array"):
  57. s_array = pd.Series([1, 2, 3], name="A", index=["a", "b", "c"])
  58. assert isinstance(s_array._mgr, SingleArrayManager)
  59. # also ensure both are seen as equal
  60. tm.assert_series_equal(s_block, s_array)
  61. # conversion from one manager to the other
  62. result = s_block._as_manager("block")
  63. assert isinstance(result._mgr, SingleBlockManager)
  64. result = s_block._as_manager("array")
  65. assert isinstance(result._mgr, SingleArrayManager)
  66. tm.assert_series_equal(result, s_block)
  67. result = s_array._as_manager("array")
  68. assert isinstance(result._mgr, SingleArrayManager)
  69. result = s_array._as_manager("block")
  70. assert isinstance(result._mgr, SingleBlockManager)
  71. tm.assert_series_equal(result, s_array)
  72. @pytest.mark.single_cpu
  73. @pytest.mark.parametrize("manager", ["block", "array"])
  74. def test_array_manager_depr_env_var(manager):
  75. # GH#55043
  76. test_env = os.environ.copy()
  77. test_env["PANDAS_DATA_MANAGER"] = manager
  78. response = subprocess.run(
  79. [sys.executable, "-c", "import pandas"],
  80. capture_output=True,
  81. env=test_env,
  82. check=True,
  83. )
  84. msg = "FutureWarning: The env variable PANDAS_DATA_MANAGER is set"
  85. stderr_msg = response.stderr.decode("utf-8")
  86. assert msg in stderr_msg, stderr_msg