Skip to content

Rename dtype typevar #198

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

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
46 changes: 6 additions & 40 deletions spec/API_specification/dataframe_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
"""
from __future__ import annotations

from typing import Mapping, Sequence, Any
from typing import Mapping, Sequence, Any, TYPE_CHECKING

from .column_object import *
from .dataframe_object import DataFrame
from .groupby_object import *
from ._types import DType
from .dtypes import *

if TYPE_CHECKING:
from ._types import DType

__all__ = [
"__dataframe_api_version__",
Expand Down Expand Up @@ -60,7 +63,7 @@ def concat(dataframes: Sequence[DataFrame]) -> DataFrame:
"""
...

def column_from_sequence(sequence: Sequence[Any], *, dtype: Any) -> Column[Any]:
def column_from_sequence(sequence: Sequence[Any], *, dtype: DType) -> Column[Any]:
"""
Construct Column from sequence of elements.

Expand Down Expand Up @@ -140,40 +143,3 @@ def is_null(value: object, /) -> bool:
implements the dataframe API standard, False otherwise.

"""

##########
# Dtypes #
##########

class Int64:
"""Integer type with 64 bits of precision."""

class Int32:
"""Integer type with 32 bits of precision."""

class Int16:
"""Integer type with 16 bits of precision."""

class Int8:
"""Integer type with 8 bits of precision."""

class UInt64:
"""Unsigned integer type with 64 bits of precision."""

class UInt32:
"""Unsigned integer type with 32 bits of precision."""

class UInt16:
"""Unsigned integer type with 16 bits of precision."""

class UInt8:
"""Unsigned integer type with 8 bits of precision."""

class Float64:
"""Floating point type with 64 bits of precision."""

class Float32:
"""Floating point type with 32 bits of precision."""

class Bool:
"""Boolean type with 8 bits of precision."""
22 changes: 20 additions & 2 deletions spec/API_specification/dataframe_api/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,34 @@
TypeVar,
Union,
Protocol,
TYPE_CHECKING,
)
from enum import Enum

if TYPE_CHECKING:
from .dtypes import (
Bool,
Float64,
Float32,
Int64,
Int32,
Int16,
Int8,
UInt64,
UInt32,
UInt16,
UInt8,
)

DType = Union[Bool, Float64, Float32, Int64, Int32, Int16, Int8, UInt64, UInt32, UInt16, UInt8]

# Type alias: Mypy needs Any, but for readability we need to make clear this
# is a Python scalar (i.e., an instance of `bool`, `int`, `float`, `str`, etc.)
Scalar = Any

array = TypeVar("array")
device = TypeVar("device")
DType = TypeVar("DType")
DTypeT = TypeVar("DTypeT")
SupportsDLPack = TypeVar("SupportsDLPack")
SupportsBufferProtocol = TypeVar("SupportsBufferProtocol")
PyCapsule = TypeVar("PyCapsule")
Expand Down Expand Up @@ -60,7 +78,7 @@ def __len__(self, /) -> int:
"Sequence",
"array",
"device",
"DType",
"DTypeT",
"ellipsis",
"Enum",
]
30 changes: 15 additions & 15 deletions spec/API_specification/dataframe_api/column_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import Any,NoReturn, Sequence, TYPE_CHECKING, Literal, Generic

from ._types import DType
from ._types import DTypeT

if TYPE_CHECKING:
from . import Bool, null
Expand All @@ -12,7 +12,7 @@
__all__ = ['Column']


class Column(Generic[DType]):
class Column(Generic[DTypeT]):
"""
Column object

Expand Down Expand Up @@ -81,7 +81,7 @@ def dtype(self) -> Any:
Return data type of column.
"""

def get_rows(self: Column[DType], indices: Column[Any]) -> Column[DType]:
def get_rows(self: Column[DTypeT], indices: Column[Any]) -> Column[DTypeT]:
"""
Select a subset of rows, similar to `ndarray.take`.

Expand Down Expand Up @@ -157,7 +157,7 @@ def __eq__(self, other: Column[Any] | Scalar) -> Column[Bool]: # type: ignore[o
Column
"""

def __ne__(self: Column[DType], other: Column[DType] | Scalar) -> Column[Bool]: # type: ignore[override]
def __ne__(self: Column[DTypeT], other: Column[DTypeT] | Scalar) -> Column[Bool]: # type: ignore[override]
"""
Compare for non-equality.

Expand All @@ -175,7 +175,7 @@ def __ne__(self: Column[DType], other: Column[DType] | Scalar) -> Column[Bool]:
Column
"""

def __ge__(self: Column[DType], other: Column[DType] | Scalar) -> Column[Bool]:
def __ge__(self: Column[DTypeT], other: Column[DTypeT] | Scalar) -> Column[Bool]:
"""
Compare for "greater than or equal to" `other`.

Expand All @@ -191,7 +191,7 @@ def __ge__(self: Column[DType], other: Column[DType] | Scalar) -> Column[Bool]:
Column
"""

def __gt__(self: Column[DType], other: Column[DType] | Scalar) -> Column[Bool]:
def __gt__(self: Column[DTypeT], other: Column[DTypeT] | Scalar) -> Column[Bool]:
"""
Compare for "greater than" `other`.

Expand All @@ -207,7 +207,7 @@ def __gt__(self: Column[DType], other: Column[DType] | Scalar) -> Column[Bool]:
Column
"""

def __le__(self: Column[DType], other: Column[DType] | Scalar) -> Column[Bool]:
def __le__(self: Column[DTypeT], other: Column[DTypeT] | Scalar) -> Column[Bool]:
"""
Compare for "less than or equal to" `other`.

Expand All @@ -223,7 +223,7 @@ def __le__(self: Column[DType], other: Column[DType] | Scalar) -> Column[Bool]:
Column
"""

def __lt__(self: Column[DType], other: Column[DType] | Scalar) -> Column[Bool]:
def __lt__(self: Column[DTypeT], other: Column[DTypeT] | Scalar) -> Column[Bool]:
"""
Compare for "less than" `other`.

Expand Down Expand Up @@ -527,26 +527,26 @@ def var(self, *, correction: int | float = 1, skip_nulls: bool = True) -> Scalar
Whether to skip null values.
"""

def cumulative_max(self: Column[DType]) -> Column[DType]:
def cumulative_max(self: Column[DTypeT]) -> Column[DTypeT]:
"""
Reduction returns a Column. Any data type that supports comparisons
must be supported. The returned value has the same dtype as the column.
"""

def cumulative_min(self: Column[DType]) -> Column[DType]:
def cumulative_min(self: Column[DTypeT]) -> Column[DTypeT]:
"""
Reduction returns a Column. Any data type that supports comparisons
must be supported. The returned value has the same dtype as the column.
"""

def cumulative_sum(self: Column[DType]) -> Column[DType]:
def cumulative_sum(self: Column[DTypeT]) -> Column[DTypeT]:
"""
Reduction returns a Column. Must be supported for numerical and
datetime data types. The returned value has the same dtype as the
column.
"""

def cumulative_prod(self: Column[DType]) -> Column[DType]:
def cumulative_prod(self: Column[DTypeT]) -> Column[DTypeT]:
"""
Reduction returns a Column. Must be supported for numerical and
datetime data types. The returned value has the same dtype as the
Expand Down Expand Up @@ -591,7 +591,7 @@ def is_nan(self) -> Column[Bool]:
In particular, does not check for `np.timedelta64('NaT')`.
"""

def is_in(self: Column[DType], values: Column[DType]) -> Column[Bool]:
def is_in(self: Column[DTypeT], values: Column[DTypeT]) -> Column[Bool]:
"""
Indicate whether the value at each row matches any value in `values`.

Expand Down Expand Up @@ -630,7 +630,7 @@ def unique_indices(self, *, skip_nulls: bool = True) -> Column[Any]:
"""
...

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

Expand All @@ -644,7 +644,7 @@ def fill_nan(self: Column[DType], value: float | 'null', /) -> Column[DType]:
"""
...

def fill_null(self: Column[DType], value: Scalar, /) -> Column[DType]:
def fill_null(self: Column[DTypeT], value: Scalar, /) -> Column[DTypeT]:
"""
Fill null values with the given fill value.

Expand Down
36 changes: 36 additions & 0 deletions spec/API_specification/dataframe_api/dtypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
##########
# Dtypes #
##########

class Int64:
"""Integer type with 64 bits of precision."""

class Int32:
"""Integer type with 32 bits of precision."""

class Int16:
"""Integer type with 16 bits of precision."""

class Int8:
"""Integer type with 8 bits of precision."""

class UInt64:
"""Unsigned integer type with 64 bits of precision."""

class UInt32:
"""Unsigned integer type with 32 bits of precision."""

class UInt16:
"""Unsigned integer type with 16 bits of precision."""

class UInt8:
"""Unsigned integer type with 8 bits of precision."""

class Float64:
"""Floating point type with 64 bits of precision."""

class Float32:
"""Floating point type with 32 bits of precision."""

class Bool:
"""Boolean type with 8 bits of precision."""
2 changes: 1 addition & 1 deletion spec/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
('py:class', 'array'),
('py:class', 'DataFrame'),
('py:class', 'device'),
('py:class', 'DType'),
('py:class', 'DTypeT'),
('py:class', 'NestedSequence'),
('py:class', 'collections.abc.Sequence'),
('py:class', 'PyCapsule'),
Expand Down