Skip to content

Commit

Permalink
add downcast logic null
Browse files Browse the repository at this point in the history
  • Loading branch information
Romano Vacca committed Sep 30, 2023
1 parent 0577e99 commit 8922d07
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 52 deletions.
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)
}
}
48 changes: 1 addition & 47 deletions crates/polars-core/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,52 +149,6 @@ fn format_object_array(
}
}

macro_rules! format_nullarray {
($f:ident, $length:expr, $dtype:expr, $name:expr, $array_type:expr) => {{
write!(
$f,
"shape: ({},)\n{}: '{}' [{}]\n[\n",
fmt_uint(&$length),
$array_type,
$name,
$dtype
)?;
let limit: usize = {
let limit = std::env::var(FMT_MAX_ROWS)
.as_deref()
.unwrap_or("")
.parse()
.map_or(LIMIT, |n: i64| if n < 0 { $length } else { n as usize });
std::cmp::min(limit, $length.clone())
};
let write_fn = |v, f: &mut Formatter| -> fmt::Result {
write!(f, "\t{}\n", v)?;

Ok(())
};
let v = "null";
if (limit == 0 && $length > 0) || ($length > limit + 1) {
if limit > 0 {
for _i in 0..std::cmp::max((limit / 2), 1) {
write_fn(v, $f)?;
}
}
write!($f, "\t\n")?;
if limit > 1 {
for _i in ($length - (limit + 1) / 2)..$length {
write_fn(v, $f)?;
}
}
} else {
for _i in 0..$length {
write_fn(v, $f)?;
}
}

write!($f, "]")
}};
}

impl<T> Debug for ChunkedArray<T>
where
T: PolarsNumericType,
Expand Down Expand Up @@ -359,7 +313,7 @@ impl Debug for Series {
"Series"
),
DataType::Null => {
format_nullarray!(f, self.len(), "nullarray", self.name(), "Series")
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: 15 additions & 5 deletions py-polars/tests/unit/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,14 +607,24 @@ def test_init_ndarray(monkeypatch: Any) -> None:
assert df2.rows() == [(1.0, 4.0), (2.5, None), (None, 6.5)]


def test_nullarray_print_format() -> None:
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.schema == {"a": pl.Null}
assert df_null.rows() == [(None,), (None,)]
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]

expected_string = "shape: (2,)\nSeries: 'a' [nullarray]\n[\n\tnull\n\tnull\n]"
assert str(df_null["a"]) == expected_string
assert (
str(df_null) == "shape: (2, 1)\n"
"┌──────┐\n"
"│ a │\n"
"│ --- │\n"
"│ null │\n"
"╞══════╡\n"
"│ null │\n"
"│ null │\n"
"└──────┘"
)


def test_init_arrow() -> None:
Expand Down

0 comments on commit 8922d07

Please sign in to comment.