done
This commit is contained in:
		| @ -0,0 +1,86 @@ | ||||
| """ | ||||
| Tests for the pseudo-public API implemented in internals/api.py and exposed | ||||
| in core.internals | ||||
| """ | ||||
|  | ||||
| import pytest | ||||
|  | ||||
| import pandas as pd | ||||
| import pandas._testing as tm | ||||
| from pandas.core import internals | ||||
| from pandas.core.internals import api | ||||
|  | ||||
|  | ||||
| def test_internals_api(): | ||||
|     assert internals.make_block is api.make_block | ||||
|  | ||||
|  | ||||
| def test_namespace(): | ||||
|     # SUBJECT TO CHANGE | ||||
|  | ||||
|     modules = [ | ||||
|         "blocks", | ||||
|         "concat", | ||||
|         "managers", | ||||
|         "construction", | ||||
|         "array_manager", | ||||
|         "base", | ||||
|         "api", | ||||
|         "ops", | ||||
|     ] | ||||
|     expected = [ | ||||
|         "make_block", | ||||
|         "DataManager", | ||||
|         "ArrayManager", | ||||
|         "BlockManager", | ||||
|         "SingleDataManager", | ||||
|         "SingleBlockManager", | ||||
|         "SingleArrayManager", | ||||
|         "concatenate_managers", | ||||
|     ] | ||||
|  | ||||
|     result = [x for x in dir(internals) if not x.startswith("__")] | ||||
|     assert set(result) == set(expected + modules) | ||||
|  | ||||
|  | ||||
| @pytest.mark.parametrize( | ||||
|     "name", | ||||
|     [ | ||||
|         "NumericBlock", | ||||
|         "ObjectBlock", | ||||
|         "Block", | ||||
|         "ExtensionBlock", | ||||
|         "DatetimeTZBlock", | ||||
|     ], | ||||
| ) | ||||
| def test_deprecations(name): | ||||
|     # GH#55139 | ||||
|     msg = f"{name} is deprecated.* Use public APIs instead" | ||||
|     with tm.assert_produces_warning(DeprecationWarning, match=msg): | ||||
|         getattr(internals, name) | ||||
|  | ||||
|     if name not in ["NumericBlock", "ObjectBlock"]: | ||||
|         # NumericBlock and ObjectBlock are not in the internals.api namespace | ||||
|         with tm.assert_produces_warning(DeprecationWarning, match=msg): | ||||
|             getattr(api, name) | ||||
|  | ||||
|  | ||||
| def test_make_block_2d_with_dti(): | ||||
|     # GH#41168 | ||||
|     dti = pd.date_range("2012", periods=3, tz="UTC") | ||||
|     blk = api.make_block(dti, placement=[0]) | ||||
|  | ||||
|     assert blk.shape == (1, 3) | ||||
|     assert blk.values.shape == (1, 3) | ||||
|  | ||||
|  | ||||
| def test_create_block_manager_from_blocks_deprecated(): | ||||
|     # GH#33892 | ||||
|     # If they must, downstream packages should get this from internals.api, | ||||
|     #  not internals. | ||||
|     msg = ( | ||||
|         "create_block_manager_from_blocks is deprecated and will be " | ||||
|         "removed in a future version. Use public APIs instead" | ||||
|     ) | ||||
|     with tm.assert_produces_warning(DeprecationWarning, match=msg): | ||||
|         internals.create_block_manager_from_blocks | ||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @ -0,0 +1,103 @@ | ||||
| """ | ||||
| Testing interaction between the different managers (BlockManager, ArrayManager) | ||||
| """ | ||||
| import os | ||||
| import subprocess | ||||
| import sys | ||||
|  | ||||
| import pytest | ||||
|  | ||||
| from pandas.core.dtypes.missing import array_equivalent | ||||
|  | ||||
| import pandas as pd | ||||
| import pandas._testing as tm | ||||
| from pandas.core.internals import ( | ||||
|     ArrayManager, | ||||
|     BlockManager, | ||||
|     SingleArrayManager, | ||||
|     SingleBlockManager, | ||||
| ) | ||||
|  | ||||
|  | ||||
| def test_dataframe_creation(): | ||||
|     msg = "data_manager option is deprecated" | ||||
|     with tm.assert_produces_warning(FutureWarning, match=msg): | ||||
|         with pd.option_context("mode.data_manager", "block"): | ||||
|             df_block = pd.DataFrame( | ||||
|                 {"a": [1, 2, 3], "b": [0.1, 0.2, 0.3], "c": [4, 5, 6]} | ||||
|             ) | ||||
|     assert isinstance(df_block._mgr, BlockManager) | ||||
|  | ||||
|     with tm.assert_produces_warning(FutureWarning, match=msg): | ||||
|         with pd.option_context("mode.data_manager", "array"): | ||||
|             df_array = pd.DataFrame( | ||||
|                 {"a": [1, 2, 3], "b": [0.1, 0.2, 0.3], "c": [4, 5, 6]} | ||||
|             ) | ||||
|     assert isinstance(df_array._mgr, ArrayManager) | ||||
|  | ||||
|     # also ensure both are seen as equal | ||||
|     tm.assert_frame_equal(df_block, df_array) | ||||
|  | ||||
|     # conversion from one manager to the other | ||||
|     result = df_block._as_manager("block") | ||||
|     assert isinstance(result._mgr, BlockManager) | ||||
|     result = df_block._as_manager("array") | ||||
|     assert isinstance(result._mgr, ArrayManager) | ||||
|     tm.assert_frame_equal(result, df_block) | ||||
|     assert all( | ||||
|         array_equivalent(left, right) | ||||
|         for left, right in zip(result._mgr.arrays, df_array._mgr.arrays) | ||||
|     ) | ||||
|  | ||||
|     result = df_array._as_manager("array") | ||||
|     assert isinstance(result._mgr, ArrayManager) | ||||
|     result = df_array._as_manager("block") | ||||
|     assert isinstance(result._mgr, BlockManager) | ||||
|     tm.assert_frame_equal(result, df_array) | ||||
|     assert len(result._mgr.blocks) == 2 | ||||
|  | ||||
|  | ||||
| def test_series_creation(): | ||||
|     msg = "data_manager option is deprecated" | ||||
|     with tm.assert_produces_warning(FutureWarning, match=msg): | ||||
|         with pd.option_context("mode.data_manager", "block"): | ||||
|             s_block = pd.Series([1, 2, 3], name="A", index=["a", "b", "c"]) | ||||
|     assert isinstance(s_block._mgr, SingleBlockManager) | ||||
|  | ||||
|     with tm.assert_produces_warning(FutureWarning, match=msg): | ||||
|         with pd.option_context("mode.data_manager", "array"): | ||||
|             s_array = pd.Series([1, 2, 3], name="A", index=["a", "b", "c"]) | ||||
|     assert isinstance(s_array._mgr, SingleArrayManager) | ||||
|  | ||||
|     # also ensure both are seen as equal | ||||
|     tm.assert_series_equal(s_block, s_array) | ||||
|  | ||||
|     # conversion from one manager to the other | ||||
|     result = s_block._as_manager("block") | ||||
|     assert isinstance(result._mgr, SingleBlockManager) | ||||
|     result = s_block._as_manager("array") | ||||
|     assert isinstance(result._mgr, SingleArrayManager) | ||||
|     tm.assert_series_equal(result, s_block) | ||||
|  | ||||
|     result = s_array._as_manager("array") | ||||
|     assert isinstance(result._mgr, SingleArrayManager) | ||||
|     result = s_array._as_manager("block") | ||||
|     assert isinstance(result._mgr, SingleBlockManager) | ||||
|     tm.assert_series_equal(result, s_array) | ||||
|  | ||||
|  | ||||
| @pytest.mark.single_cpu | ||||
| @pytest.mark.parametrize("manager", ["block", "array"]) | ||||
| def test_array_manager_depr_env_var(manager): | ||||
|     # GH#55043 | ||||
|     test_env = os.environ.copy() | ||||
|     test_env["PANDAS_DATA_MANAGER"] = manager | ||||
|     response = subprocess.run( | ||||
|         [sys.executable, "-c", "import pandas"], | ||||
|         capture_output=True, | ||||
|         env=test_env, | ||||
|         check=True, | ||||
|     ) | ||||
|     msg = "FutureWarning: The env variable PANDAS_DATA_MANAGER is set" | ||||
|     stderr_msg = response.stderr.decode("utf-8") | ||||
|     assert msg in stderr_msg, stderr_msg | ||||
		Reference in New Issue
	
	Block a user