Skip to content

Commit

Permalink
Proposal: Remove date_like_to_physical
Browse files Browse the repository at this point in the history
This function was only used in arithmetic methods in pl.Series. Not sure what we would expect, what should the answer be when we subtract two dates, or a date and a time, given we do not have a timedelta data type. No tests are hitting this logic either. So I propose to remove it, but I may be missing something here..
  • Loading branch information
zundertj authored and ritchie46 committed Nov 26, 2021
1 parent 96ee42f commit 1f85f31
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 45 deletions.
11 changes: 0 additions & 11 deletions py-polars/polars/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,6 @@ class Categorical(DataType):
}


def date_like_to_physical(dtype: Type[DataType]) -> Type[DataType]:
# TODO: add more
if dtype == Date:
return Int32
if dtype == Datetime:
return Int64
if dtype == Time:
return Int64
return dtype


def dtype_to_ctype(dtype: Type[DataType]) -> Type[_SimpleCData]:
try:
return _DTYPE_TO_CTYPE[dtype]
Expand Down
51 changes: 17 additions & 34 deletions py-polars/polars/internals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
UInt32,
UInt64,
Utf8,
date_like_to_physical,
dtype_to_ctype,
dtype_to_ffiname,
maybe_cast,
Expand Down Expand Up @@ -358,8 +357,7 @@ def __add__(self, other: Any) -> "Series":
if isinstance(other, Series):
return wrap_s(self._s.add(other._s))
other = maybe_cast(other, self.dtype)
dtype = date_like_to_physical(self.dtype)
f = get_ffi_func("add_<>", dtype, self._s)
f = get_ffi_func("add_<>", self.dtype, self._s)
if f is None:
return NotImplemented
return wrap_s(f(other))
Expand All @@ -368,29 +366,23 @@ def __sub__(self, other: Any) -> "Series":
if isinstance(other, Series):
return Series._from_pyseries(self._s.sub(other._s))
other = maybe_cast(other, self.dtype)
dtype = date_like_to_physical(self.dtype)
f = get_ffi_func("sub_<>", dtype, self._s)
f = get_ffi_func("sub_<>", self.dtype, self._s)
if f is None:
return NotImplemented
return wrap_s(f(other))

def __truediv__(self, other: Any) -> "Series":
physical_type = date_like_to_physical(self.dtype)

# this branch is exactly the floordiv function without rounding the floats
if self.is_float():
if isinstance(other, Series):
return Series._from_pyseries(self._s.div(other._s))

other = maybe_cast(other, self.dtype)
dtype = date_like_to_physical(self.dtype)
f = get_ffi_func("div_<>", dtype, self._s)
f = get_ffi_func("div_<>", self.dtype, self._s)
if f is None:
return NotImplemented
return wrap_s(f(other))

if self.dtype != physical_type:
return self.__floordiv__(other)
return self.cast(Float64) / other

def __floordiv__(self, other: Any) -> "Series":
Expand All @@ -400,8 +392,7 @@ def __floordiv__(self, other: Any) -> "Series":
return Series._from_pyseries(self._s.div(other._s))

other = maybe_cast(other, self.dtype)
dtype = date_like_to_physical(self.dtype)
f = get_ffi_func("div_<>", dtype, self._s)
f = get_ffi_func("div_<>", self.dtype, self._s)
if f is None:
return NotImplemented
if self.is_float():
Expand All @@ -412,8 +403,7 @@ def __mul__(self, other: Any) -> "Series":
if isinstance(other, Series):
return Series._from_pyseries(self._s.mul(other._s))
other = maybe_cast(other, self.dtype)
dtype = date_like_to_physical(self.dtype)
f = get_ffi_func("mul_<>", dtype, self._s)
f = get_ffi_func("mul_<>", self.dtype, self._s)
if f is None:
return NotImplemented
return wrap_s(f(other))
Expand All @@ -422,40 +412,36 @@ def __mod__(self, other: Any) -> "Series":
if isinstance(other, Series):
return Series._from_pyseries(self._s.rem(other._s))
other = maybe_cast(other, self.dtype)
dtype = date_like_to_physical(self.dtype)
f = get_ffi_func("rem_<>", dtype, self._s)
f = get_ffi_func("rem_<>", self.dtype, self._s)
if f is None:
return NotImplemented
return wrap_s(f(other))

def __rmod__(self, other: Any) -> "Series":
if isinstance(other, Series):
return Series._from_pyseries(other._s.rem(self._s))
dtype = date_like_to_physical(self.dtype)
other = maybe_cast(other, self.dtype)
other = match_dtype(other, dtype)
f = get_ffi_func("rem_<>_rhs", dtype, self._s)
other = match_dtype(other, self.dtype)
f = get_ffi_func("rem_<>_rhs", self.dtype, self._s)
if f is None:
return NotImplemented
return wrap_s(f(other))

def __radd__(self, other: Any) -> "Series":
if isinstance(other, Series):
return Series._from_pyseries(self._s.add(other._s))
dtype = date_like_to_physical(self.dtype)
other = maybe_cast(other, self.dtype)
other = match_dtype(other, dtype)
f = get_ffi_func("add_<>_rhs", dtype, self._s)
other = match_dtype(other, self.dtype)
f = get_ffi_func("add_<>_rhs", self.dtype, self._s)
if f is None:
return NotImplemented
return wrap_s(f(other))

def __rsub__(self, other: Any) -> "Series":
if isinstance(other, Series):
return Series._from_pyseries(other._s.sub(self._s))
dtype = date_like_to_physical(self.dtype)
other = match_dtype(other, dtype)
f = get_ffi_func("sub_<>_rhs", dtype, self._s)
other = match_dtype(other, self.dtype)
f = get_ffi_func("sub_<>_rhs", self.dtype, self._s)
if f is None:
return NotImplemented
return wrap_s(f(other))
Expand All @@ -466,8 +452,7 @@ def __invert__(self) -> "Series":
return NotImplemented

def __rtruediv__(self, other: Any) -> np.ndarray:
primitive = date_like_to_physical(self.dtype)
if self.dtype != primitive or self.is_float():
if self.is_float():
self.__rfloordiv__(other)

if isinstance(other, int):
Expand All @@ -478,19 +463,17 @@ def __rtruediv__(self, other: Any) -> np.ndarray:
def __rfloordiv__(self, other: Any) -> "Series":
if isinstance(other, Series):
return Series._from_pyseries(other._s.div(self._s))
dtype = date_like_to_physical(self.dtype)
other = match_dtype(other, dtype)
f = get_ffi_func("div_<>_rhs", dtype, self._s)
other = match_dtype(other, self.dtype)
f = get_ffi_func("div_<>_rhs", self.dtype, self._s)
if f is None:
return NotImplemented
return wrap_s(f(other))

def __rmul__(self, other: Any) -> "Series":
if isinstance(other, Series):
return Series._from_pyseries(self._s.mul(other._s))
dtype = date_like_to_physical(self.dtype)
other = match_dtype(other, dtype)
f = get_ffi_func("mul_<>", dtype, self._s)
other = match_dtype(other, self.dtype)
f = get_ffi_func("mul_<>", self.dtype, self._s)
if f is None:
return NotImplemented
return wrap_s(f(other))
Expand Down

0 comments on commit 1f85f31

Please sign in to comment.