Skip to content

Commit

Permalink
fix integer division and windows datetime handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 16, 2021
1 parent ad4a931 commit a460916
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
11 changes: 5 additions & 6 deletions py-polars/polars/eager/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,14 +416,13 @@ def __invert__(self) -> "Series":

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

if self.is_float():
out_dtype = self.dtype
else:
out_dtype = Float64
return np.true_divide(other, self, dtype=out_dtype) # type: ignore[call-overload]
if isinstance(other, int):
other = float(other)

return self.cast(pl.Float64).__rfloordiv__(other) # type: ignore

def __rfloordiv__(self, other: Any) -> "Series":
if isinstance(other, Series):
Expand Down
39 changes: 35 additions & 4 deletions py-polars/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,41 @@ impl<'s> FromPyObject<'s> for Wrap<AnyValue<'s>> {
Ok(AnyValue::Utf8(v).into())
} else if let Ok(v) = ob.extract::<bool>() {
Ok(AnyValue::Boolean(v).into())
} else if let Ok(res) = ob.call_method0("timestamp") {
// s to ms
let v = res.extract::<f64>()? as i64;
Ok(AnyValue::Date64(v * 1000).into())
}
else if ob.get_type().name()?.contains("datetime") {
let gil = Python::acquire_gil();
let py = gil.python();

// windows
#[cfg(target_arch = "windows")]
{
let kwargs = PyDict::new(py);
kwargs.set_item("tzinfo", py.None())?;
let dt = ob.call_method("replace", (), Some(kwargs))?;

let pytz = PyModule::import(py, "pytz")?;
let tz = pytz.call_method("timezone", ("UTC", ), None)?;
let kwargs = PyDict::new(py);
kwargs.set_item("is_dst", py.None())?;
let loc_tz = tz.call_method("localize", (dt, ), Some(kwargs))?;
loc_tz.call_method0("timestamp")?;
// s to ms
let v = ts.extract::<f64>()? as i64;
Ok(AnyValue::Date64(v * 1000).into())
}
// unix
#[cfg(not(target_arch = "windows"))]
{
let datetime = PyModule::import(py, "datetime")?;
let timezone = datetime.getattr("timezone")?;
let kwargs = PyDict::new(py);
kwargs.set_item("tzinfo", timezone.getattr("utc")?)?;
let dt = ob.call_method("replace", (), Some(kwargs))?;
let ts = dt.call_method0("timestamp")?;
// s to ms
let v = ts.extract::<f64>()? as i64;
Ok(AnyValue::Date64(v * 1000).into())
}
} else if ob.is_none() {
Ok(AnyValue::Null.into())
} else {
Expand Down

0 comments on commit a460916

Please sign in to comment.