Skip to content

Commit 30b0586

Browse files
authored
Fix up some return types for methods with skip_nulls (#215)
1 parent b9ce3c8 commit 30b0586

File tree

5 files changed

+22
-18
lines changed

5 files changed

+22
-18
lines changed

spec/API_specification/dataframe_api/_types.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
# Type alias: Mypy needs Any, but for readability we need to make clear this
2424
# is a Python scalar (i.e., an instance of `bool`, `int`, `float`, `str`, etc.)
2525
Scalar = Any
26+
# null is a special object which represents a missing value.
27+
# It is not valid as a type.
28+
NullType = Any
2629

2730
array = TypeVar("array")
2831
device = TypeVar("device")

spec/API_specification/dataframe_api/column_object.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from __future__ import annotations
22

3-
from typing import Any,NoReturn, Sequence, TYPE_CHECKING, Literal, Generic
3+
from typing import Any,NoReturn, TYPE_CHECKING, Literal, Generic
44

55
from ._types import DType
66

77
if TYPE_CHECKING:
8-
from . import Bool, null
9-
from ._types import Scalar
8+
from . import Bool
9+
from ._types import NullType, Scalar
1010

1111

1212
__all__ = ['Column']
@@ -455,7 +455,7 @@ def __invert__(self: Column[Bool]) -> Column[Bool]:
455455
If any of the Column's columns is not boolean.
456456
"""
457457

458-
def any(self: Column[Bool], *, skip_nulls: bool = True) -> bool:
458+
def any(self: Column[Bool], *, skip_nulls: bool = True) -> bool | NullType:
459459
"""
460460
Reduction returns a bool.
461461
@@ -465,7 +465,7 @@ def any(self: Column[Bool], *, skip_nulls: bool = True) -> bool:
465465
If column is not boolean.
466466
"""
467467

468-
def all(self: Column[Bool], *, skip_nulls: bool = True) -> bool:
468+
def all(self: Column[Bool], *, skip_nulls: bool = True) -> bool | NullType:
469469
"""
470470
Reduction returns a bool.
471471
@@ -475,48 +475,48 @@ def all(self: Column[Bool], *, skip_nulls: bool = True) -> bool:
475475
If column is not boolean.
476476
"""
477477

478-
def min(self, *, skip_nulls: bool = True) -> Scalar:
478+
def min(self, *, skip_nulls: bool = True) -> Scalar | NullType:
479479
"""
480480
Reduction returns a scalar. Any data type that supports comparisons
481481
must be supported. The returned value has the same dtype as the column.
482482
"""
483483

484-
def max(self, *, skip_nulls: bool = True) -> Scalar:
484+
def max(self, *, skip_nulls: bool = True) -> Scalar | NullType:
485485
"""
486486
Reduction returns a scalar. Any data type that supports comparisons
487487
must be supported. The returned value has the same dtype as the column.
488488
"""
489489

490-
def sum(self, *, skip_nulls: bool = True) -> Scalar:
490+
def sum(self, *, skip_nulls: bool = True) -> Scalar | NullType:
491491
"""
492492
Reduction returns a scalar. Must be supported for numerical and
493493
datetime data types. The returned value has the same dtype as the
494494
column.
495495
"""
496496

497-
def prod(self, *, skip_nulls: bool = True) -> Scalar:
497+
def prod(self, *, skip_nulls: bool = True) -> Scalar | NullType:
498498
"""
499499
Reduction returns a scalar. Must be supported for numerical data types.
500500
The returned value has the same dtype as the column.
501501
"""
502502

503-
def median(self, *, skip_nulls: bool = True) -> Scalar:
503+
def median(self, *, skip_nulls: bool = True) -> Scalar | NullType:
504504
"""
505505
Reduction returns a scalar. Must be supported for numerical and
506506
datetime data types. Returns a float for numerical data types, and
507507
datetime (with the appropriate timedelta format string) for datetime
508508
dtypes.
509509
"""
510510

511-
def mean(self, *, skip_nulls: bool = True) -> Scalar:
511+
def mean(self, *, skip_nulls: bool = True) -> Scalar | NullType:
512512
"""
513513
Reduction returns a scalar. Must be supported for numerical and
514514
datetime data types. Returns a float for numerical data types, and
515515
datetime (with the appropriate timedelta format string) for datetime
516516
dtypes.
517517
"""
518518

519-
def std(self, *, correction: int | float = 1, skip_nulls: bool = True) -> Scalar:
519+
def std(self, *, correction: int | float = 1, skip_nulls: bool = True) -> Scalar | NullType:
520520
"""
521521
Reduction returns a scalar. Must be supported for numerical and
522522
datetime data types. Returns a float for numerical data types, and
@@ -542,7 +542,7 @@ def std(self, *, correction: int | float = 1, skip_nulls: bool = True) -> Scalar
542542
Whether to skip null values.
543543
"""
544544

545-
def var(self, *, correction: int | float = 1, skip_nulls: bool = True) -> Scalar:
545+
def var(self, *, correction: int | float = 1, skip_nulls: bool = True) -> Scalar | NullType:
546546
"""
547547
Reduction returns a scalar. Must be supported for numerical and
548548
datetime data types. Returns a float for numerical data types, and
@@ -662,7 +662,7 @@ def unique_indices(self, *, skip_nulls: bool = True) -> Column[Any]:
662662
"""
663663
...
664664

665-
def fill_nan(self: Column[DType], value: float | 'null', /) -> Column[DType]:
665+
def fill_nan(self: Column[DType], value: float | NullType, /) -> Column[DType]:
666666
"""
667667
Fill floating point ``nan`` values with the given fill value.
668668

spec/API_specification/dataframe_api/dataframe_object.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
if TYPE_CHECKING:
77
from .column_object import Column
88
from .groupby_object import GroupBy
9-
from . import Bool, null
10-
from ._types import Scalar
9+
from . import Bool
10+
from ._types import NullType, Scalar
1111

1212

1313
__all__ = ["DataFrame"]
@@ -768,7 +768,7 @@ def unique_indices(self, keys: Sequence[str], *, skip_nulls: bool = True) -> Col
768768
"""
769769
...
770770

771-
def fill_nan(self, value: float | 'null', /) -> DataFrame:
771+
def fill_nan(self, value: float | NullType, /) -> DataFrame:
772772
"""
773773
Fill ``nan`` values with the given fill value.
774774

spec/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
('py:class', 'Scalar'),
8585
('py:class', 'Bool'),
8686
('py:class', 'optional'),
87+
('py:class', 'NullType'),
8788
]
8889
# NOTE: this alias handling isn't used yet - added in anticipation of future
8990
# need based on dataframe API aliases.

spec/design_topics/python_builtin_types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class DataFrame:
1818
...
1919

2020
class Column:
21-
def mean(self, skip_nulls: bool = True) -> float:
21+
def mean(self, skip_nulls: bool = True) -> float | NullType:
2222
...
2323

2424
larger = df2 > df1.get_column_by_name('foo').mean()

0 commit comments

Comments
 (0)