Skip to content

Support Numpy 2.1 #981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions .github/setup/action.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
name: Install project dependencies
description: Install project dependencies

inputs:
python-version:
description: The python version to use
required: true
os:
required: true
numpy-version:
description: The OS to run on
required: true

runs:
using: composite
steps:

- name: Set up Python
uses: actions/setup-python@v4
with:
Expand All @@ -35,7 +35,3 @@ runs:
- name: Install project dependencies
shell: bash
run: poetry install -vvv --no-root

- name: Set numpy version
shell: bash
run: pip install numpy"${{ inputs. numpy-version }}"
9 changes: 2 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@ jobs:
matrix:
# Don't use macos-latest because it is arm64
os: [ubuntu-latest, windows-latest, macos-13]
python-version: ["3.9", "3.10", "3.11", "3.12"]
include:
- numpy-version: "<2.0"
- numpy-version: ">= 2.0"
os: ubuntu-latest
python-version: ["3.10", "3.11", "3.12"]

name: OS ${{ matrix.os }} - Python ${{ matrix.python-version }} - Numpy ${{ matrix.numpy-version }}
name: OS ${{ matrix.os }} - Python ${{ matrix.python-version }}

steps:
- uses: actions/checkout@v3
Expand All @@ -35,7 +31,6 @@ jobs:
with:
os: ${{ matrix.os }}
python-version: ${{ matrix.python-version }}
numpy-version: ${{ matrix.numpy-version }}

- name: Run mypy on 'tests' (using the local stubs) and on the local stubs
run: poetry run poe mypy
Expand Down
36 changes: 36 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import gc

import pytest


@pytest.fixture
def mpl_cleanup():
"""
Ensure Matplotlib is cleaned up around a test.

Before a test is run:

1) Set the backend to "template" to avoid requiring a GUI.

After a test is run:

1) Reset units registry
2) Reset rc_context
3) Close all figures

See matplotlib/testing/decorators.py#L24.
"""
mpl = pytest.importorskip("matplotlib")
mpl_units = pytest.importorskip("matplotlib.units")
plt = pytest.importorskip("matplotlib.pyplot")
orig_units_registry = mpl_units.registry.copy()
try:
with mpl.rc_context():
mpl.use("template")
yield
finally:
mpl_units.registry.clear()
mpl_units.registry.update(orig_units_registry)
plt.close("all")
# https://matplotlib.org/stable/users/prev_whats_new/whats_new_3.6.0.html#garbage-collection-is-no-longer-run-on-figure-close
gc.collect(1)
5 changes: 2 additions & 3 deletions docs/release_procedure.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@

```shell
rm dist/*
poetry publish --build # you will get prompted for your pypi username/password
poetry build
twine upload dist/* # Requires having the pypi API token allowing uploads
git commit -a -m "Version a.b.c.yymmdd"
git push upstream main
git tag va.b.c.yymmdd
git push upstream --tags
```

The conda bots will recognize that a new version has been uploaded to pypi, and generate a pull request sent to the maintainers to approve it.

Note - Changes will need to be made to use pypi API tokens in the near future.
4 changes: 2 additions & 2 deletions docs/setup.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Set Up Environment

- Make sure you have `python >= 3.9` installed.
- Install poetry: `pip install 'poetry>=1.2'`
- Make sure you have `python >= 3.10` installed.
- Install poetry: `pip install 'poetry>=1.8'`
- Install the project dependencies: `poetry update`
- Enter the virtual environment: `poetry shell`
- Run all tests: `poe test_all`
Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/core/dtypes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ExtensionDtype:
@property
def kind(
self,
) -> Literal["b", "i", "u", "f", "c", "m", "M", "O", "S", "U", "V"]: ...
) -> Literal["b", "i", "u", "f", "c", "m", "M", "O", "S", "U", "V", "T"]: ...
@property
def names(self) -> list[str] | None: ...
def empty(self, size: int | tuple[int, ...]) -> type_t[ExtensionArray]: ...
Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/core/indexes/datetimelike.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DatetimeIndexOpsMixin(ExtensionIndex[S1]):
def argmin(self, axis=..., skipna: bool = ..., *args, **kwargs): ...
def max(self, axis=..., skipna: bool = ..., *args, **kwargs): ...
def argmax(self, axis=..., skipna: bool = ..., *args, **kwargs): ...
def __rsub__( # type: ignore[override]
def __rsub__( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
self, other: DatetimeIndexOpsMixin
) -> TimedeltaIndex: ...

Expand Down
26 changes: 16 additions & 10 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1506,21 +1506,23 @@ class Series(IndexOpsMixin[S1], NDFrame):
self, other: bool | list[bool] | list[int] | np_ndarray_bool | Series[bool]
) -> Series[bool]: ...
@overload
def __and__(self, other: int | np_ndarray_anyint | Series[int]) -> Series[int]: ...
def __and__( # pyright: ignore[reportIncompatibleMethodOverride]
self, other: int | np_ndarray_anyint | Series[int]
) -> Series[int]: ...
# def __array__(self, dtype: Optional[_bool] = ...) -> _np_ndarray
def __div__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ...
def __eq__(self, other: object) -> Series[_bool]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
def __floordiv__(self, other: num | _ListLike | Series[S1]) -> Series[int]: ...
def __ge__( # type: ignore[override]
def __ge__( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
self, other: S1 | _ListLike | Series[S1] | datetime | timedelta | date
) -> Series[_bool]: ...
def __gt__( # type: ignore[override]
def __gt__( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
self, other: S1 | _ListLike | Series[S1] | datetime | timedelta | date
) -> Series[_bool]: ...
def __le__( # type: ignore[override]
def __le__( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
self, other: S1 | _ListLike | Series[S1] | datetime | timedelta | date
) -> Series[_bool]: ...
def __lt__( # type: ignore[override]
def __lt__( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
self, other: S1 | _ListLike | Series[S1] | datetime | timedelta | date
) -> Series[_bool]: ...
@overload
Expand All @@ -1538,7 +1540,9 @@ class Series(IndexOpsMixin[S1], NDFrame):
self, other: bool | list[bool] | list[int] | np_ndarray_bool | Series[bool]
) -> Series[bool]: ...
@overload
def __or__(self, other: int | np_ndarray_anyint | Series[int]) -> Series[int]: ...
def __or__( # pyright: ignore[reportIncompatibleMethodOverride]
self, other: int | np_ndarray_anyint | Series[int]
) -> Series[int]: ...
@overload
def __radd__(self, other: S1 | Series[S1]) -> Self: ...
@overload
Expand All @@ -1549,7 +1553,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
self, other: bool | list[bool] | list[int] | np_ndarray_bool | Series[bool]
) -> Series[bool]: ...
@overload
def __rand__(self, other: int | np_ndarray_anyint | Series[int]) -> Series[int]: ... # type: ignore[misc]
def __rand__(self, other: int | np_ndarray_anyint | Series[int]) -> Series[int]: ... # type: ignore[misc] # pyright: ignore[reportIncompatibleMethodOverride]
def __rdiv__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ...
def __rdivmod__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
def __rfloordiv__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ...
Expand All @@ -1568,7 +1572,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
self, other: bool | list[bool] | list[int] | np_ndarray_bool | Series[bool]
) -> Series[bool]: ...
@overload
def __ror__(self, other: int | np_ndarray_anyint | Series[int]) -> Series[int]: ... # type: ignore[misc]
def __ror__(self, other: int | np_ndarray_anyint | Series[int]) -> Series[int]: ... # type: ignore[misc] # pyright: ignore[reportIncompatibleMethodOverride]
def __rsub__(self, other: num | _ListLike | Series[S1]) -> Series: ...
def __rtruediv__(self, other: num | _ListLike | Series[S1]) -> Series: ...
# ignore needed for mypy as we want different results based on the arguments
Expand All @@ -1577,7 +1581,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
self, other: bool | list[bool] | list[int] | np_ndarray_bool | Series[bool]
) -> Series[bool]: ...
@overload
def __rxor__(self, other: int | np_ndarray_anyint | Series[int]) -> Series[int]: ... # type: ignore[misc]
def __rxor__(self, other: int | np_ndarray_anyint | Series[int]) -> Series[int]: ... # type: ignore[misc] # pyright: ignore[reportIncompatibleMethodOverride]
@overload
def __sub__(
self: Series[Timestamp],
Expand All @@ -1601,7 +1605,9 @@ class Series(IndexOpsMixin[S1], NDFrame):
self, other: bool | list[bool] | list[int] | np_ndarray_bool | Series[bool]
) -> Series[bool]: ...
@overload
def __xor__(self, other: int | np_ndarray_anyint | Series[int]) -> Series[int]: ...
def __xor__( # pyright: ignore[reportIncompatibleMethodOverride]
self, other: int | np_ndarray_anyint | Series[int]
) -> Series[int]: ...
def __invert__(self) -> Series[bool]: ...
# properties
# @property
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ packages = [{ "include" = "pandas-stubs" }]
"Documentation" = "https://pandas.pydata.org/pandas-docs/stable"

[tool.poetry.dependencies]
python = ">=3.9"
python = ">=3.10"
types-pytz = ">= 2022.1.1"
numpy = ">= 1.23.5"
numpy = ">= 2.1"

[tool.poetry.group.dev.dependencies]
mypy = "1.10.1"
Expand Down
5 changes: 5 additions & 0 deletions tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
)


@pytest.fixture(autouse=True)
def autouse_mpl_cleanup(mpl_cleanup):
pass


@pytest.fixture
def close_figures():
plt.close("all")
Expand Down
16 changes: 8 additions & 8 deletions tests/test_scalars.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,8 +875,8 @@ def test_timedelta_cmp() -> None:
le = check(assert_type(c_dt_timedelta <= td, bool), bool)
assert gt != le

gt_b = check(assert_type(c_timedelta64 > td, Any), bool)
le_b = check(assert_type(c_timedelta64 <= td, Any), bool)
gt_b = check(assert_type(c_timedelta64 > td, np.bool), bool)
le_b = check(assert_type(c_timedelta64 <= td, np.bool), bool)
assert gt_b != le_b

gt_a = check(
Expand Down Expand Up @@ -943,8 +943,8 @@ def test_timedelta_cmp() -> None:
ge = check(assert_type(c_dt_timedelta >= td, bool), bool)
assert lt != ge

lt_b = check(assert_type(c_timedelta64 < td, Any), bool)
ge_b = check(assert_type(c_timedelta64 >= td, Any), bool)
lt_b = check(assert_type(c_timedelta64 < td, np.bool), bool)
ge_b = check(assert_type(c_timedelta64 >= td, np.bool), bool)
assert lt_b != ge_b

lt_a = check(
Expand Down Expand Up @@ -1267,8 +1267,8 @@ def test_timestamp_cmp() -> None:
check(assert_type(ts > c_series_dt64, "pd.Series[bool]"), pd.Series, np.bool_)
check(assert_type(ts <= c_series_dt64, "pd.Series[bool]"), pd.Series, np.bool_)

check(assert_type(c_np_dt64 > ts, Any), bool)
check(assert_type(c_np_dt64 <= ts, Any), bool)
check(assert_type(c_np_dt64 > ts, np.bool), bool)
check(assert_type(c_np_dt64 <= ts, np.bool), bool)

gt = check(assert_type(c_dt_datetime > ts, bool), bool)
lte = check(assert_type(c_dt_datetime <= ts, bool), bool)
Expand Down Expand Up @@ -1311,8 +1311,8 @@ def test_timestamp_cmp() -> None:
lt = check(assert_type(c_dt_datetime < ts, bool), bool)
assert gte != lt

check(assert_type(c_np_dt64 >= ts, Any), bool)
check(assert_type(c_np_dt64 < ts, Any), bool)
check(assert_type(c_np_dt64 >= ts, np.bool), bool)
check(assert_type(c_np_dt64 < ts, np.bool), bool)

check(assert_type(c_datetimeindex >= ts, np_ndarray_bool), np.ndarray, np.bool_)
check(assert_type(c_datetimeindex < ts, np_ndarray_bool), np.ndarray, np.bool_)
Expand Down
24 changes: 21 additions & 3 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
TYPE_CHECKING,
Any,
Generic,
Literal,
TypeVar,
Union,
cast,
Expand Down Expand Up @@ -81,6 +82,23 @@
)
from pandas._typing import np_ndarray_int # noqa: F401

# Tests will use numpy 2.1 in python 3.10 or later
# From Numpy 2.1 __init__.pyi
_DTypeKind: TypeAlias = Literal[
"b", # boolean
"i", # signed integer
"u", # unsigned integer
"f", # floating-point
"c", # complex floating-point
"m", # timedelta64
"M", # datetime64
"O", # python object
"S", # byte-string (fixed-width)
"U", # unicode-string (fixed-width)
"V", # void
"T", # unicode-string (variable-width)
]


def test_types_init() -> None:
pd.Series(1)
Expand Down Expand Up @@ -1675,15 +1693,15 @@ def test_dtype_type() -> None:
# GH 216
s1 = pd.Series(["foo"], dtype="string")
check(assert_type(s1.dtype, DtypeObj), ExtensionDtype)
check(assert_type(s1.dtype.kind, str), str)
check(assert_type(s1.dtype.kind, _DTypeKind), str)

s2 = pd.Series([1], dtype="Int64")
check(assert_type(s2.dtype, DtypeObj), ExtensionDtype)
check(assert_type(s2.dtype.kind, str), str)
check(assert_type(s2.dtype.kind, _DTypeKind), str)

s3 = pd.Series([1, 2, 3])
check(assert_type(s3.dtype, DtypeObj), np.dtype)
check(assert_type(s3.dtype.kind, str), str)
check(assert_type(s3.dtype.kind, _DTypeKind), str)


def test_types_to_numpy() -> None:
Expand Down
Loading