From ddcfa7b78919b54f36b5f1d2d4cc658874c361e5 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 27 Mar 2024 15:08:24 -0700 Subject: [PATCH 1/2] DEPR: remove deprecated internals names --- doc/source/whatsnew/v3.0.0.rst | 1 + pandas/core/internals/__init__.py | 50 ------------------------ pandas/core/internals/api.py | 61 +----------------------------- pandas/core/internals/blocks.py | 10 +---- pandas/tests/internals/test_api.py | 30 --------------- 5 files changed, 3 insertions(+), 149 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 2286a75f5d0c5..8ed3e6adda4d2 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -207,6 +207,7 @@ Removal of prior version deprecations/changes - All arguments except ``name`` in :meth:`Index.rename` are now keyword only (:issue:`56493`) - All arguments except the first ``path``-like argument in IO writers are now keyword only (:issue:`54229`) - Removed "freq" keyword from :class:`PeriodArray` constructor, use "dtype" instead (:issue:`52462`) +- Removed ``Block``, ``DatetimeTZBlock``, ``ExtensionBlock``, ``create_block_manager_from_blocks`` from ``pandas.core.internals`` and ``pandas.core.internals.api`` (:issue:`55139`) - Removed the "closed" and "normalize" keywords in :meth:`DatetimeIndex.__new__` (:issue:`52628`) - Removed the "closed" and "unit" keywords in :meth:`TimedeltaIndex.__new__` (:issue:`52628`, :issue:`55499`) - All arguments in :meth:`Index.sort_values` are now keyword only (:issue:`56493`) diff --git a/pandas/core/internals/__init__.py b/pandas/core/internals/__init__.py index 31234fb1f116f..45758379e0bd6 100644 --- a/pandas/core/internals/__init__.py +++ b/pandas/core/internals/__init__.py @@ -6,58 +6,8 @@ ) __all__ = [ - "Block", # pylint: disable=undefined-all-variable - "DatetimeTZBlock", # pylint: disable=undefined-all-variable - "ExtensionBlock", # pylint: disable=undefined-all-variable "make_block", "BlockManager", "SingleBlockManager", "concatenate_managers", ] - - -def __getattr__(name: str): - # GH#55139 - import warnings - - if name == "create_block_manager_from_blocks": - # GH#33892 - warnings.warn( - f"{name} is deprecated and will be removed in a future version. " - "Use public APIs instead.", - DeprecationWarning, - # https://github.com/pandas-dev/pandas/pull/55139#pullrequestreview-1720690758 - # on hard-coding stacklevel - stacklevel=2, - ) - from pandas.core.internals.managers import create_block_manager_from_blocks - - return create_block_manager_from_blocks - - if name in [ - "Block", - "ExtensionBlock", - "DatetimeTZBlock", - ]: - warnings.warn( - f"{name} is deprecated and will be removed in a future version. " - "Use public APIs instead.", - DeprecationWarning, - # https://github.com/pandas-dev/pandas/pull/55139#pullrequestreview-1720690758 - # on hard-coding stacklevel - stacklevel=2, - ) - if name == "DatetimeTZBlock": - from pandas.core.internals.blocks import DatetimeTZBlock - - return DatetimeTZBlock - elif name == "ExtensionBlock": - from pandas.core.internals.blocks import ExtensionBlock - - return ExtensionBlock - else: - from pandas.core.internals.blocks import Block - - return Block - - raise AttributeError(f"module 'pandas.core.internals' has no attribute '{name}'") diff --git a/pandas/core/internals/api.py b/pandas/core/internals/api.py index d6e1e8b38dfe3..14695d5d40ea7 100644 --- a/pandas/core/internals/api.py +++ b/pandas/core/internals/api.py @@ -21,7 +21,6 @@ PeriodDtype, ) -from pandas.core.arrays import DatetimeArray from pandas.core.construction import extract_array from pandas.core.internals.blocks import ( check_ndim, @@ -56,10 +55,7 @@ def make_block( values, dtype = extract_pandas_array(values, dtype, ndim) - from pandas.core.internals.blocks import ( - DatetimeTZBlock, - ExtensionBlock, - ) + from pandas.core.internals.blocks import ExtensionBlock if klass is ExtensionBlock and isinstance(values.dtype, PeriodDtype): # GH-44681 changed PeriodArray to be stored in the 2D @@ -71,16 +67,6 @@ def make_block( dtype = dtype or values.dtype klass = get_block_type(dtype) - elif klass is DatetimeTZBlock and not isinstance(values.dtype, DatetimeTZDtype): - # pyarrow calls get here - values = DatetimeArray._simple_new( - # error: Argument "dtype" to "_simple_new" of "DatetimeArray" has - # incompatible type "Union[ExtensionDtype, dtype[Any], None]"; - # expected "Union[dtype[datetime64], DatetimeTZDtype]" - values, - dtype=dtype, # type: ignore[arg-type] - ) - if not isinstance(placement, BlockPlacement): placement = BlockPlacement(placement) @@ -110,48 +96,3 @@ def maybe_infer_ndim(values, placement: BlockPlacement, ndim: int | None) -> int else: ndim = values.ndim return ndim - - -def __getattr__(name: str): - # GH#55139 - import warnings - - if name in [ - "Block", - "ExtensionBlock", - "DatetimeTZBlock", - "create_block_manager_from_blocks", - ]: - # GH#33892 - warnings.warn( - f"{name} is deprecated and will be removed in a future version. " - "Use public APIs instead.", - DeprecationWarning, - # https://github.com/pandas-dev/pandas/pull/55139#pullrequestreview-1720690758 - # on hard-coding stacklevel - stacklevel=2, - ) - - if name == "create_block_manager_from_blocks": - from pandas.core.internals.managers import create_block_manager_from_blocks - - return create_block_manager_from_blocks - - elif name == "Block": - from pandas.core.internals.blocks import Block - - return Block - - elif name == "DatetimeTZBlock": - from pandas.core.internals.blocks import DatetimeTZBlock - - return DatetimeTZBlock - - elif name == "ExtensionBlock": - from pandas.core.internals.blocks import ExtensionBlock - - return ExtensionBlock - - raise AttributeError( - f"module 'pandas.core.internals.api' has no attribute '{name}'" - ) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index a7cdc7c39754d..d9a5f555fb85d 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -2149,14 +2149,6 @@ class DatetimeLikeBlock(NDArrayBackedExtensionBlock): values: DatetimeArray | TimedeltaArray -class DatetimeTZBlock(DatetimeLikeBlock): - """implement a datetime64 block with a tz attribute""" - - values: DatetimeArray - - __slots__ = () - - # ----------------------------------------------------------------- # Constructor Helpers @@ -2203,7 +2195,7 @@ def get_block_type(dtype: DtypeObj) -> type[Block]: cls : class, subclass of Block """ if isinstance(dtype, DatetimeTZDtype): - return DatetimeTZBlock + return DatetimeLikeBlock elif isinstance(dtype, PeriodDtype): return NDArrayBackedExtensionBlock elif isinstance(dtype, ExtensionDtype): diff --git a/pandas/tests/internals/test_api.py b/pandas/tests/internals/test_api.py index 5bff1b7be3080..80431a8748860 100644 --- a/pandas/tests/internals/test_api.py +++ b/pandas/tests/internals/test_api.py @@ -3,10 +3,7 @@ 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 @@ -37,21 +34,6 @@ def test_namespace(): assert set(result) == set(expected + modules) -@pytest.mark.parametrize( - "name", - [ - "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) - - def test_make_block_2d_with_dti(): # GH#41168 dti = pd.date_range("2012", periods=3, tz="UTC") @@ -59,15 +41,3 @@ def test_make_block_2d_with_dti(): 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 From 6d0be83d65b25bffa95f816a6d23d413d37b73f6 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 27 Mar 2024 21:01:33 -0700 Subject: [PATCH 2/2] mypy fixup --- pandas/io/pytables.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 5ecf7e287ea58..daae86ae4f1fb 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -124,7 +124,7 @@ npt, ) - from pandas.core.internals import Block + from pandas.core.internals.blocks import Block # versioning attribute _version = "0.15.2"