Skip to content

Commit

Permalink
fix date related series fmting
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Oct 19, 2021
1 parent 4e8bb87 commit 3790d41
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 40 deletions.
5 changes: 5 additions & 0 deletions polars/polars-core/src/chunked_array/logical/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ impl LogicalType for DateChunked {
fn dtype(&self) -> &'static DataType {
&DataType::Date
}

#[cfg(feature = "dtype-date")]
fn get_any_value(&self, i: usize) -> AnyValue<'_> {
self.0.get_any_value(i).into_date()
}
}
5 changes: 5 additions & 0 deletions polars/polars-core/src/chunked_array/logical/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ impl LogicalType for DatetimeChunked {
fn dtype(&self) -> &'static DataType {
&DataType::Datetime
}

#[cfg(feature = "dtype-date")]
fn get_any_value(&self, i: usize) -> AnyValue<'_> {
self.0.get_any_value(i).into_date()
}
}
4 changes: 4 additions & 0 deletions polars/polars-core/src/chunked_array/logical/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ impl<K: PolarsDataType, T: PolarsDataType> Logical<K, T> {
pub trait LogicalType {
/// Get data type of ChunkedArray.
fn dtype(&self) -> &'static DataType;

fn get_any_value(&self, _i: usize) -> AnyValue<'_> {
unimplemented!()
}
}

impl<K: PolarsDataType, T: PolarsDataType> Logical<K, T>
Expand Down
5 changes: 5 additions & 0 deletions polars/polars-core/src/chunked_array/logical/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ impl LogicalType for TimeChunked {
fn dtype(&self) -> &'static DataType {
&DataType::Time
}

#[cfg(feature = "dtype-time")]
fn get_any_value(&self, i: usize) -> AnyValue<'_> {
self.0.get_any_value(i).into_time()
}
}
52 changes: 13 additions & 39 deletions polars/polars-core/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,36 +517,23 @@ impl Display for AnyValue<'_> {
}
}

macro_rules! fmt_option {
($opt:expr) => {{
match $opt {
Some(v) => format!("{}", v),
None => "null".to_string(),
}
}};
}

macro_rules! impl_fmt_list {
($self:ident) => {{
match $self.len() {
0 => format!("[]"),
1 => format!("[{}]", fmt_option!($self.get(0))),
2 => format!(
"[{}, {}]",
fmt_option!($self.get(0)),
fmt_option!($self.get(1))
),
1 => format!("[{}]", $self.get_any_value(0)),
2 => format!("[{}, {}]", $self.get_any_value(0), $self.get_any_value(1)),
3 => format!(
"[{}, {}, {}]",
fmt_option!($self.get(0)),
fmt_option!($self.get(1)),
fmt_option!($self.get(2))
$self.get_any_value(0),
$self.get_any_value(1),
$self.get_any_value(2)
),
_ => format!(
"[{}, {}, ... {}]",
fmt_option!($self.get(0)),
fmt_option!($self.get(1)),
fmt_option!($self.get($self.len() - 1))
$self.get_any_value(0),
$self.get_any_value(1),
$self.get_any_value($self.len() - 1)
),
}
}};
Expand Down Expand Up @@ -648,12 +635,11 @@ Series: 'a' [list]
}

#[test]
#[cfg(feature = "dtype-time64-ns")]
fn test_fmt_temporal() {
let s = DateChunked::new_from_opt_slice("Date", &[Some(1), None, Some(3)]);
let s = Int32Chunked::new_from_opt_slice("Date", &[Some(1), None, Some(3)]).into_date();
assert_eq!(
r#"shape: (3,)
Series: 'Date' [Date]
Series: 'Date' [date]
[
1970-01-02
null
Expand All @@ -662,7 +648,8 @@ Series: 'Date' [Date]
format!("{:?}", s.into_series())
);

let s = DatetimeChunked::new_from_opt_slice("", &[Some(1), None, Some(1_000_000_000_000)]);
let s = Int64Chunked::new_from_opt_slice("", &[Some(1), None, Some(1_000_000_000_000)])
.into_date();
assert_eq!(
r#"shape: (3,)
Series: '' [datetime]
Expand All @@ -673,21 +660,8 @@ Series: '' [datetime]
]"#,
format!("{:?}", s.into_series())
);
let s = Time64NanosecondChunked::new_from_slice(
"",
&[1_000_000, 37_800_005_000_000, 86_399_210_000_000],
);
assert_eq!(
r#"shape: (3,)
Series: '' [time64(ns)]
[
00:00:00.001
10:30:00.005
23:59:59.210
]"#,
format!("{:?}", s.into_series())
)
}

#[test]
fn test_fmt_chunkedarray() {
let ca = Int32Chunked::new_from_opt_slice("Date", &[Some(1), None, Some(3)]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ macro_rules! impl_dyn_series {
}

fn get(&self, index: usize) -> AnyValue {
self.0.get_any_value(index).$into_logical()
self.0.get_any_value(index)
}

#[inline]
Expand Down
20 changes: 20 additions & 0 deletions py-polars/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,23 @@ def test_csq_quote_char():
"""

assert pl.read_csv(rolling_stones.encode(), quote_char=None).shape == (9, 3)


def test_date_list_fmt():
df = pl.DataFrame(
{
"mydate": ["2020-01-01", "2020-01-02", "2020-01-05", "2020-01-05"],
"index": [1, 2, 5, 5],
}
)

df = df.with_column(pl.col("mydate").str.strptime(pl.Date, "%Y-%m-%d"))
str(
df.groupby("index", maintain_order=True).agg(pl.col("mydate"))["mydate"]
) == """shape: (3,)
Series: 'mydate' [list]
[
[2020-01-01]
[2020-01-02]
[2020-01-05, 2020-01-05]
]"""

0 comments on commit 3790d41

Please sign in to comment.