Skip to content

Commit

Permalink
pyhon: rows return date and datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Dec 9, 2021
1 parent 6f72cdf commit 63f2b73
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 4 deletions.
9 changes: 9 additions & 0 deletions polars/polars-lazy/src/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ impl GetOutput {
f(&flds[0])
}))
}

pub fn map_dtype<F: 'static + Fn(&DataType) -> DataType + Send + Sync>(f: F) -> Self {
NoEq::new(Arc::new(move |_: &Schema, _: Context, flds: &[Field]| {
let mut fld = flds[0].clone();
let new_type = f(fld.data_type());
fld.coerce(new_type);
fld
}))
}
}

impl<F> FunctionOutputField for F
Expand Down
1 change: 1 addition & 0 deletions py-polars/docs/source/reference/expression.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ Manipulation/ selection
Expr.upper_bound
Expr.str_concat
Expr.reshape
Series.to_physical

Column names
------------
Expand Down
11 changes: 11 additions & 0 deletions py-polars/polars/internals/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,17 @@ def function(s: "pli.Series") -> "pli.Series":

return self.map(function, return_dtype=dtype)

def to_physical(self) -> "Expr":
"""
Cast to physical representation of the logical dtype.
Date -> Int32
Datetime -> Int64
Time -> Int64
other -> other
"""
return wrap_expr(self._pyexpr.to_physical())

def sqrt(self) -> "Expr":
"""
Compute the square root of the elements
Expand Down
20 changes: 17 additions & 3 deletions py-polars/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,25 @@ impl IntoPy<PyObject> for Wrap<AnyValue<'_>> {
let s = rev.get(idx);
s.into_py(py)
}
AnyValue::Date(v) => v.into_py(py),
AnyValue::Datetime(v) => v.into_py(py),
AnyValue::Date(v) => {
let pl = PyModule::import(py, "polars").unwrap();
let pli = pl.getattr("internals").unwrap();
let m_series = pli.getattr("series").unwrap();
let convert = m_series.getattr("_to_python_datetime").unwrap();
let py_date_dtype = pl.getattr("Date").unwrap();
convert.call1((v, py_date_dtype)).unwrap().into_py(py)
}
AnyValue::Datetime(v) => {
let pl = PyModule::import(py, "polars").unwrap();
let pli = pl.getattr("internals").unwrap();
let m_series = pli.getattr("series").unwrap();
let convert = m_series.getattr("_to_python_datetime").unwrap();
let py_datetime_dtype = pl.getattr("Datetime").unwrap();
convert.call1((v, py_datetime_dtype)).unwrap().into_py(py)
}
AnyValue::Time(v) => v.into_py(py),
AnyValue::List(v) => {
let pypolars = PyModule::import(py, "polars").expect("polars installed");
let pypolars = PyModule::import(py, "polars").unwrap();
let pyseries = PySeries::new(v);
let python_series_wrapper = pypolars
.getattr("wrap_s")
Expand Down
10 changes: 10 additions & 0 deletions py-polars/src/lazy/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,16 @@ impl PyExpr {
pub fn cumcount(&self, reverse: bool) -> Self {
self.inner.clone().cumcount(reverse).into()
}

pub fn to_physical(&self) -> Self {
self.inner
.clone()
.map(
|s| Ok(s.to_physical_repr().into_owned()),
GetOutput::map_dtype(|dt| dt.to_physical()),
)
.into()
}
}

impl From<dsl::Expr> for PyExpr {
Expand Down
11 changes: 10 additions & 1 deletion py-polars/tests/test_datelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ def test_timezone() -> None:


def test_to_list() -> None:

s = pl.Series("date", [123543, 283478, 1243]).cast(pl.Date)

out = s.to_list()
Expand All @@ -211,3 +210,13 @@ def test_to_list() -> None:
s = pl.Series("datetime", [123543, 283478, 1243]).cast(pl.Datetime)
out = s.to_list()
assert out[0] == datetime(1970, 1, 1, 0, 2, 3, 543000)


def test_rows() -> None:
s0 = pl.Series("date", [123543, 283478, 1243]).cast(pl.Date)
s1 = pl.Series("datetime", [123543, 283478, 1243]).cast(pl.Datetime)
df = pl.DataFrame([s0, s1])

rows = df.rows()
assert rows[0][0] == date(2308, 4, 2)
assert rows[0][1] == datetime(1970, 1, 1, 0, 2, 3, 543000)

0 comments on commit 63f2b73

Please sign in to comment.