Skip to content

Commit

Permalink
remove deprecated and use nanoseconds as opposed to milliseconds as d…
Browse files Browse the repository at this point in the history
…atatime
  • Loading branch information
ritchie46 committed Dec 17, 2021
1 parent 1acf56d commit 7ccc925
Show file tree
Hide file tree
Showing 25 changed files with 48 additions and 306 deletions.
22 changes: 11 additions & 11 deletions polars/polars-core/src/chunked_array/kernels/temporal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use arrow::array::{ArrayRef, PrimitiveArray};
use arrow::compute::arity::unary;
#[cfg(feature = "dtype-time")]
use arrow::temporal_conversions::time64ns_to_time;
use arrow::temporal_conversions::{date32_to_datetime, timestamp_ms_to_datetime};
use arrow::temporal_conversions::{date32_to_datetime, timestamp_ns_to_datetime};
use chrono::{Datelike, NaiveDate, NaiveDateTime, Timelike};
use std::sync::Arc;

Expand Down Expand Up @@ -98,79 +98,79 @@ to_temporal_unit!(
to_temporal_unit!(
datetime_to_week,
week,
timestamp_ms_to_datetime,
timestamp_ns_to_datetime,
i64,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_weekday,
p_weekday,
timestamp_ms_to_datetime,
timestamp_ns_to_datetime,
i64,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_year,
year,
timestamp_ms_to_datetime,
timestamp_ns_to_datetime,
i64,
ArrowDataType::Int32
);
#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_month,
month,
timestamp_ms_to_datetime,
timestamp_ns_to_datetime,
i64,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_day,
day,
timestamp_ms_to_datetime,
timestamp_ns_to_datetime,
i64,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_hour,
hour,
timestamp_ms_to_datetime,
timestamp_ns_to_datetime,
i64,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_minute,
minute,
timestamp_ms_to_datetime,
timestamp_ns_to_datetime,
i64,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_second,
second,
timestamp_ms_to_datetime,
timestamp_ns_to_datetime,
i64,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_nanosecond,
nanosecond,
timestamp_ms_to_datetime,
timestamp_ns_to_datetime,
i64,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_ordinal,
ordinal,
timestamp_ms_to_datetime,
timestamp_ns_to_datetime,
i64,
ArrowDataType::UInt32
);
Expand Down
3 changes: 3 additions & 0 deletions polars/polars-core/src/chunked_array/temporal/utf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ where
// 2019-04-18T02:45:55
"%FT%H:%M:%S",
// 2019-04-18T02:45:55.555000000
// microseconds
"%FT%H:%M:%S.%6f",
// nanoseconds
"%FT%H:%M:%S.%9f",
] {
if convert(val, fmt).is_ok() {
return Some(fmt);
Expand Down
4 changes: 2 additions & 2 deletions polars/polars-core/src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude::*;

#[cfg(any(feature = "dtype-date", feature = "dtype-datetime"))]
use arrow::temporal_conversions::{date32_to_date, timestamp_ms_to_datetime};
use arrow::temporal_conversions::{date32_to_date, timestamp_ns_to_datetime};
use num::{Num, NumCast};
use std::{
fmt,
Expand Down Expand Up @@ -505,7 +505,7 @@ impl Display for AnyValue<'_> {
#[cfg(feature = "dtype-date")]
AnyValue::Date(v) => write!(f, "{}", date32_to_date(*v)),
#[cfg(feature = "dtype-datetime")]
AnyValue::Datetime(v) => write!(f, "{}", timestamp_ms_to_datetime(*v)),
AnyValue::Datetime(v) => write!(f, "{}", timestamp_ns_to_datetime(*v)),
#[cfg(feature = "dtype-time")]
AnyValue::Time(_) => {
let nt: chrono::NaiveTime = self.into();
Expand Down
1 change: 1 addition & 0 deletions polars/polars-core/src/series/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::chunked_array::cast::cast_chunks;
use crate::chunked_array::object::extension::polars_extension::PolarsExtension;
use crate::prelude::*;
use arrow::compute::cast::utf8_to_large_utf8;
use arrow::temporal_conversions::NANOSECONDS;
use polars_arrow::compute::cast::cast;
use std::convert::TryFrom;

Expand Down
6 changes: 3 additions & 3 deletions polars/polars-core/src/series/implementations/dates_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,18 +436,18 @@ macro_rules! impl_dyn_series {
}

fn cast(&self, data_type: &DataType) -> Result<Series> {
const MS_IN_DAY: i64 = 86400000;
const NS_IN_DAY: i64 = 86400000_000_000;
use DataType::*;
let ca = match (self.dtype(), data_type) {
#[cfg(feature = "dtype-datetime")]
(Date, Datetime) => {
let casted = self.0.cast(data_type)?;
let casted = casted.datetime().unwrap();
return Ok((casted.deref() * MS_IN_DAY).into_date().into_series());
return Ok((casted.deref() * NS_IN_DAY).into_date().into_series());
}
#[cfg(feature = "dtype-date")]
(Datetime, Date) => {
let ca = self.0.deref() / MS_IN_DAY;
let ca = self.0.deref() / NS_IN_DAY;
Cow::Owned(ca)
}
_ => Cow::Borrowed(self.0.deref()),
Expand Down
3 changes: 1 addition & 2 deletions polars/polars-core/src/series/series_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use arrow::array::ArrayRef;
#[cfg(feature = "object")]
use crate::chunked_array::object::PolarsObjectSafe;
use crate::chunked_array::ChunkIdIter;
use arrow::temporal_conversions::MILLISECONDS;
#[cfg(feature = "object")]
use std::any::Any;
use std::borrow::Cow;
Expand Down Expand Up @@ -1022,7 +1021,7 @@ pub trait SeriesTrait:
.unwrap()
.datetime()
.map(|ca| (ca.deref() * 1000)),
DataType::Datetime => self.datetime().map(|ca| ca.deref().clone() / MILLISECONDS),
DataType::Datetime => self.datetime().map(|ca| ca.deref().clone() / 1_000_000),
_ => Err(PolarsError::InvalidOperation(
format!("operation not supported on dtype {:?}", self.dtype()).into(),
)),
Expand Down
7 changes: 2 additions & 5 deletions polars/polars-io/src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,10 @@ where
W: Write,
{
fn new(buffer: W) -> Self {
// 6f because our precision is milliseconds
// no need for 3 traling zeros
// 9f: all nanoseconds
let options = write::SerializeOptions {
// 9f: all nanoseconds
time64_format: Some("%T%.9f".to_string()),
// 6f: all milliseconds
timestamp_format: Some("%FT%H:%M:%S.%6f".to_string()),
timestamp_format: Some("%FT%H:%M:%S.%9f".to_string()),
..Default::default()
};

Expand Down
2 changes: 1 addition & 1 deletion polars/polars-lazy/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ pub fn datetime(
Some(
NaiveDate::from_ymd(y, m, d)
.and_hms_milli(h, mnt, s, ms)
.timestamp_millis(),
.timestamp_nanos(),
)
} else {
None
Expand Down
3 changes: 1 addition & 2 deletions py-polars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ pyo3 = { git = "https://github.com/ghuls/pyo3", branch = "polars_pypy_hasattr" }
parquet = ["polars/parquet"]
pivot = ["polars/pivot"]
ipc = ["polars/ipc"]
downsample = ["polars/downsample"]
is_in = ["polars/is_in"]
json = ["polars/serde", "serde_json"]

default = ["pivot", "json", "parquet", "downsample", "ipc", "is_in", "json", "polars/repeat_by"]
default = ["pivot", "json", "parquet", "ipc", "is_in", "json", "polars/repeat_by"]

[dependencies.polars]
path = "../polars"
Expand Down
1 change: 0 additions & 1 deletion py-polars/docs/source/reference/dataframe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ Manipulation/ selection
DataFrame.hstack
DataFrame.vstack
DataFrame.groupby
DataFrame.downsample
DataFrame.select
DataFrame.with_columns
DataFrame.with_column_renamed
Expand Down
1 change: 0 additions & 1 deletion py-polars/docs/source/reference/expression.rst
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ The following methods are available under the `expr.dt` attribute.
ExprDateTimeNameSpace.minute
ExprDateTimeNameSpace.second
ExprDateTimeNameSpace.nanosecond
ExprDateTimeNameSpace.round
ExprDateTimeNameSpace.to_python_datetime
ExprDateTimeNameSpace.timestamp
ExprDateTimeNameSpace.buckets
Expand Down
1 change: 0 additions & 1 deletion py-polars/docs/source/reference/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ The following methods are available under the `Series.dt` attribute.
DateTimeNameSpace.max
DateTimeNameSpace.median
DateTimeNameSpace.mean
DateTimeNameSpace.round
DateTimeNameSpace.buckets
DateTimeNameSpace.epoch_days
DateTimeNameSpace.epoch_milliseconds
Expand Down
4 changes: 2 additions & 2 deletions py-polars/polars/datatypes_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
UInt32: PySeries.new_opt_u32,
UInt64: PySeries.new_opt_u64,
Date: PySeries.new_opt_i32,
Datetime: PySeries.new_opt_i32,
Time: PySeries.new_opt_i32,
Datetime: PySeries.new_opt_i64,
Time: PySeries.new_opt_i64,
Boolean: PySeries.new_opt_bool,
Utf8: PySeries.new_str,
Object: PySeries.new_object,
Expand Down
22 changes: 0 additions & 22 deletions py-polars/polars/internals/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2653,28 +2653,6 @@ def nanosecond(self) -> Expr:
"""
return wrap_expr(self._pyexpr.nanosecond())

def round(self, rule: str, n: int) -> Expr:
"""
Round the datetime.
Parameters
----------
rule
Units of the downscaling operation.
Any of:
- "month"
- "week"
- "day"
- "hour"
- "minute"
- "second"
n
Number of units (e.g. 5 "day", 15 "minute".
"""
return wrap_expr(self._pyexpr).map(lambda s: s.dt.round(rule, n), None)

def to_python_datetime(self) -> Expr:
"""
Go from Date/Datetime to python DateTime objects
Expand Down

0 comments on commit 7ccc925

Please sign in to comment.