Skip to content

Commit 7039781

Browse files
authored
feat(array): #624 accumulate (#1400)
* feat(array): #624 accumulate * use pd.array directly * fix(comment): remove Series[Int64Dtype]
1 parent 8527b25 commit 7039781

File tree

8 files changed

+91
-25
lines changed

8 files changed

+91
-25
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ repos:
1111
hooks:
1212
- id: isort
1313
- repo: https://github.com/astral-sh/ruff-pre-commit
14-
rev: v0.13.2
14+
rev: v0.13.3
1515
hooks:
1616
- id: ruff-check
1717
args: [

pandas-stubs/core/arrays/base.pyi

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections.abc import Iterator
22
from typing import (
33
Any,
4+
Literal,
45
overload,
56
)
67

@@ -68,7 +69,13 @@ class ExtensionArray:
6869
def _reduce(
6970
self, name: str, *, skipna: bool = ..., keepdims: bool = ..., **kwargs
7071
) -> object: ...
71-
def _accumulate(self, name: str, *, skipna: bool = ..., **kwargs) -> Self: ...
72+
def _accumulate(
73+
self,
74+
name: Literal["cummin", "cummax", "cumsum", "cumprod"],
75+
*,
76+
skipna: bool = True,
77+
**kwargs,
78+
) -> Self: ...
7279

7380
class ExtensionOpsMixin:
7481
@classmethod

pandas-stubs/core/construction.pyi

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
from collections.abc import Sequence
2+
from typing import overload
23

34
import numpy as np
45
from pandas.core.arrays.base import ExtensionArray
6+
from pandas.core.arrays.integer import IntegerArray
7+
8+
from pandas._libs.missing import NAType
59

610
from pandas.core.dtypes.dtypes import ExtensionDtype
711

12+
@overload
13+
def array(
14+
data: Sequence[int] | Sequence[int | NAType],
15+
dtype: str | np.dtype | ExtensionDtype | None = None,
16+
copy: bool = True,
17+
) -> IntegerArray: ...
18+
@overload
819
def array(
920
data: Sequence[object],
1021
dtype: str | np.dtype | ExtensionDtype | None = None,

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ mypy = "1.18.2"
4040
pandas = "2.3.2"
4141
pyarrow = ">=10.0.1"
4242
pytest = ">=8.4.2"
43-
pyright = ">=1.1.405"
43+
pyright = ">=1.1.406"
4444
ty = ">=0.0.1a21"
4545
pyrefly = ">=0.35.0"
4646
poethepoet = ">=0.16.5"

tests/arrays/__init__.py

Whitespace-only changes.

tests/arrays/test_arrays.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from pandas.core.arrays.integer import IntegerArray
2+
from pandas.core.construction import array
3+
from typing_extensions import assert_type
4+
5+
from pandas._libs.missing import NA
6+
7+
from tests import check
8+
9+
10+
def test_construction() -> None:
11+
check(assert_type(array([1]), IntegerArray), IntegerArray)
12+
check(assert_type(array([1, NA]), IntegerArray), IntegerArray)

tests/arrays/test_cumul.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from pandas.core.arrays.integer import IntegerArray
2+
from pandas.core.construction import array
3+
from typing_extensions import assert_type
4+
5+
from pandas._libs.missing import NA
6+
7+
from tests import check
8+
9+
10+
def test_cumul_int64dtype() -> None:
11+
arr = array([1, NA, 2])
12+
13+
check(assert_type(arr._accumulate("cummin"), IntegerArray), IntegerArray)
14+
check(assert_type(arr._accumulate("cummax"), IntegerArray), IntegerArray)
15+
check(assert_type(arr._accumulate("cumsum"), IntegerArray), IntegerArray)
16+
check(assert_type(arr._accumulate("cumprod"), IntegerArray), IntegerArray)
17+
18+
check(
19+
assert_type(arr._accumulate("cummin", skipna=False), IntegerArray),
20+
IntegerArray,
21+
)
22+
check(
23+
assert_type(arr._accumulate("cummax", skipna=False), IntegerArray),
24+
IntegerArray,
25+
)
26+
check(
27+
assert_type(arr._accumulate("cumsum", skipna=False), IntegerArray),
28+
IntegerArray,
29+
)
30+
check(
31+
assert_type(arr._accumulate("cumprod", skipna=False), IntegerArray),
32+
IntegerArray,
33+
)

tests/series/test_properties.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,27 @@
33
)
44

55
import numpy as np
6-
import pandas as pd
76
from pandas.core.arrays import DatetimeArray
87
from pandas.core.arrays.base import ExtensionArray
8+
from pandas.core.arrays.categorical import Categorical
99
from pandas.core.arrays.interval import IntervalArray
1010
from pandas.core.arrays.timedeltas import TimedeltaArray
11+
from pandas.core.frame import DataFrame
1112
from pandas.core.indexes.accessors import (
1213
DatetimeProperties,
1314
PeriodProperties,
1415
Properties,
1516
TimedeltaProperties,
1617
)
18+
from pandas.core.indexes.interval import interval_range
19+
from pandas.core.indexes.period import period_range
20+
from pandas.core.series import Series
1721
from typing_extensions import assert_type
1822

23+
from pandas._libs.interval import Interval
24+
from pandas._libs.tslibs.timedeltas import Timedelta
25+
from pandas._libs.tslibs.timestamps import Timestamp
26+
1927
from tests import (
2028
TYPE_CHECKING_INVALID_USAGE,
2129
check,
@@ -25,58 +33,53 @@
2533
from pandas.core.indexes.accessors import TimestampProperties # noqa: F401
2634

2735

28-
def test_dt_property() -> None:
36+
def test_property_dt() -> None:
2937
"""Test the Series.dt property"""
3038
check(
31-
assert_type(pd.Series([pd.Timestamp(2025, 9, 28)]).dt, "TimestampProperties"),
39+
assert_type(Series([Timestamp(2025, 9, 28)]).dt, "TimestampProperties"),
3240
DatetimeProperties,
3341
)
3442
check(
35-
assert_type(pd.Series([pd.Timedelta(1, "s")]).dt, TimedeltaProperties),
43+
assert_type(Series([Timedelta(1, "s")]).dt, TimedeltaProperties),
3644
TimedeltaProperties,
3745
)
3846
check(
3947
assert_type(
40-
pd.period_range(start="2022-06-01", periods=10).to_series().dt,
48+
period_range(start="2022-06-01", periods=10).to_series().dt,
4149
PeriodProperties,
4250
),
4351
PeriodProperties,
4452
)
4553

4654
if TYPE_CHECKING_INVALID_USAGE:
47-
s = pd.DataFrame({"a": [1]})["a"]
55+
s = DataFrame({"a": [1]})["a"]
4856
# python/mypy#19952: mypy believes Properties and its subclasses have a
4957
# conflict and gives Any for s.dt
5058
assert_type(s.dt, Properties) # type: ignore[assert-type]
51-
_1 = pd.Series([1]).dt # type: ignore[arg-type] # pyright: ignore[reportAttributeAccessIssue]
59+
_1 = Series([1]).dt # type: ignore[arg-type] # pyright: ignore[reportAttributeAccessIssue]
5260

5361

54-
def test_array_property() -> None:
62+
def test_property_array() -> None:
5563
"""Test that Series.array returns ExtensionArray and its subclasses"""
5664
check(
57-
assert_type(
58-
pd.Series([1], dtype="category").array,
59-
pd.Categorical,
60-
),
61-
pd.Categorical,
62-
int,
65+
assert_type(Series([1], dtype="category").array, Categorical), Categorical, int
6366
)
6467
check(
65-
assert_type(pd.Series(pd.interval_range(0, 1)).array, IntervalArray),
68+
assert_type(Series(interval_range(0, 1)).array, IntervalArray),
6669
IntervalArray,
67-
pd.Interval,
70+
Interval,
6871
)
6972
check(
70-
assert_type(pd.Series([pd.Timestamp(2025, 9, 28)]).array, DatetimeArray),
73+
assert_type(Series([Timestamp(2025, 9, 28)]).array, DatetimeArray),
7174
DatetimeArray,
72-
pd.Timestamp,
75+
Timestamp,
7376
)
7477
check(
75-
assert_type(pd.Series([pd.Timedelta(1, "s")]).array, TimedeltaArray),
78+
assert_type(Series([Timedelta(1, "s")]).array, TimedeltaArray),
7679
TimedeltaArray,
77-
pd.Timedelta,
80+
Timedelta,
7881
)
79-
check(assert_type(pd.Series([1]).array, ExtensionArray), ExtensionArray, np.integer)
82+
check(assert_type(Series([1]).array, ExtensionArray), ExtensionArray, np.integer)
8083
# python/mypy#19952: mypy believes ExtensionArray and its subclasses have a
8184
# conflict and gives Any for s.array
82-
check(assert_type(pd.Series([1, "s"]).array, ExtensionArray), ExtensionArray) # type: ignore[assert-type]
85+
check(assert_type(Series([1, "s"]).array, ExtensionArray), ExtensionArray) # type: ignore[assert-type]

0 commit comments

Comments
 (0)