Skip to content

Commit 5063f9d

Browse files
Add namespace.date (data-apis#289)
* drive by: put __all__ in alphabetical order * add to namespace, type return as scalar * remove outdated type ignore * note range of support * Update spec/API_specification/dataframe_api/__init__.py Co-authored-by: Keith Kraus <[email protected]> --------- Co-authored-by: Keith Kraus <[email protected]>
1 parent 28bb549 commit 5063f9d

File tree

3 files changed

+45
-23
lines changed

3 files changed

+45
-23
lines changed

spec/API_specification/dataframe_api/__init__.py

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,36 @@
1212
from .dtypes import *
1313

1414
if TYPE_CHECKING:
15-
from .typing import DType
15+
from .typing import DType, Scalar
1616

1717
__all__ = [
18-
"__dataframe_api_version__",
19-
"DataFrame",
20-
"Column",
21-
"column_from_sequence",
22-
"column_from_1d_array",
23-
"concat",
24-
"dataframe_from_columns",
25-
"dataframe_from_2d_array",
26-
"is_null",
27-
"null",
28-
"Int64",
29-
"Int32",
30-
"Int16",
31-
"Int8",
32-
"UInt64",
33-
"UInt32",
34-
"UInt16",
35-
"UInt8",
36-
"Float64",
37-
"Float32",
3818
"Bool",
19+
"Column",
20+
"DataFrame",
3921
"Date",
4022
"Datetime",
4123
"Duration",
24+
"Float32",
25+
"Float64",
26+
"Int16",
27+
"Int32",
28+
"Int64",
29+
"Int8",
4230
"String",
31+
"UInt16",
32+
"UInt32",
33+
"UInt64",
34+
"UInt8",
35+
"__dataframe_api_version__",
36+
"column_from_1d_array",
37+
"column_from_sequence",
38+
"concat",
39+
"dataframe_from_2d_array",
40+
"dataframe_from_columns",
41+
"date",
4342
"is_dtype",
43+
"is_null",
44+
"null",
4445
]
4546

4647

@@ -235,3 +236,21 @@ def is_dtype(dtype: DType, kind: str | tuple[str, ...]) -> bool:
235236
-------
236237
bool
237238
"""
239+
240+
def date(year: int, month: int, day: int) -> Scalar:
241+
"""
242+
Create date object which can be used for filtering.
243+
244+
The full 32-bit signed integer range of days since epoch should be supported (between -5877641-06-23 and 5881580-07-11 inclusive).
245+
246+
Examples
247+
--------
248+
>>> df: DataFrame
249+
>>> namespace = df.__dataframe_namespace__()
250+
>>> mask = (
251+
... (df.get_column_by_name('date') >= namespace.date(2020, 1, 1))
252+
... & (df.get_column_by_name('date') < namespace.date(2021, 1, 1))
253+
... )
254+
>>> df.filter(mask)
255+
"""
256+

spec/API_specification/dataframe_api/typing.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ def is_null(value: object, /) -> bool:
161161
def is_dtype(dtype: Any, kind: str | tuple[str, ...]) -> bool:
162162
...
163163

164+
@staticmethod
165+
def date(year: int, month: int, day: int) -> Scalar:
166+
...
164167

165168
class SupportsDataFrameAPI(Protocol):
166169
def __dataframe_consortium_standard__(

spec/API_specification/examples/tpch/q5.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ def query(
5555
mask = (
5656
(result.col("c_nationkey") == result.col("s_nationkey"))
5757
& (result.col("r_name") == "ASIA")
58-
& (result.col("o_orderdate") >= namespace.date(1994, 1, 1)) # type: ignore
59-
& (result.col("o_orderdate") < namespace.date(1995, 1, 1)) # type: ignore
58+
& (result.col("o_orderdate") >= namespace.date(1994, 1, 1))
59+
& (result.col("o_orderdate") < namespace.date(1995, 1, 1))
6060
)
6161
result = result.filter(mask)
6262

0 commit comments

Comments
 (0)