Skip to content

Commit

Permalink
feat: Support max/min method for Time dtype
Browse files Browse the repository at this point in the history
  • Loading branch information
eitsupi committed Nov 17, 2024
1 parent 34ee4ee commit bd66968
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
4 changes: 4 additions & 0 deletions crates/polars-core/src/datatypes/dtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,8 @@ impl DataType {
UInt64 => Scalar::from(u64::MAX),
Float32 => Scalar::from(f32::INFINITY),
Float64 => Scalar::from(f64::INFINITY),
#[cfg(feature = "dtype-time")]
Time => Scalar::new(Time, AnyValue::Time(NS_IN_DAY - 1)),
dt => polars_bail!(ComputeError: "cannot determine upper bound for dtype `{}`", dt),
};
Ok(v)
Expand All @@ -613,6 +615,8 @@ impl DataType {
UInt64 => Scalar::from(u64::MIN),
Float32 => Scalar::from(f32::NEG_INFINITY),
Float64 => Scalar::from(f64::NEG_INFINITY),
#[cfg(feature = "dtype-time")]
Time => Scalar::new(Time, AnyValue::Time(0)),
dt => polars_bail!(ComputeError: "cannot determine lower bound for dtype `{}`", dt),
};
Ok(v)
Expand Down
38 changes: 38 additions & 0 deletions py-polars/polars/datatypes/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,44 @@ class Time(TemporalType):
The integer indicates the number of nanoseconds since midnight.
"""

@classmethod
def max(cls) -> pl.Expr:
"""
Return a literal expression representing the maximum value of this data type.
Examples
--------
>>> pl.select(pl.Time.max() == 86_399_999_999_999)
shape: (1, 1)
┌─────────┐
│ literal │
│ --- │
│ bool │
╞═════════╡
│ true │
└─────────┘
"""
return pl.Expr._from_pyexpr(plr._get_dtype_max(cls))

@classmethod
def min(cls) -> pl.Expr:
"""
Return a literal expression representing the minimum value of this data type.
Examples
--------
>>> pl.select(pl.Time.min() == 0)
shape: (1, 1)
┌─────────┐
│ literal │
│ --- │
│ bool │
╞═════════╡
│ true │
└─────────┘
"""
return pl.Expr._from_pyexpr(plr._get_dtype_min(cls))


class Datetime(TemporalType):
"""
Expand Down
12 changes: 8 additions & 4 deletions py-polars/tests/unit/test_datatypes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import pickle
from datetime import datetime, timedelta
from datetime import datetime, time, timedelta
from typing import TYPE_CHECKING

import pytest
Expand Down Expand Up @@ -217,12 +217,16 @@ def test_raise_invalid_namespace() -> None:
(pl.UInt64, 0, 18446744073709551615),
(pl.Float32, float("-inf"), float("inf")),
(pl.Float64, float("-inf"), float("inf")),
(pl.Time, time(0, 0), time(23, 59, 59, 999999)),
],
)
def test_max_min(
dtype: datatypes.IntegerType | datatypes.Float32 | datatypes.Float64,
upper: int | float,
lower: int | float,
dtype: datatypes.IntegerType
| datatypes.Float32
| datatypes.Float64
| datatypes.Time,
upper: int | float | time,
lower: int | float | time,
) -> None:
df = pl.select(min=dtype.min(), max=dtype.max())
assert df.to_series(0).item() == lower
Expand Down

0 comments on commit bd66968

Please sign in to comment.