Skip to content

Commit

Permalink
Remove code branches on datelike types
Browse files Browse the repository at this point in the history
A lot of operations don't make sense, so let's
not do them. E.g. no variance of dates, no
summing etc.
  • Loading branch information
ritchie46 committed Jun 23, 2021
1 parent 496fce4 commit 79285c4
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 133 deletions.
47 changes: 33 additions & 14 deletions polars/polars-core/src/frame/groupby/resample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ impl DataFrame {
let second_c = "__POLARS_TEMP_SECOND";
let temp_key = "__POLAR_TEMP_NAME";

let key_dtype = key.dtype();
let mut key_phys = key.to_physical_repr();
let mut key = key.clone();
let key_name = key.name().to_string();
let wrong_key_dtype = || Err(PolarsError::Other("key should be date32 || date64".into()));
Expand Down Expand Up @@ -185,18 +187,23 @@ impl DataFrame {

df.hstack_mut(&[year, day])?;

match key.dtype() {
match key_dtype {
DataType::Date32 => {
key = key / n;
key = key * n;
// round to lower bucket
key_phys = key_phys / n;
key_phys = key_phys * n;
}
DataType::Date64 => {
let fact = 1000 * 3600 * 24 * n;
key = key / fact;
key = key * fact;
// round to lower bucket
key_phys = key_phys / fact;
key_phys = key_phys * fact;
}
_ => return wrong_key_dtype(),
}
key = key_phys
.cast_with_dtype(key_dtype)
.expect("back to original type");

df.groupby_stable(&[year_c, day_c])?
}
Expand All @@ -209,14 +216,18 @@ impl DataFrame {
hour.rename(hour_c);
df.hstack_mut(&[year, day, hour])?;

match key.dtype() {
match key_dtype {
DataType::Date64 => {
let fact = 1000 * 3600 * n;
key = key / fact;
key = key * fact;
// round to lower bucket
key_phys = key_phys / fact;
key_phys = key_phys * fact;
}
_ => return wrong_key_dtype(),
}
key = key_phys
.cast_with_dtype(key_dtype)
.expect("back to original type");
df.groupby_stable(&[year_c, day_c, hour_c])?
}
Minute(n) => {
Expand All @@ -231,14 +242,18 @@ impl DataFrame {

df.hstack_mut(&[year, day, hour, minute])?;

match key.dtype() {
match key_dtype {
DataType::Date64 => {
let fact = 1000 * 60 * n;
key = key / fact;
key = key * fact;
// round to lower bucket
key_phys = key_phys / fact;
key_phys = key_phys * fact;
}
_ => return wrong_key_dtype_date64(),
}
key = key_phys
.cast_with_dtype(key_dtype)
.expect("back to original type");

df.groupby_stable(&[year_c, day_c, hour_c, minute_c])?
}
Expand All @@ -256,14 +271,18 @@ impl DataFrame {

df.hstack_mut(&[year, day, hour, minute, second])?;

match key.dtype() {
match key_dtype {
DataType::Date64 => {
let fact = 1000 * n;
key = key / fact;
key = key * fact;
// round to lower bucket
key_phys = key_phys / fact;
key_phys = key_phys * fact;
}
_ => return wrong_key_dtype_date64(),
}
key = key_phys
.cast_with_dtype(key_dtype)
.expect("back to original type");

df.groupby_stable(&[day_c, hour_c, minute_c, second_c])?
}
Expand Down
91 changes: 16 additions & 75 deletions polars/polars-core/src/series/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,18 +412,13 @@ where
type Output = Series;

fn sub(self, rhs: T) -> Self::Output {
macro_rules! numeric {
macro_rules! sub {
($ca:expr) => {{
$ca.sub(rhs).into_series()
}};
}

macro_rules! noop {
($ca:expr) => {{
unimplemented!()
}};
}
match_arrow_data_type_apply_macro_ca!(self, numeric, noop, noop)
match_arrow_data_type_apply_macro_ca_logical_num!(self, sub)
}
}

Expand All @@ -445,18 +440,12 @@ where
type Output = Series;

fn add(self, rhs: T) -> Self::Output {
macro_rules! numeric {
macro_rules! add {
($ca:expr) => {{
$ca.add(rhs).into_series()
}};
}

macro_rules! noop {
($ca:expr) => {{
unimplemented!()
}};
}
match_arrow_data_type_apply_macro_ca!(self, numeric, noop, noop)
match_arrow_data_type_apply_macro_ca_logical_num!(self, add)
}
}

Expand All @@ -478,18 +467,13 @@ where
type Output = Series;

fn div(self, rhs: T) -> Self::Output {
macro_rules! numeric {
macro_rules! div {
($ca:expr) => {{
$ca.div(rhs).into_series()
}};
}

macro_rules! noop {
($ca:expr) => {{
unimplemented!()
}};
}
match_arrow_data_type_apply_macro_ca!(self, numeric, noop, noop)
match_arrow_data_type_apply_macro_ca_logical_num!(self, div)
}
}

Expand All @@ -511,18 +495,12 @@ where
type Output = Series;

fn mul(self, rhs: T) -> Self::Output {
macro_rules! numeric {
macro_rules! mul {
($ca:expr) => {{
$ca.mul(rhs).into_series()
}};
}

macro_rules! noop {
($ca:expr) => {{
unimplemented!()
}};
}
match_arrow_data_type_apply_macro_ca!(self, numeric, noop, noop)
match_arrow_data_type_apply_macro_ca_logical_num!(self, mul)
}
}

Expand All @@ -544,18 +522,12 @@ where
type Output = Series;

fn rem(self, rhs: T) -> Self::Output {
macro_rules! numeric {
macro_rules! rem {
($ca:expr) => {{
$ca.rem(rhs).into_series()
}};
}

macro_rules! noop {
($ca:expr) => {{
unimplemented!()
}};
}
match_arrow_data_type_apply_macro_ca!(self, numeric, noop, noop)
match_arrow_data_type_apply_macro_ca_logical_num!(self, rem)
}
}

Expand Down Expand Up @@ -619,50 +591,33 @@ where
rhs + self
}
fn sub(self, rhs: &Series) -> Self::Output {
macro_rules! numeric {
macro_rules! sub {
($rhs:expr) => {{
$rhs.lhs_sub(self).into_series()
}};
}

macro_rules! noop {
($ca:expr) => {{
unimplemented!()
}};
}
match_arrow_data_type_apply_macro_ca!(rhs, numeric, noop, noop)
match_arrow_data_type_apply_macro_ca_logical_num!(rhs, sub)
}
fn div(self, rhs: &Series) -> Self::Output {
macro_rules! numeric {
macro_rules! div {
($rhs:expr) => {{
$rhs.lhs_div(self).into_series()
}};
}

macro_rules! noop {
($ca:expr) => {{
unimplemented!()
}};
}
match_arrow_data_type_apply_macro_ca!(rhs, numeric, noop, noop)
match_arrow_data_type_apply_macro_ca_logical_num!(rhs, div)
}
fn mul(self, rhs: &Series) -> Self::Output {
// order doesn't matter, dispatch to rhs * lhs
rhs * self
}
fn rem(self, rhs: &Series) -> Self::Output {
macro_rules! numeric {
macro_rules! rem {
($rhs:expr) => {{
$rhs.lhs_rem(self).into_series()
}};
}

macro_rules! noop {
($ca:expr) => {{
unimplemented!()
}};
}
match_arrow_data_type_apply_macro_ca!(rhs, numeric, noop, noop)
match_arrow_data_type_apply_macro_ca_logical_num!(rhs, rem)
}
}

Expand Down Expand Up @@ -736,20 +691,6 @@ mod test {
assert_eq!((1.div(&s)).name(), "foo");
}

#[test]
#[cfg(feature = "dtype-date64")]
fn test_arithmetic_series_date() {
// Date Series have a different dispatch, so let's test that.
let s = Date64Chunked::new_from_slice("foo", &[0, 1, 2, 3]).into_series();

// test if it runs.
let _ = &s * &s;

let _ = s.minute().map(|m| &m / 5);
let _ = s.minute().map(|m| m / 5);
let _ = s.minute().map(|m| m.into_series() / 5);
}

#[test]
#[cfg(feature = "checked_arithmetic")]
fn test_checked_div() {
Expand Down

0 comments on commit 79285c4

Please sign in to comment.