From 234f678daff9e0e76e9d3c1994898cf19d04e609 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Tue, 14 May 2024 16:35:22 +0200 Subject: [PATCH 1/4] revert removing exposing blocks core.internals and replace DeprecationWarning with FutureWarning --- doc/source/whatsnew/v3.0.0.rst | 1 - pandas/core/internals/__init__.py | 50 ++++++++++++++++++++++++++++++ pandas/io/pytables.py | 3 +- pandas/tests/internals/test_api.py | 27 ++++++++++++++++ pandas/tests/io/test_parquet.py | 4 --- 5 files changed, 78 insertions(+), 7 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 083e004fb94fa..066080df6b825 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -259,7 +259,6 @@ Removal of prior version deprecations/changes - Enforced deprecation of :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` for object-dtype (:issue:`57820`) - Enforced deprecation of :meth:`offsets.Tick.delta`, use ``pd.Timedelta(obj)`` instead (:issue:`55498`) - Enforced deprecation of ``axis=None`` acting the same as ``axis=0`` in the DataFrame reductions ``sum``, ``prod``, ``std``, ``var``, and ``sem``, passing ``axis=None`` will now reduce over both axes; this is particularly the case when doing e.g. ``numpy.sum(df)`` (:issue:`21597`) -- Enforced deprecation of ``core.internals`` members ``Block``, ``ExtensionBlock``, and ``DatetimeTZBlock`` (:issue:`58467`) - Enforced deprecation of ``date_parser`` in :func:`read_csv`, :func:`read_table`, :func:`read_fwf`, and :func:`read_excel` in favour of ``date_format`` (:issue:`50601`) - Enforced deprecation of ``keep_date_col`` keyword in :func:`read_csv` (:issue:`55569`) - Enforced deprecation of ``quantile`` keyword in :meth:`.Rolling.quantile` and :meth:`.Expanding.quantile`, renamed to ``q`` instead. (:issue:`52550`) diff --git a/pandas/core/internals/__init__.py b/pandas/core/internals/__init__.py index 45758379e0bd6..cd5056b0718ef 100644 --- a/pandas/core/internals/__init__.py +++ b/pandas/core/internals/__init__.py @@ -6,8 +6,58 @@ ) __all__ = [ + "Block", + "DatetimeTZBlock", + "ExtensionBlock", "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.", + FutureWarning, + # 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.", + FutureWarning, + # 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/io/pytables.py b/pandas/io/pytables.py index 4fce338ccad6f..5d325397a81ae 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -125,8 +125,7 @@ npt, ) - from pandas.core.internals.blocks import Block - + from pandas.core.internals import Block # versioning attribute _version = "0.15.2" diff --git a/pandas/tests/internals/test_api.py b/pandas/tests/internals/test_api.py index c189d5248b1f3..9587b94f5a7d5 100644 --- a/pandas/tests/internals/test_api.py +++ b/pandas/tests/internals/test_api.py @@ -41,6 +41,21 @@ 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(FutureWarning, match=msg): + getattr(internals, name) + + def test_make_block_2d_with_dti(): # GH#41168 dti = pd.date_range("2012", periods=3, tz="UTC") @@ -50,6 +65,18 @@ def test_make_block_2d_with_dti(): 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(FutureWarning, match=msg): + internals.create_block_manager_from_blocks + + def test_create_dataframe_from_blocks(float_frame): block = float_frame._mgr.blocks[0] index = float_frame.index.copy() diff --git a/pandas/tests/io/test_parquet.py b/pandas/tests/io/test_parquet.py index 2860b3a6483af..55be48eb572fd 100644 --- a/pandas/tests/io/test_parquet.py +++ b/pandas/tests/io/test_parquet.py @@ -655,7 +655,6 @@ def test_read_empty_array(self, pa, dtype): "value": pd.array([], dtype=dtype), } ) - pytest.importorskip("pyarrow", "11.0.0") # GH 45694 expected = None if dtype == "float": @@ -672,7 +671,6 @@ def test_read_empty_array(self, pa, dtype): class TestParquetPyArrow(Base): def test_basic(self, pa, df_full): df = df_full - pytest.importorskip("pyarrow", "11.0.0") # additional supported types for pyarrow dti = pd.date_range("20130101", periods=3, tz="Europe/Brussels") @@ -940,8 +938,6 @@ def test_timestamp_nanoseconds(self, pa): check_round_trip(df, pa, write_kwargs={"version": ver}) def test_timezone_aware_index(self, request, pa, timezone_aware_date_list): - pytest.importorskip("pyarrow", "11.0.0") - if timezone_aware_date_list.tzinfo != datetime.timezone.utc: request.applymarker( pytest.mark.xfail( From 547a81a3d662bf3c5747d47b46b60bc59483c21f Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Wed, 15 May 2024 01:01:32 +0200 Subject: [PATCH 2/4] core.internals member ExtensionBlock --- doc/source/whatsnew/v3.0.0.rst | 1 + pandas/core/internals/__init__.py | 8 +------- pandas/tests/internals/test_api.py | 1 - 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 066080df6b825..09609254b57be 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -259,6 +259,7 @@ Removal of prior version deprecations/changes - Enforced deprecation of :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` for object-dtype (:issue:`57820`) - Enforced deprecation of :meth:`offsets.Tick.delta`, use ``pd.Timedelta(obj)`` instead (:issue:`55498`) - Enforced deprecation of ``axis=None`` acting the same as ``axis=0`` in the DataFrame reductions ``sum``, ``prod``, ``std``, ``var``, and ``sem``, passing ``axis=None`` will now reduce over both axes; this is particularly the case when doing e.g. ``numpy.sum(df)`` (:issue:`21597`) +- Enforced deprecation of ``core.internals`` member ``ExtensionBlock`` (:issue:`58467`) - Enforced deprecation of ``date_parser`` in :func:`read_csv`, :func:`read_table`, :func:`read_fwf`, and :func:`read_excel` in favour of ``date_format`` (:issue:`50601`) - Enforced deprecation of ``keep_date_col`` keyword in :func:`read_csv` (:issue:`55569`) - Enforced deprecation of ``quantile`` keyword in :meth:`.Rolling.quantile` and :meth:`.Expanding.quantile`, renamed to ``q`` instead. (:issue:`52550`) diff --git a/pandas/core/internals/__init__.py b/pandas/core/internals/__init__.py index cd5056b0718ef..5ab70ba38f9c2 100644 --- a/pandas/core/internals/__init__.py +++ b/pandas/core/internals/__init__.py @@ -7,7 +7,6 @@ __all__ = [ "Block", - "DatetimeTZBlock", "ExtensionBlock", "make_block", "BlockManager", @@ -37,7 +36,6 @@ def __getattr__(name: str): if name in [ "Block", "ExtensionBlock", - "DatetimeTZBlock", ]: warnings.warn( f"{name} is deprecated and will be removed in a future version. " @@ -47,11 +45,7 @@ def __getattr__(name: str): # on hard-coding stacklevel stacklevel=2, ) - if name == "DatetimeTZBlock": - from pandas.core.internals.blocks import DatetimeTZBlock - - return DatetimeTZBlock - elif name == "ExtensionBlock": + if name == "ExtensionBlock": from pandas.core.internals.blocks import ExtensionBlock return ExtensionBlock diff --git a/pandas/tests/internals/test_api.py b/pandas/tests/internals/test_api.py index 9587b94f5a7d5..4dca05d5e7626 100644 --- a/pandas/tests/internals/test_api.py +++ b/pandas/tests/internals/test_api.py @@ -46,7 +46,6 @@ def test_namespace(): [ "Block", "ExtensionBlock", - "DatetimeTZBlock", ], ) def test_deprecations(name): From 9b986ccc43e36f1db628a5180e86d2109a535e3c Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Wed, 15 May 2024 01:09:17 +0200 Subject: [PATCH 3/4] in v3.0.0 replace ExtensionBlock with DatetimeTZBlock --- doc/source/whatsnew/v3.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 09609254b57be..3aa4540c7c4ff 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -259,7 +259,7 @@ Removal of prior version deprecations/changes - Enforced deprecation of :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` for object-dtype (:issue:`57820`) - Enforced deprecation of :meth:`offsets.Tick.delta`, use ``pd.Timedelta(obj)`` instead (:issue:`55498`) - Enforced deprecation of ``axis=None`` acting the same as ``axis=0`` in the DataFrame reductions ``sum``, ``prod``, ``std``, ``var``, and ``sem``, passing ``axis=None`` will now reduce over both axes; this is particularly the case when doing e.g. ``numpy.sum(df)`` (:issue:`21597`) -- Enforced deprecation of ``core.internals`` member ``ExtensionBlock`` (:issue:`58467`) +- Enforced deprecation of ``core.internals`` member ``DatetimeTZBlock`` (:issue:`58467`) - Enforced deprecation of ``date_parser`` in :func:`read_csv`, :func:`read_table`, :func:`read_fwf`, and :func:`read_excel` in favour of ``date_format`` (:issue:`50601`) - Enforced deprecation of ``keep_date_col`` keyword in :func:`read_csv` (:issue:`55569`) - Enforced deprecation of ``quantile`` keyword in :meth:`.Rolling.quantile` and :meth:`.Expanding.quantile`, renamed to ``q`` instead. (:issue:`52550`) From 20ca40a7d35eba7a173d0662d37b16616802b0f7 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Wed, 15 May 2024 12:39:26 +0200 Subject: [PATCH 4/4] add importorskip(pyarrow, 11.0.0) to test_parquet.py --- pandas/tests/io/test_parquet.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/tests/io/test_parquet.py b/pandas/tests/io/test_parquet.py index 55be48eb572fd..2860b3a6483af 100644 --- a/pandas/tests/io/test_parquet.py +++ b/pandas/tests/io/test_parquet.py @@ -655,6 +655,7 @@ def test_read_empty_array(self, pa, dtype): "value": pd.array([], dtype=dtype), } ) + pytest.importorskip("pyarrow", "11.0.0") # GH 45694 expected = None if dtype == "float": @@ -671,6 +672,7 @@ def test_read_empty_array(self, pa, dtype): class TestParquetPyArrow(Base): def test_basic(self, pa, df_full): df = df_full + pytest.importorskip("pyarrow", "11.0.0") # additional supported types for pyarrow dti = pd.date_range("20130101", periods=3, tz="Europe/Brussels") @@ -938,6 +940,8 @@ def test_timestamp_nanoseconds(self, pa): check_round_trip(df, pa, write_kwargs={"version": ver}) def test_timezone_aware_index(self, request, pa, timezone_aware_date_list): + pytest.importorskip("pyarrow", "11.0.0") + if timezone_aware_date_list.tzinfo != datetime.timezone.utc: request.applymarker( pytest.mark.xfail(