Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rust): Format null arrays in Series #11289

Merged
merged 7 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/polars-core/src/chunked_array/ops/any_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use polars_utils::sync::SyncPtr;
#[cfg(feature = "object")]
use crate::chunked_array::object::extension::polars_extension::PolarsExtension;
use crate::prelude::*;
use crate::series::implementations::null::NullChunked;

#[inline]
#[allow(unused_variables)]
Expand Down Expand Up @@ -282,3 +283,14 @@ impl<T: PolarsObject> ChunkAnyValue for ObjectChunked<T> {
}
}
}

impl ChunkAnyValue for NullChunked {
#[inline]
unsafe fn get_any_value_unchecked(&self, _index: usize) -> AnyValue {
AnyValue::Null
}

fn get_any_value(&self, _index: usize) -> PolarsResult<AnyValue> {
Ok(AnyValue::Null)
}
}
2 changes: 1 addition & 1 deletion crates/polars-core/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ impl Debug for Series {
"Series"
),
DataType::Null => {
writeln!(f, "nullarray; shape: {}", self.len())
format_array!(f, self.null().unwrap(), "null", self.name(), "Series")
},
DataType::Binary => {
format_array!(f, self.binary().unwrap(), "binary", self.name(), "Series")
Expand Down
6 changes: 6 additions & 0 deletions crates/polars-core/src/series/ops/downcast.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::prelude::*;
use crate::series::implementations::null::NullChunked;

macro_rules! unpack_chunked {
($series:expr, $expected:pat => $ca:ty, $name:expr) => {
Expand Down Expand Up @@ -152,4 +153,9 @@ impl Series {
}
unpack_chunked!(self, DataType::Struct(_) => StructChunked, "Struct")
}

/// Unpack to [`ChunkedArray`] of dtype `[DataType::Null]`
pub fn null(&self) -> PolarsResult<&NullChunked> {
unpack_chunked!(self, DataType::Null => NullChunked, "Null")
}
}
20 changes: 20 additions & 0 deletions py-polars/tests/unit/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,26 @@ def test_init_ndarray(monkeypatch: Any) -> None:
assert df2.rows() == [(1.0, 4.0), (2.5, None), (None, 6.5)]


def test_null_array_print_format() -> None:
pa_tbl_null = pa.table({"a": [None, None]})
df_null = pl.from_arrow(pa_tbl_null)
assert df_null.shape == (2, 1)
assert df_null.dtypes == [pl.Null] # type: ignore[union-attr]
assert df_null.rows() == [(None,), (None,)] # type: ignore[union-attr]

assert (
str(df_null) == "shape: (2, 1)\n"
"┌──────┐\n"
"│ a │\n"
"│ --- │\n"
"│ null │\n"
"╞══════╡\n"
"│ null │\n"
"│ null │\n"
"└──────┘"
)


def test_init_arrow() -> None:
# Handle unnamed column
df = pl.DataFrame(pa.table({"a": [1, 2], None: [3, 4]}))
Expand Down