From 0354117b70ad64f57ffcb88e30d3f230e574d2c6 Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Tue, 2 Jan 2024 12:53:35 +0000 Subject: [PATCH] MAINT: Add NumPy 2 compat Check compatability with numpy 2 Improve compatability with future pandas --- arch/compat/pandas.py | 8 +++++++- arch/tests/unitroot/cointegration_data.py | 4 +++- arch/tests/univariate/test_mean.py | 4 +++- azure-pipelines.yml | 1 - meson.build | 0 setup.py | 6 ++---- 6 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 meson.build diff --git a/arch/compat/pandas.py b/arch/compat/pandas.py index 462a4222af..3481259e55 100644 --- a/arch/compat/pandas.py +++ b/arch/compat/pandas.py @@ -1,5 +1,8 @@ from typing import TYPE_CHECKING +from packaging.version import parse +import pandas as pd + if TYPE_CHECKING: from pandas.api.types import is_datetime64_any_dtype else: @@ -8,4 +11,7 @@ except ImportError: from pandas.core.common import is_datetime64_any_dtype -__all__ = ["is_datetime64_any_dtype"] +PD_LT_22 = parse(pd.__version__) < parse("2.1.99") +MONTH_END = "M" if PD_LT_22 else "ME" + +__all__ = ["is_datetime64_any_dtype", "MONTH_END"] diff --git a/arch/tests/unitroot/cointegration_data.py b/arch/tests/unitroot/cointegration_data.py index a40374f57b..594e8f362f 100644 --- a/arch/tests/unitroot/cointegration_data.py +++ b/arch/tests/unitroot/cointegration_data.py @@ -1,3 +1,5 @@ +from arch.compat.pandas import MONTH_END + import numpy as np import pandas as pd import pytest @@ -60,7 +62,7 @@ def trivariate_data(request) -> tuple[ArrayLike2D, ArrayLike2D]: idx += 1 y = y @ rot if request.param: - dt_index = pd.date_range("1-1-2000", periods=nobs, freq="M") + dt_index = pd.date_range("1-1-2000", periods=nobs, freq=MONTH_END) cols = [f"y{i}" for i in range(1, 4)] data = pd.DataFrame(y, columns=cols, index=dt_index) return data.iloc[:, :1], data.iloc[:, 1:] diff --git a/arch/tests/univariate/test_mean.py b/arch/tests/univariate/test_mean.py index 7dfb47d5bb..ad9292075f 100644 --- a/arch/tests/univariate/test_mean.py +++ b/arch/tests/univariate/test_mean.py @@ -1,3 +1,5 @@ +from arch.compat.pandas import MONTH_END + from io import StringIO from itertools import product from string import ascii_lowercase @@ -820,7 +822,7 @@ def test_date_first_last_obs(self): assert_equal(res.resid.values, res2.resid.values) def test_align(self): - dates = pd.date_range("2000-01-01", "2010-01-01", freq="M") + dates = pd.date_range("2000-01-01", "2010-01-01", freq=MONTH_END) columns = ["h." + f"{h + 1:>02}" for h in range(10)] forecasts = pd.DataFrame(self.rng.randn(120, 10), index=dates, columns=columns) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 943c1ad1a4..9450ca520d 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -10,7 +10,6 @@ variables: VML_NUM_THREADS: 1 OPENBLAS_NUM_THREADS: 1 PYTHONHASHSEED: 12345678 # Ensure tests are correctly gathered by xdist - # SETUPTOOLS_USE_DISTUTILS: "stdlib" TEST_INSTALL: false MPLBACKEND: agg PYTEST_PATTERN: "(not slow)" diff --git a/meson.build b/meson.build new file mode 100644 index 0000000000..e69de29bb2 diff --git a/setup.py b/setup.py index d9006f6dd2..977421e270 100644 --- a/setup.py +++ b/setup.py @@ -46,10 +46,8 @@ # prevent setup.py from crashing by calling import numpy before numpy is installed class build_ext(_build_ext): def build_extensions(self) -> None: - ref = importlib.resources.files("numpy") / "core/include" - with importlib.resources.as_file(ref) as path: - numpy_incl = str(path) - + import numpy as np + numpy_incl = np.get_include() for ext in self.extensions: if hasattr(ext, "include_dirs") and numpy_incl not in ext.include_dirs: ext.include_dirs.append(numpy_incl)