Trait polars_core::chunked_array::ops::ChunkAgg
source · pub trait ChunkAgg<T> {
fn sum(&self) -> Option<T> { ... }
fn min(&self) -> Option<T> { ... }
fn max(&self) -> Option<T> { ... }
fn mean(&self) -> Option<f64> { ... }
}
Expand description
Aggregation operations
Provided Methods§
sourcefn sum(&self) -> Option<T>
fn sum(&self) -> Option<T>
Aggregate the sum of the ChunkedArray.
Returns None
if the array is empty or only contains null values.
Examples found in repository?
More examples
src/functions.rs (line 36)
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
pub fn cov_f<T>(a: &ChunkedArray<T>, b: &ChunkedArray<T>) -> Option<T::Native>
where
T: PolarsFloatType,
T::Native: Float,
<T::Native as Simd>::Simd: Add<Output = <T::Native as Simd>::Simd>
+ compute::aggregate::Sum<T::Native>
+ compute::aggregate::SimdOrd<T::Native>,
{
if a.len() != b.len() {
None
} else {
let tmp = (a - a.mean()?) * (b - b.mean()?);
let n = tmp.len() - tmp.null_count();
Some(tmp.sum()? / NumCast::from(n - 1).unwrap())
}
}
/// Compute the covariance between two columns.
pub fn cov_i<T>(a: &ChunkedArray<T>, b: &ChunkedArray<T>) -> Option<f64>
where
T: PolarsIntegerType,
T::Native: ToPrimitive,
<T::Native as Simd>::Simd: Add<Output = <T::Native as Simd>::Simd>
+ compute::aggregate::Sum<T::Native>
+ compute::aggregate::SimdOrd<T::Native>,
{
if a.len() != b.len() {
None
} else {
let a_mean = a.mean()?;
let b_mean = b.mean()?;
let a = a.apply_cast_numeric::<_, Float64Type>(|a| a.to_f64().unwrap() - a_mean);
let b = b.apply_cast_numeric(|b| b.to_f64().unwrap() - b_mean);
let tmp = a * b;
let n = tmp.len() - tmp.null_count();
Some(tmp.sum()? / (n - 1) as f64)
}
}
src/testing.rs (line 43)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
pub fn series_equal_missing(&self, other: &Series) -> bool {
// TODO! remove this? Default behavior already includes equal missing
#[cfg(feature = "timezones")]
{
use crate::datatypes::DataType::Datetime;
if let Datetime(_, tz_lhs) = self.dtype() {
if let Datetime(_, tz_rhs) = other.dtype() {
if tz_lhs != tz_rhs {
return false;
}
} else {
return false;
}
}
}
// differences from Partial::eq in that numerical dtype may be different
self.len() == other.len()
&& self.name() == other.name()
&& self.null_count() == other.null_count()
&& {
let eq = self.equal(other);
match eq {
Ok(b) => b.sum().map(|s| s as usize).unwrap_or(0) == self.len(),
Err(_) => false,
}
}
}
/// Get a pointer to the underlying data of this Series.
/// Can be useful for fast comparisons.
pub fn get_data_ptr(&self) -> usize {
let object = self.0.deref();
// Safety:
// A fat pointer consists of a data ptr and a ptr to the vtable.
// we specifically check that we only transmute &dyn SeriesTrait e.g.
// a trait object, therefore this is sound.
#[allow(clippy::transmute_undefined_repr)]
let (data_ptr, _vtable_ptr) =
unsafe { std::mem::transmute::<&dyn SeriesTrait, (usize, usize)>(object) };
data_ptr
}
}
impl PartialEq for Series {
fn eq(&self, other: &Self) -> bool {
self.len() == other.len()
&& self.field() == other.field()
&& self.null_count() == other.null_count()
&& self
.equal(other)
.unwrap()
.sum()
.map(|s| s as usize)
.unwrap_or(0)
== self.len()
}
src/chunked_array/ops/aggregate.rs (line 136)
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
fn mean(&self) -> Option<f64> {
match self.dtype() {
DataType::Float64 => {
let len = (self.len() - self.null_count()) as f64;
self.sum().map(|v| v.to_f64().unwrap() / len)
}
_ => {
let null_count = self.null_count();
let len = self.len();
if null_count == len {
None
} else {
let mut acc = 0.0;
let len = (len - null_count) as f64;
for arr in self.downcast_iter() {
if arr.null_count() > 0 {
for v in arr.into_iter().flatten() {
// safety
// all these types can be coerced to f64
unsafe {
let val = v.to_f64().unwrap_unchecked();
acc += val
}
}
} else {
for v in arr.values().as_slice() {
// safety
// all these types can be coerced to f64
unsafe {
let val = v.to_f64().unwrap_unchecked();
acc += val
}
}
}
}
Some(acc / len)
}
}
}
}
}
/// helper
fn quantile_idx(
quantile: f64,
length: usize,
null_count: usize,
interpol: QuantileInterpolOptions,
) -> (i64, f64, i64) {
let mut base_idx = match interpol {
QuantileInterpolOptions::Nearest => {
(((length - null_count) as f64) * quantile + null_count as f64) as i64
}
QuantileInterpolOptions::Lower
| QuantileInterpolOptions::Midpoint
| QuantileInterpolOptions::Linear => {
(((length - null_count) as f64 - 1.0) * quantile + null_count as f64) as i64
}
QuantileInterpolOptions::Higher => {
(((length - null_count) as f64 - 1.0) * quantile + null_count as f64).ceil() as i64
}
};
base_idx = base_idx.clamp(0, (length - 1) as i64);
let float_idx = ((length - null_count) as f64 - 1.0) * quantile + null_count as f64;
let top_idx = f64::ceil(float_idx) as i64;
(base_idx, float_idx, top_idx)
}
/// helper
fn linear_interpol<T: Float>(bounds: &[Option<T>], idx: i64, float_idx: f64) -> Option<T> {
if bounds[0] == bounds[1] {
Some(bounds[0].unwrap())
} else {
let proportion: T = T::from(float_idx).unwrap() - T::from(idx).unwrap();
Some(proportion * (bounds[1].unwrap() - bounds[0].unwrap()) + bounds[0].unwrap())
}
}
impl<T> ChunkQuantile<f64> for ChunkedArray<T>
where
T: PolarsIntegerType,
T::Native: Ord,
<T::Native as Simd>::Simd: Add<Output = <T::Native as Simd>::Simd>
+ compute::aggregate::Sum<T::Native>
+ compute::aggregate::SimdOrd<T::Native>,
{
fn quantile(
&self,
quantile: f64,
interpol: QuantileInterpolOptions,
) -> PolarsResult<Option<f64>> {
if !(0.0..=1.0).contains(&quantile) {
return Err(PolarsError::ComputeError(
"quantile should be between 0.0 and 1.0".into(),
));
}
let null_count = self.null_count();
let length = self.len();
if null_count == length {
return Ok(None);
}
let (idx, float_idx, top_idx) = quantile_idx(quantile, length, null_count, interpol);
let opt = match interpol {
QuantileInterpolOptions::Midpoint => {
if top_idx == idx {
ChunkSort::sort(self, false)
.slice(idx, 1)
.apply_cast_numeric::<_, Float64Type>(|value| value.to_f64().unwrap())
.into_iter()
.next()
.flatten()
} else {
let bounds: Vec<Option<f64>> = ChunkSort::sort(self, false)
.slice(idx, 2)
.apply_cast_numeric::<_, Float64Type>(|value| value.to_f64().unwrap())
.into_iter()
.collect();
Some((bounds[0].unwrap() + bounds[1].unwrap()) / 2.0f64)
}
}
QuantileInterpolOptions::Linear => {
if top_idx == idx {
ChunkSort::sort(self, false)
.slice(idx, 1)
.apply_cast_numeric::<_, Float64Type>(|value| value.to_f64().unwrap())
.into_iter()
.next()
.flatten()
} else {
let bounds: Vec<Option<f64>> = ChunkSort::sort(self, false)
.slice(idx, 2)
.apply_cast_numeric::<_, Float64Type>(|value| value.to_f64().unwrap())
.into_iter()
.collect();
linear_interpol(&bounds, idx, float_idx)
}
}
_ => ChunkSort::sort(self, false)
.slice(idx, 1)
.apply_cast_numeric::<_, Float64Type>(|value| value.to_f64().unwrap())
.into_iter()
.next()
.flatten(),
};
Ok(opt)
}
fn median(&self) -> Option<f64> {
self.quantile(0.5, QuantileInterpolOptions::Linear).unwrap() // unwrap fine since quantile in range
}
}
impl ChunkQuantile<f32> for Float32Chunked {
fn quantile(
&self,
quantile: f64,
interpol: QuantileInterpolOptions,
) -> PolarsResult<Option<f32>> {
if !(0.0..=1.0).contains(&quantile) {
return Err(PolarsError::ComputeError(
"quantile should be between 0.0 and 1.0".into(),
));
}
let null_count = self.null_count();
let length = self.len();
if null_count == length {
return Ok(None);
}
let (idx, float_idx, top_idx) = quantile_idx(quantile, length, null_count, interpol);
let opt = match interpol {
QuantileInterpolOptions::Midpoint => {
if top_idx == idx {
ChunkSort::sort(self, false)
.slice(idx, 1)
.apply_cast_numeric::<_, Float32Type>(|value| value.to_f32().unwrap())
.into_iter()
.next()
.flatten()
} else {
let bounds: Vec<Option<f32>> = ChunkSort::sort(self, false)
.slice(idx, 2)
.apply_cast_numeric::<_, Float32Type>(|value| value.to_f32().unwrap())
.into_iter()
.collect();
Some((bounds[0].unwrap() + bounds[1].unwrap()) / 2.0f32)
}
}
QuantileInterpolOptions::Linear => {
if top_idx == idx {
ChunkSort::sort(self, false)
.slice(idx, 1)
.apply_cast_numeric::<_, Float32Type>(|value| value.to_f32().unwrap())
.into_iter()
.next()
.flatten()
} else {
let bounds: Vec<Option<f32>> = ChunkSort::sort(self, false)
.slice(idx, 2)
.apply_cast_numeric::<_, Float32Type>(|value| value.to_f32().unwrap())
.into_iter()
.collect();
linear_interpol(&bounds, idx, float_idx)
}
}
_ => ChunkSort::sort(self, false)
.slice(idx, 1)
.apply_cast_numeric::<_, Float32Type>(|value| value.to_f32().unwrap())
.into_iter()
.next()
.flatten(),
};
Ok(opt)
}
fn median(&self) -> Option<f32> {
self.quantile(0.5, QuantileInterpolOptions::Linear).unwrap() // unwrap fine since quantile in range
}
}
impl ChunkQuantile<f64> for Float64Chunked {
fn quantile(
&self,
quantile: f64,
interpol: QuantileInterpolOptions,
) -> PolarsResult<Option<f64>> {
if !(0.0..=1.0).contains(&quantile) {
return Err(PolarsError::ComputeError(
"quantile should be between 0.0 and 1.0".into(),
));
}
let null_count = self.null_count();
let length = self.len();
if null_count == length {
return Ok(None);
}
let (idx, float_idx, top_idx) = quantile_idx(quantile, length, null_count, interpol);
let opt = match interpol {
QuantileInterpolOptions::Midpoint => {
if top_idx == idx {
ChunkSort::sort(self, false)
.slice(idx, 1)
.apply_cast_numeric::<_, Float64Type>(|value| value.to_f64().unwrap())
.into_iter()
.next()
.flatten()
} else {
let bounds: Vec<Option<f64>> = ChunkSort::sort(self, false)
.slice(idx, 2)
.apply_cast_numeric::<_, Float64Type>(|value| value.to_f64().unwrap())
.into_iter()
.collect();
Some((bounds[0].unwrap() + bounds[1].unwrap()) / 2.0f64)
}
}
QuantileInterpolOptions::Linear => {
if top_idx == idx {
ChunkSort::sort(self, false)
.slice(idx, 1)
.apply_cast_numeric::<_, Float64Type>(|value| value.to_f64().unwrap())
.into_iter()
.next()
.flatten()
} else {
let bounds: Vec<Option<f64>> = ChunkSort::sort(self, false)
.slice(idx, 2)
.apply_cast_numeric::<_, Float64Type>(|value| value.to_f64().unwrap())
.into_iter()
.collect();
linear_interpol(&bounds, idx, float_idx)
}
}
_ => ChunkSort::sort(self, false)
.slice(idx, 1)
.apply_cast_numeric::<_, Float64Type>(|value| value.to_f64().unwrap())
.into_iter()
.next()
.flatten(),
};
Ok(opt)
}
fn median(&self) -> Option<f64> {
self.quantile(0.5, QuantileInterpolOptions::Linear).unwrap() // unwrap fine since quantile in range
}
}
impl ChunkQuantile<String> for Utf8Chunked {}
impl ChunkQuantile<Series> for ListChunked {}
#[cfg(feature = "object")]
impl<T: PolarsObject> ChunkQuantile<Series> for ObjectChunked<T> {}
impl ChunkQuantile<bool> for BooleanChunked {}
impl<T> ChunkVar<f64> for ChunkedArray<T>
where
T: PolarsIntegerType,
<T::Native as Simd>::Simd: Add<Output = <T::Native as Simd>::Simd>
+ compute::aggregate::Sum<T::Native>
+ compute::aggregate::SimdOrd<T::Native>,
{
fn var(&self, ddof: u8) -> Option<f64> {
if self.len() == 1 {
return Some(0.0);
}
let n_values = self.len() - self.null_count();
if ddof as usize > n_values {
return None;
}
let n_values = n_values as f64;
let mean = self.mean()?;
let squared = self.apply_cast_numeric::<_, Float64Type>(|value| {
let tmp = value.to_f64().unwrap() - mean;
tmp * tmp
});
// Note, this is similar behavior to numpy if DDOF=1.
// in statistics DDOF often = 1.
// this last step is similar to mean, only now instead of 1/n it is 1/(n-1)
squared.sum().map(|sum| sum / (n_values - ddof as f64))
}
fn std(&self, ddof: u8) -> Option<f64> {
self.var(ddof).map(|var| var.sqrt())
}
}
impl ChunkVar<f32> for Float32Chunked {
fn var(&self, ddof: u8) -> Option<f32> {
if self.len() == 1 {
return Some(0.0);
}
let n_values = self.len() - self.null_count();
if ddof as usize > n_values {
return None;
}
let n_values = n_values as f32;
let mean = self.mean()? as f32;
let squared = self.apply(|value| {
let tmp = value - mean;
tmp * tmp
});
squared.sum().map(|sum| sum / (n_values - ddof as f32))
}
fn std(&self, ddof: u8) -> Option<f32> {
self.var(ddof).map(|var| var.sqrt())
}
}
impl ChunkVar<f64> for Float64Chunked {
fn var(&self, ddof: u8) -> Option<f64> {
if self.len() == 1 {
return Some(0.0);
}
let n_values = self.len() - self.null_count();
if ddof as usize > n_values {
return None;
}
let n_values = n_values as f64;
let mean = self.mean()?;
let squared = self.apply(|value| {
let tmp = value - mean;
tmp * tmp
});
squared.sum().map(|sum| sum / (n_values - ddof as f64))
}
fn std(&self, ddof: u8) -> Option<f64> {
self.var(ddof).map(|var| var.sqrt())
}
}
impl ChunkVar<String> for Utf8Chunked {}
impl ChunkVar<Series> for ListChunked {}
#[cfg(feature = "object")]
impl<T: PolarsObject> ChunkVar<Series> for ObjectChunked<T> {}
impl ChunkVar<bool> for BooleanChunked {}
/// Booleans are casted to 1 or 0.
impl ChunkAgg<IdxSize> for BooleanChunked {
/// Returns `None` if the array is empty or only contains null values.
fn sum(&self) -> Option<IdxSize> {
if self.is_empty() {
None
} else {
Some(
self.downcast_iter()
.map(|arr| match arr.validity() {
Some(validity) => {
(arr.len() - (validity & arr.values()).unset_bits()) as IdxSize
}
None => (arr.len() - arr.values().unset_bits()) as IdxSize,
})
.sum(),
)
}
}
fn min(&self) -> Option<IdxSize> {
if self.is_empty() {
return None;
}
if self.all() {
Some(1)
} else {
Some(0)
}
}
fn max(&self) -> Option<IdxSize> {
if self.is_empty() {
return None;
}
if self.any() {
Some(1)
} else {
Some(0)
}
}
fn mean(&self) -> Option<f64> {
self.sum()
.map(|sum| sum as f64 / (self.len() - self.null_count()) as f64)
}
}
// Needs the same trait bounds as the implementation of ChunkedArray<T> of dyn Series
impl<T> ChunkAggSeries for ChunkedArray<T>
where
T: PolarsNumericType,
<T::Native as Simd>::Simd: Add<Output = <T::Native as Simd>::Simd>
+ compute::aggregate::Sum<T::Native>
+ compute::aggregate::SimdOrd<T::Native>,
ChunkedArray<T>: IntoSeries,
{
fn sum_as_series(&self) -> Series {
let v = self.sum();
let mut ca: ChunkedArray<T> = [v].iter().copied().collect();
ca.rename(self.name());
ca.into_series()
}
fn max_as_series(&self) -> Series {
let v = self.max();
let mut ca: ChunkedArray<T> = [v].iter().copied().collect();
ca.rename(self.name());
ca.into_series()
}
fn min_as_series(&self) -> Series {
let v = self.min();
let mut ca: ChunkedArray<T> = [v].iter().copied().collect();
ca.rename(self.name());
ca.into_series()
}
fn prod_as_series(&self) -> Series {
let mut prod = None;
for opt_v in self.into_iter() {
match (prod, opt_v) {
(_, None) => return Self::full_null(self.name(), 1).into_series(),
(None, Some(v)) => prod = Some(v),
(Some(p), Some(v)) => prod = Some(p * v),
}
}
Self::from_slice_options(self.name(), &[prod]).into_series()
}
}
macro_rules! impl_as_series {
($self:expr, $agg:ident, $ty: ty) => {{
let v = $self.$agg();
let mut ca: $ty = [v].iter().copied().collect();
ca.rename($self.name());
ca.into_series()
}};
($self:expr, $agg:ident, $arg:expr, $ty: ty) => {{
let v = $self.$agg($arg);
let mut ca: $ty = [v].iter().copied().collect();
ca.rename($self.name());
ca.into_series()
}};
}
impl<T> VarAggSeries for ChunkedArray<T>
where
T: PolarsIntegerType,
<T::Native as Simd>::Simd: Add<Output = <T::Native as Simd>::Simd>
+ compute::aggregate::Sum<T::Native>
+ compute::aggregate::SimdOrd<T::Native>,
{
fn var_as_series(&self, ddof: u8) -> Series {
impl_as_series!(self, var, ddof, Float64Chunked)
}
fn std_as_series(&self, ddof: u8) -> Series {
impl_as_series!(self, std, ddof, Float64Chunked)
}
}
impl VarAggSeries for Float32Chunked {
fn var_as_series(&self, ddof: u8) -> Series {
impl_as_series!(self, var, ddof, Float32Chunked)
}
fn std_as_series(&self, ddof: u8) -> Series {
impl_as_series!(self, std, ddof, Float32Chunked)
}
}
impl VarAggSeries for Float64Chunked {
fn var_as_series(&self, ddof: u8) -> Series {
impl_as_series!(self, var, ddof, Float64Chunked)
}
fn std_as_series(&self, ddof: u8) -> Series {
impl_as_series!(self, std, ddof, Float64Chunked)
}
}
macro_rules! impl_quantile_as_series {
($self:expr, $agg:ident, $ty: ty, $qtl:expr, $opt:expr) => {{
let v = $self.$agg($qtl, $opt)?;
let mut ca: $ty = [v].iter().copied().collect();
ca.rename($self.name());
Ok(ca.into_series())
}};
}
impl<T> QuantileAggSeries for ChunkedArray<T>
where
T: PolarsIntegerType,
T::Native: Ord,
<T::Native as Simd>::Simd: Add<Output = <T::Native as Simd>::Simd>
+ compute::aggregate::Sum<T::Native>
+ compute::aggregate::SimdOrd<T::Native>,
{
fn quantile_as_series(
&self,
quantile: f64,
interpol: QuantileInterpolOptions,
) -> PolarsResult<Series> {
impl_quantile_as_series!(self, quantile, Float64Chunked, quantile, interpol)
}
fn median_as_series(&self) -> Series {
impl_as_series!(self, median, Float64Chunked)
}
}
impl QuantileAggSeries for Float32Chunked {
fn quantile_as_series(
&self,
quantile: f64,
interpol: QuantileInterpolOptions,
) -> PolarsResult<Series> {
impl_quantile_as_series!(self, quantile, Float32Chunked, quantile, interpol)
}
fn median_as_series(&self) -> Series {
impl_as_series!(self, median, Float32Chunked)
}
}
impl QuantileAggSeries for Float64Chunked {
fn quantile_as_series(
&self,
quantile: f64,
interpol: QuantileInterpolOptions,
) -> PolarsResult<Series> {
impl_quantile_as_series!(self, quantile, Float64Chunked, quantile, interpol)
}
fn median_as_series(&self) -> Series {
impl_as_series!(self, median, Float64Chunked)
}
}
impl ChunkAggSeries for BooleanChunked {
fn sum_as_series(&self) -> Series {
let v = ChunkAgg::sum(self);
let mut ca: IdxCa = [v].iter().copied().collect();
ca.rename(self.name());
ca.into_series()
}
src/frame/groupby/aggregations/mod.rs (line 636)
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
pub(crate) unsafe fn agg_sum(&self, groups: &GroupsProxy) -> Series {
match groups {
GroupsProxy::Idx(groups) => _agg_helper_idx::<T, _>(groups, |(first, idx)| {
debug_assert!(idx.len() <= self.len());
if idx.is_empty() {
None
} else if idx.len() == 1 {
self.get(first as usize)
} else {
match (self.has_validity(), self.chunks.len()) {
(false, 1) => Some({
take_agg_no_null_primitive_iter_unchecked(
self.downcast_iter().next().unwrap(),
idx.iter().map(|i| *i as usize),
|a, b| a + b,
T::Native::zero(),
)
}),
(_, 1) => take_agg_primitive_iter_unchecked::<T::Native, _, _>(
self.downcast_iter().next().unwrap(),
idx.iter().map(|i| *i as usize),
|a, b| a + b,
T::Native::zero(),
idx.len() as IdxSize,
),
_ => {
let take = { self.take_unchecked(idx.into()) };
take.sum()
}
}
}
}),
GroupsProxy::Slice { groups, .. } => {
if _use_rolling_kernels(groups, self.chunks()) {
let arr = self.downcast_iter().next().unwrap();
let values = arr.values().as_slice();
let offset_iter = groups.iter().map(|[first, len]| (*first, *len));
let arr = match arr.validity() {
None => _rolling_apply_agg_window_no_nulls::<SumWindow<_>, _, _>(
values,
offset_iter,
),
Some(validity) => _rolling_apply_agg_window_nulls::<
rolling::nulls::SumWindow<_>,
_,
_,
>(values, validity, offset_iter),
};
Self::from_chunks("", vec![arr]).into_series()
} else {
_agg_helper_slice::<T, _>(groups, |[first, len]| {
debug_assert!(len <= self.len() as IdxSize);
match len {
0 => None,
1 => self.get(first as usize),
_ => {
let arr_group = _slice_from_offsets(self, first, len);
arr_group.sum()
}
}
})
}
}
}
}
sourcefn min(&self) -> Option<T>
fn min(&self) -> Option<T>
Examples found in repository?
src/chunked_array/ops/aggregate.rs (line 604)
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
fn min_as_series(&self) -> Series {
let v = self.min();
let mut ca: ChunkedArray<T> = [v].iter().copied().collect();
ca.rename(self.name());
ca.into_series()
}
fn prod_as_series(&self) -> Series {
let mut prod = None;
for opt_v in self.into_iter() {
match (prod, opt_v) {
(_, None) => return Self::full_null(self.name(), 1).into_series(),
(None, Some(v)) => prod = Some(v),
(Some(p), Some(v)) => prod = Some(p * v),
}
}
Self::from_slice_options(self.name(), &[prod]).into_series()
}
}
macro_rules! impl_as_series {
($self:expr, $agg:ident, $ty: ty) => {{
let v = $self.$agg();
let mut ca: $ty = [v].iter().copied().collect();
ca.rename($self.name());
ca.into_series()
}};
($self:expr, $agg:ident, $arg:expr, $ty: ty) => {{
let v = $self.$agg($arg);
let mut ca: $ty = [v].iter().copied().collect();
ca.rename($self.name());
ca.into_series()
}};
}
impl<T> VarAggSeries for ChunkedArray<T>
where
T: PolarsIntegerType,
<T::Native as Simd>::Simd: Add<Output = <T::Native as Simd>::Simd>
+ compute::aggregate::Sum<T::Native>
+ compute::aggregate::SimdOrd<T::Native>,
{
fn var_as_series(&self, ddof: u8) -> Series {
impl_as_series!(self, var, ddof, Float64Chunked)
}
fn std_as_series(&self, ddof: u8) -> Series {
impl_as_series!(self, std, ddof, Float64Chunked)
}
}
impl VarAggSeries for Float32Chunked {
fn var_as_series(&self, ddof: u8) -> Series {
impl_as_series!(self, var, ddof, Float32Chunked)
}
fn std_as_series(&self, ddof: u8) -> Series {
impl_as_series!(self, std, ddof, Float32Chunked)
}
}
impl VarAggSeries for Float64Chunked {
fn var_as_series(&self, ddof: u8) -> Series {
impl_as_series!(self, var, ddof, Float64Chunked)
}
fn std_as_series(&self, ddof: u8) -> Series {
impl_as_series!(self, std, ddof, Float64Chunked)
}
}
macro_rules! impl_quantile_as_series {
($self:expr, $agg:ident, $ty: ty, $qtl:expr, $opt:expr) => {{
let v = $self.$agg($qtl, $opt)?;
let mut ca: $ty = [v].iter().copied().collect();
ca.rename($self.name());
Ok(ca.into_series())
}};
}
impl<T> QuantileAggSeries for ChunkedArray<T>
where
T: PolarsIntegerType,
T::Native: Ord,
<T::Native as Simd>::Simd: Add<Output = <T::Native as Simd>::Simd>
+ compute::aggregate::Sum<T::Native>
+ compute::aggregate::SimdOrd<T::Native>,
{
fn quantile_as_series(
&self,
quantile: f64,
interpol: QuantileInterpolOptions,
) -> PolarsResult<Series> {
impl_quantile_as_series!(self, quantile, Float64Chunked, quantile, interpol)
}
fn median_as_series(&self) -> Series {
impl_as_series!(self, median, Float64Chunked)
}
}
impl QuantileAggSeries for Float32Chunked {
fn quantile_as_series(
&self,
quantile: f64,
interpol: QuantileInterpolOptions,
) -> PolarsResult<Series> {
impl_quantile_as_series!(self, quantile, Float32Chunked, quantile, interpol)
}
fn median_as_series(&self) -> Series {
impl_as_series!(self, median, Float32Chunked)
}
}
impl QuantileAggSeries for Float64Chunked {
fn quantile_as_series(
&self,
quantile: f64,
interpol: QuantileInterpolOptions,
) -> PolarsResult<Series> {
impl_quantile_as_series!(self, quantile, Float64Chunked, quantile, interpol)
}
fn median_as_series(&self) -> Series {
impl_as_series!(self, median, Float64Chunked)
}
}
impl ChunkAggSeries for BooleanChunked {
fn sum_as_series(&self) -> Series {
let v = ChunkAgg::sum(self);
let mut ca: IdxCa = [v].iter().copied().collect();
ca.rename(self.name());
ca.into_series()
}
fn max_as_series(&self) -> Series {
let v = ChunkAgg::max(self);
let mut ca: IdxCa = [v].iter().copied().collect();
ca.rename(self.name());
ca.into_series()
}
fn min_as_series(&self) -> Series {
let v = ChunkAgg::min(self);
let mut ca: IdxCa = [v].iter().copied().collect();
ca.rename(self.name());
ca.into_series()
}
More examples
src/frame/groupby/aggregations/mod.rs (line 234)
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
pub(crate) unsafe fn agg_min(&self, groups: &GroupsProxy) -> Series {
// faster paths
match (self.is_sorted2(), self.null_count()) {
(IsSorted::Ascending, 0) => {
return self.clone().into_series().agg_first(groups);
}
(IsSorted::Descending, 0) => {
return self.clone().into_series().agg_last(groups);
}
_ => {}
}
match groups {
GroupsProxy::Idx(groups) => _agg_helper_idx_bool(groups, |(first, idx)| {
debug_assert!(idx.len() <= self.len());
if idx.is_empty() {
None
} else if idx.len() == 1 {
self.get(first as usize)
} else {
// TODO! optimize this
// can just check if any is false and early stop
let take = { self.take_unchecked(idx.into()) };
take.min().map(|v| v == 1)
}
}),
GroupsProxy::Slice {
groups: groups_slice,
..
} => _agg_helper_slice_bool(groups_slice, |[first, len]| {
debug_assert!(len <= self.len() as IdxSize);
match len {
0 => None,
1 => self.get(first as usize),
_ => {
let arr_group = _slice_from_offsets(self, first, len);
arr_group.min().map(|v| v == 1)
}
}
}),
}
}
pub(crate) unsafe fn agg_max(&self, groups: &GroupsProxy) -> Series {
// faster paths
match (self.is_sorted2(), self.null_count()) {
(IsSorted::Ascending, 0) => {
return self.clone().into_series().agg_last(groups);
}
(IsSorted::Descending, 0) => {
return self.clone().into_series().agg_first(groups);
}
_ => {}
}
match groups {
GroupsProxy::Idx(groups) => _agg_helper_idx_bool(groups, |(first, idx)| {
debug_assert!(idx.len() <= self.len());
if idx.is_empty() {
None
} else if idx.len() == 1 {
self.get(first as usize)
} else {
// TODO! optimize this
// can just check if any is true and early stop
let take = { self.take_unchecked(idx.into()) };
take.max().map(|v| v == 1)
}
}),
GroupsProxy::Slice {
groups: groups_slice,
..
} => _agg_helper_slice_bool(groups_slice, |[first, len]| {
debug_assert!(len <= self.len() as IdxSize);
match len {
0 => None,
1 => self.get(first as usize),
_ => {
let arr_group = _slice_from_offsets(self, first, len);
arr_group.max().map(|v| v == 1)
}
}
}),
}
}
pub(crate) unsafe fn agg_sum(&self, groups: &GroupsProxy) -> Series {
self.cast(&IDX_DTYPE).unwrap().agg_sum(groups)
}
}
impl Utf8Chunked {
#[allow(clippy::needless_lifetimes)]
pub(crate) unsafe fn agg_min<'a>(&'a self, groups: &GroupsProxy) -> Series {
// faster paths
match (&self.is_sorted2(), &self.null_count()) {
(IsSorted::Ascending, 0) => {
return self.clone().into_series().agg_first(groups);
}
(IsSorted::Descending, 0) => {
return self.clone().into_series().agg_last(groups);
}
_ => {}
}
match groups {
GroupsProxy::Idx(groups) => {
let ca_self = self.rechunk();
let arr = ca_self.downcast_iter().next().unwrap();
_agg_helper_idx_utf8(groups, |(first, idx)| {
debug_assert!(idx.len() <= ca_self.len());
if idx.is_empty() {
None
} else if idx.len() == 1 {
ca_self.get(first as usize)
} else if self.null_count() == 0 {
take_agg_utf8_iter_unchecked_no_null(
arr,
indexes_to_usizes(idx),
|acc, v| if acc < v { acc } else { v },
)
} else {
take_agg_utf8_iter_unchecked(
arr,
indexes_to_usizes(idx),
|acc, v| if acc < v { acc } else { v },
idx.len() as IdxSize,
)
}
})
}
GroupsProxy::Slice {
groups: groups_slice,
..
} => _agg_helper_slice_utf8(groups_slice, |[first, len]| {
debug_assert!(len <= self.len() as IdxSize);
match len {
0 => None,
1 => self.get(first as usize),
_ => {
let arr_group = _slice_from_offsets(self, first, len);
let borrowed = arr_group.min_str();
// Safety:
// The borrowed has `arr_group`s lifetime, but it actually points to data
// hold by self. Here we tell the compiler that.
unsafe { std::mem::transmute::<Option<&str>, Option<&'a str>>(borrowed) }
}
}
}),
}
}
#[allow(clippy::needless_lifetimes)]
pub(crate) unsafe fn agg_max<'a>(&'a self, groups: &GroupsProxy) -> Series {
// faster paths
match (self.is_sorted2(), self.null_count()) {
(IsSorted::Ascending, 0) => {
return self.clone().into_series().agg_last(groups);
}
(IsSorted::Descending, 0) => {
return self.clone().into_series().agg_first(groups);
}
_ => {}
}
match groups {
GroupsProxy::Idx(groups) => {
let ca_self = self.rechunk();
let arr = ca_self.downcast_iter().next().unwrap();
_agg_helper_idx_utf8(groups, |(first, idx)| {
debug_assert!(idx.len() <= self.len());
if idx.is_empty() {
None
} else if idx.len() == 1 {
ca_self.get(first as usize)
} else if ca_self.null_count() == 0 {
take_agg_utf8_iter_unchecked_no_null(
arr,
indexes_to_usizes(idx),
|acc, v| if acc > v { acc } else { v },
)
} else {
take_agg_utf8_iter_unchecked(
arr,
indexes_to_usizes(idx),
|acc, v| if acc > v { acc } else { v },
idx.len() as IdxSize,
)
}
})
}
GroupsProxy::Slice {
groups: groups_slice,
..
} => _agg_helper_slice_utf8(groups_slice, |[first, len]| {
debug_assert!(len <= self.len() as IdxSize);
match len {
0 => None,
1 => self.get(first as usize),
_ => {
let arr_group = _slice_from_offsets(self, first, len);
let borrowed = arr_group.max_str();
// Safety:
// The borrowed has `arr_group`s lifetime, but it actually points to data
// hold by self. Here we tell the compiler that.
unsafe { std::mem::transmute::<Option<&str>, Option<&'a str>>(borrowed) }
}
}
}),
}
}
}
#[inline(always)]
fn take_min<T: PartialOrd>(a: T, b: T) -> T {
if a < b {
a
} else {
b
}
}
#[inline(always)]
fn take_max<T: PartialOrd>(a: T, b: T) -> T {
if a > b {
a
} else {
b
}
}
impl<T> ChunkedArray<T>
where
T: PolarsNumericType + Sync,
T::Native:
NativeType + PartialOrd + Num + NumCast + Zero + Simd + Bounded + std::iter::Sum<T::Native>,
<T::Native as Simd>::Simd: std::ops::Add<Output = <T::Native as Simd>::Simd>
+ arrow::compute::aggregate::Sum<T::Native>
+ arrow::compute::aggregate::SimdOrd<T::Native>,
ChunkedArray<T>: IntoSeries,
{
pub(crate) unsafe fn agg_min(&self, groups: &GroupsProxy) -> Series {
// faster paths
match (self.is_sorted2(), self.null_count()) {
(IsSorted::Ascending, 0) => {
return self.clone().into_series().agg_first(groups);
}
(IsSorted::Descending, 0) => {
return self.clone().into_series().agg_last(groups);
}
_ => {}
}
match groups {
GroupsProxy::Idx(groups) => _agg_helper_idx::<T, _>(groups, |(first, idx)| {
debug_assert!(idx.len() <= self.len());
if idx.is_empty() {
None
} else if idx.len() == 1 {
self.get(first as usize)
} else {
match (self.has_validity(), self.chunks.len()) {
(false, 1) => Some(take_agg_no_null_primitive_iter_unchecked(
self.downcast_iter().next().unwrap(),
idx.iter().map(|i| *i as usize),
take_min,
T::Native::max_value(),
)),
(_, 1) => take_agg_primitive_iter_unchecked::<T::Native, _, _>(
self.downcast_iter().next().unwrap(),
idx.iter().map(|i| *i as usize),
take_min,
T::Native::max_value(),
idx.len() as IdxSize,
),
_ => {
let take = { self.take_unchecked(idx.into()) };
take.min()
}
}
}
}),
GroupsProxy::Slice {
groups: groups_slice,
..
} => {
if _use_rolling_kernels(groups_slice, self.chunks()) {
let arr = self.downcast_iter().next().unwrap();
let values = arr.values().as_slice();
let offset_iter = groups_slice.iter().map(|[first, len]| (*first, *len));
let arr = match arr.validity() {
None => _rolling_apply_agg_window_no_nulls::<MinWindow<_>, _, _>(
values,
offset_iter,
),
Some(validity) => _rolling_apply_agg_window_nulls::<
rolling::nulls::MinWindow<_>,
_,
_,
>(values, validity, offset_iter),
};
Self::from_chunks("", vec![arr]).into_series()
} else {
_agg_helper_slice::<T, _>(groups_slice, |[first, len]| {
debug_assert!(len <= self.len() as IdxSize);
match len {
0 => None,
1 => self.get(first as usize),
_ => {
let arr_group = _slice_from_offsets(self, first, len);
arr_group.min()
}
}
})
}
}
}
}
src/chunked_array/ops/fill_null.rs (line 253)
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
fn fill_null(&self, strategy: FillNullStrategy) -> PolarsResult<Self> {
// nothing to fill
if !self.has_validity() {
return Ok(self.clone());
}
let mut ca = match strategy {
FillNullStrategy::Forward(None) => fill_forward(self),
FillNullStrategy::Forward(Some(limit)) => fill_forward_limit(self, limit),
FillNullStrategy::Backward(None) => fill_backward(self),
FillNullStrategy::Backward(Some(limit)) => fill_backward_limit(self, limit),
FillNullStrategy::Min => {
self.fill_null_with_values(self.min().ok_or_else(|| {
PolarsError::ComputeError("Could not determine fill value".into())
})?)?
}
FillNullStrategy::Max => {
self.fill_null_with_values(self.max().ok_or_else(|| {
PolarsError::ComputeError("Could not determine fill value".into())
})?)?
}
FillNullStrategy::Mean => self.fill_null_with_values(
self.mean()
.map(|v| NumCast::from(v).unwrap())
.ok_or_else(|| {
PolarsError::ComputeError("Could not determine fill value".into())
})?,
)?,
FillNullStrategy::One => return self.fill_null_with_values(One::one()),
FillNullStrategy::Zero => return self.fill_null_with_values(Zero::zero()),
FillNullStrategy::MinBound => return self.fill_null_with_values(Bounded::min_value()),
FillNullStrategy::MaxBound => return self.fill_null_with_values(Bounded::max_value()),
};
ca.rename(self.name());
Ok(ca)
}
}
impl<T> ChunkFillNullValue<T::Native> for ChunkedArray<T>
where
T: PolarsNumericType,
{
fn fill_null_with_values(&self, value: T::Native) -> PolarsResult<Self> {
Ok(self.apply_kernel(&|arr| Box::new(set_at_nulls(arr, value))))
}
}
impl ChunkFillNull for BooleanChunked {
fn fill_null(&self, strategy: FillNullStrategy) -> PolarsResult<Self> {
// nothing to fill
if !self.has_validity() {
return Ok(self.clone());
}
match strategy {
FillNullStrategy::Forward(limit) => {
let mut out: Self = match limit {
Some(limit) => impl_fill_forward_limit!(self, limit),
None => impl_fill_forward!(self),
};
out.rename(self.name());
Ok(out)
}
FillNullStrategy::Backward(limit) => {
let mut out: Self = match limit {
None => fill_backward_bool(self),
Some(limit) => fill_backward_limit_bool(self, limit),
};
out.rename(self.name());
Ok(out)
}
FillNullStrategy::Min => self.fill_null_with_values(
1 == self.min().ok_or_else(|| {
PolarsError::ComputeError("Could not determine fill value".into())
})?,
),
FillNullStrategy::Max => self.fill_null_with_values(
1 == self.max().ok_or_else(|| {
PolarsError::ComputeError("Could not determine fill value".into())
})?,
),
FillNullStrategy::Mean => Err(PolarsError::InvalidOperation(
"mean not supported on array of Boolean type".into(),
)),
FillNullStrategy::One | FillNullStrategy::MaxBound => self.fill_null_with_values(true),
FillNullStrategy::Zero | FillNullStrategy::MinBound => {
self.fill_null_with_values(false)
}
}
}
sourcefn max(&self) -> Option<T>
fn max(&self) -> Option<T>
Returns the maximum value in the array, according to the natural order.
Returns None
if the array is empty or only contains null values.
Examples found in repository?
src/chunked_array/ops/aggregate.rs (line 598)
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
fn max_as_series(&self) -> Series {
let v = self.max();
let mut ca: ChunkedArray<T> = [v].iter().copied().collect();
ca.rename(self.name());
ca.into_series()
}
fn min_as_series(&self) -> Series {
let v = self.min();
let mut ca: ChunkedArray<T> = [v].iter().copied().collect();
ca.rename(self.name());
ca.into_series()
}
fn prod_as_series(&self) -> Series {
let mut prod = None;
for opt_v in self.into_iter() {
match (prod, opt_v) {
(_, None) => return Self::full_null(self.name(), 1).into_series(),
(None, Some(v)) => prod = Some(v),
(Some(p), Some(v)) => prod = Some(p * v),
}
}
Self::from_slice_options(self.name(), &[prod]).into_series()
}
}
macro_rules! impl_as_series {
($self:expr, $agg:ident, $ty: ty) => {{
let v = $self.$agg();
let mut ca: $ty = [v].iter().copied().collect();
ca.rename($self.name());
ca.into_series()
}};
($self:expr, $agg:ident, $arg:expr, $ty: ty) => {{
let v = $self.$agg($arg);
let mut ca: $ty = [v].iter().copied().collect();
ca.rename($self.name());
ca.into_series()
}};
}
impl<T> VarAggSeries for ChunkedArray<T>
where
T: PolarsIntegerType,
<T::Native as Simd>::Simd: Add<Output = <T::Native as Simd>::Simd>
+ compute::aggregate::Sum<T::Native>
+ compute::aggregate::SimdOrd<T::Native>,
{
fn var_as_series(&self, ddof: u8) -> Series {
impl_as_series!(self, var, ddof, Float64Chunked)
}
fn std_as_series(&self, ddof: u8) -> Series {
impl_as_series!(self, std, ddof, Float64Chunked)
}
}
impl VarAggSeries for Float32Chunked {
fn var_as_series(&self, ddof: u8) -> Series {
impl_as_series!(self, var, ddof, Float32Chunked)
}
fn std_as_series(&self, ddof: u8) -> Series {
impl_as_series!(self, std, ddof, Float32Chunked)
}
}
impl VarAggSeries for Float64Chunked {
fn var_as_series(&self, ddof: u8) -> Series {
impl_as_series!(self, var, ddof, Float64Chunked)
}
fn std_as_series(&self, ddof: u8) -> Series {
impl_as_series!(self, std, ddof, Float64Chunked)
}
}
macro_rules! impl_quantile_as_series {
($self:expr, $agg:ident, $ty: ty, $qtl:expr, $opt:expr) => {{
let v = $self.$agg($qtl, $opt)?;
let mut ca: $ty = [v].iter().copied().collect();
ca.rename($self.name());
Ok(ca.into_series())
}};
}
impl<T> QuantileAggSeries for ChunkedArray<T>
where
T: PolarsIntegerType,
T::Native: Ord,
<T::Native as Simd>::Simd: Add<Output = <T::Native as Simd>::Simd>
+ compute::aggregate::Sum<T::Native>
+ compute::aggregate::SimdOrd<T::Native>,
{
fn quantile_as_series(
&self,
quantile: f64,
interpol: QuantileInterpolOptions,
) -> PolarsResult<Series> {
impl_quantile_as_series!(self, quantile, Float64Chunked, quantile, interpol)
}
fn median_as_series(&self) -> Series {
impl_as_series!(self, median, Float64Chunked)
}
}
impl QuantileAggSeries for Float32Chunked {
fn quantile_as_series(
&self,
quantile: f64,
interpol: QuantileInterpolOptions,
) -> PolarsResult<Series> {
impl_quantile_as_series!(self, quantile, Float32Chunked, quantile, interpol)
}
fn median_as_series(&self) -> Series {
impl_as_series!(self, median, Float32Chunked)
}
}
impl QuantileAggSeries for Float64Chunked {
fn quantile_as_series(
&self,
quantile: f64,
interpol: QuantileInterpolOptions,
) -> PolarsResult<Series> {
impl_quantile_as_series!(self, quantile, Float64Chunked, quantile, interpol)
}
fn median_as_series(&self) -> Series {
impl_as_series!(self, median, Float64Chunked)
}
}
impl ChunkAggSeries for BooleanChunked {
fn sum_as_series(&self) -> Series {
let v = ChunkAgg::sum(self);
let mut ca: IdxCa = [v].iter().copied().collect();
ca.rename(self.name());
ca.into_series()
}
fn max_as_series(&self) -> Series {
let v = ChunkAgg::max(self);
let mut ca: IdxCa = [v].iter().copied().collect();
ca.rename(self.name());
ca.into_series()
}
More examples
src/frame/groupby/aggregations/mod.rs (line 276)
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
pub(crate) unsafe fn agg_max(&self, groups: &GroupsProxy) -> Series {
// faster paths
match (self.is_sorted2(), self.null_count()) {
(IsSorted::Ascending, 0) => {
return self.clone().into_series().agg_last(groups);
}
(IsSorted::Descending, 0) => {
return self.clone().into_series().agg_first(groups);
}
_ => {}
}
match groups {
GroupsProxy::Idx(groups) => _agg_helper_idx_bool(groups, |(first, idx)| {
debug_assert!(idx.len() <= self.len());
if idx.is_empty() {
None
} else if idx.len() == 1 {
self.get(first as usize)
} else {
// TODO! optimize this
// can just check if any is true and early stop
let take = { self.take_unchecked(idx.into()) };
take.max().map(|v| v == 1)
}
}),
GroupsProxy::Slice {
groups: groups_slice,
..
} => _agg_helper_slice_bool(groups_slice, |[first, len]| {
debug_assert!(len <= self.len() as IdxSize);
match len {
0 => None,
1 => self.get(first as usize),
_ => {
let arr_group = _slice_from_offsets(self, first, len);
arr_group.max().map(|v| v == 1)
}
}
}),
}
}
pub(crate) unsafe fn agg_sum(&self, groups: &GroupsProxy) -> Series {
self.cast(&IDX_DTYPE).unwrap().agg_sum(groups)
}
}
impl Utf8Chunked {
#[allow(clippy::needless_lifetimes)]
pub(crate) unsafe fn agg_min<'a>(&'a self, groups: &GroupsProxy) -> Series {
// faster paths
match (&self.is_sorted2(), &self.null_count()) {
(IsSorted::Ascending, 0) => {
return self.clone().into_series().agg_first(groups);
}
(IsSorted::Descending, 0) => {
return self.clone().into_series().agg_last(groups);
}
_ => {}
}
match groups {
GroupsProxy::Idx(groups) => {
let ca_self = self.rechunk();
let arr = ca_self.downcast_iter().next().unwrap();
_agg_helper_idx_utf8(groups, |(first, idx)| {
debug_assert!(idx.len() <= ca_self.len());
if idx.is_empty() {
None
} else if idx.len() == 1 {
ca_self.get(first as usize)
} else if self.null_count() == 0 {
take_agg_utf8_iter_unchecked_no_null(
arr,
indexes_to_usizes(idx),
|acc, v| if acc < v { acc } else { v },
)
} else {
take_agg_utf8_iter_unchecked(
arr,
indexes_to_usizes(idx),
|acc, v| if acc < v { acc } else { v },
idx.len() as IdxSize,
)
}
})
}
GroupsProxy::Slice {
groups: groups_slice,
..
} => _agg_helper_slice_utf8(groups_slice, |[first, len]| {
debug_assert!(len <= self.len() as IdxSize);
match len {
0 => None,
1 => self.get(first as usize),
_ => {
let arr_group = _slice_from_offsets(self, first, len);
let borrowed = arr_group.min_str();
// Safety:
// The borrowed has `arr_group`s lifetime, but it actually points to data
// hold by self. Here we tell the compiler that.
unsafe { std::mem::transmute::<Option<&str>, Option<&'a str>>(borrowed) }
}
}
}),
}
}
#[allow(clippy::needless_lifetimes)]
pub(crate) unsafe fn agg_max<'a>(&'a self, groups: &GroupsProxy) -> Series {
// faster paths
match (self.is_sorted2(), self.null_count()) {
(IsSorted::Ascending, 0) => {
return self.clone().into_series().agg_last(groups);
}
(IsSorted::Descending, 0) => {
return self.clone().into_series().agg_first(groups);
}
_ => {}
}
match groups {
GroupsProxy::Idx(groups) => {
let ca_self = self.rechunk();
let arr = ca_self.downcast_iter().next().unwrap();
_agg_helper_idx_utf8(groups, |(first, idx)| {
debug_assert!(idx.len() <= self.len());
if idx.is_empty() {
None
} else if idx.len() == 1 {
ca_self.get(first as usize)
} else if ca_self.null_count() == 0 {
take_agg_utf8_iter_unchecked_no_null(
arr,
indexes_to_usizes(idx),
|acc, v| if acc > v { acc } else { v },
)
} else {
take_agg_utf8_iter_unchecked(
arr,
indexes_to_usizes(idx),
|acc, v| if acc > v { acc } else { v },
idx.len() as IdxSize,
)
}
})
}
GroupsProxy::Slice {
groups: groups_slice,
..
} => _agg_helper_slice_utf8(groups_slice, |[first, len]| {
debug_assert!(len <= self.len() as IdxSize);
match len {
0 => None,
1 => self.get(first as usize),
_ => {
let arr_group = _slice_from_offsets(self, first, len);
let borrowed = arr_group.max_str();
// Safety:
// The borrowed has `arr_group`s lifetime, but it actually points to data
// hold by self. Here we tell the compiler that.
unsafe { std::mem::transmute::<Option<&str>, Option<&'a str>>(borrowed) }
}
}
}),
}
}
}
#[inline(always)]
fn take_min<T: PartialOrd>(a: T, b: T) -> T {
if a < b {
a
} else {
b
}
}
#[inline(always)]
fn take_max<T: PartialOrd>(a: T, b: T) -> T {
if a > b {
a
} else {
b
}
}
impl<T> ChunkedArray<T>
where
T: PolarsNumericType + Sync,
T::Native:
NativeType + PartialOrd + Num + NumCast + Zero + Simd + Bounded + std::iter::Sum<T::Native>,
<T::Native as Simd>::Simd: std::ops::Add<Output = <T::Native as Simd>::Simd>
+ arrow::compute::aggregate::Sum<T::Native>
+ arrow::compute::aggregate::SimdOrd<T::Native>,
ChunkedArray<T>: IntoSeries,
{
pub(crate) unsafe fn agg_min(&self, groups: &GroupsProxy) -> Series {
// faster paths
match (self.is_sorted2(), self.null_count()) {
(IsSorted::Ascending, 0) => {
return self.clone().into_series().agg_first(groups);
}
(IsSorted::Descending, 0) => {
return self.clone().into_series().agg_last(groups);
}
_ => {}
}
match groups {
GroupsProxy::Idx(groups) => _agg_helper_idx::<T, _>(groups, |(first, idx)| {
debug_assert!(idx.len() <= self.len());
if idx.is_empty() {
None
} else if idx.len() == 1 {
self.get(first as usize)
} else {
match (self.has_validity(), self.chunks.len()) {
(false, 1) => Some(take_agg_no_null_primitive_iter_unchecked(
self.downcast_iter().next().unwrap(),
idx.iter().map(|i| *i as usize),
take_min,
T::Native::max_value(),
)),
(_, 1) => take_agg_primitive_iter_unchecked::<T::Native, _, _>(
self.downcast_iter().next().unwrap(),
idx.iter().map(|i| *i as usize),
take_min,
T::Native::max_value(),
idx.len() as IdxSize,
),
_ => {
let take = { self.take_unchecked(idx.into()) };
take.min()
}
}
}
}),
GroupsProxy::Slice {
groups: groups_slice,
..
} => {
if _use_rolling_kernels(groups_slice, self.chunks()) {
let arr = self.downcast_iter().next().unwrap();
let values = arr.values().as_slice();
let offset_iter = groups_slice.iter().map(|[first, len]| (*first, *len));
let arr = match arr.validity() {
None => _rolling_apply_agg_window_no_nulls::<MinWindow<_>, _, _>(
values,
offset_iter,
),
Some(validity) => _rolling_apply_agg_window_nulls::<
rolling::nulls::MinWindow<_>,
_,
_,
>(values, validity, offset_iter),
};
Self::from_chunks("", vec![arr]).into_series()
} else {
_agg_helper_slice::<T, _>(groups_slice, |[first, len]| {
debug_assert!(len <= self.len() as IdxSize);
match len {
0 => None,
1 => self.get(first as usize),
_ => {
let arr_group = _slice_from_offsets(self, first, len);
arr_group.min()
}
}
})
}
}
}
}
pub(crate) unsafe fn agg_max(&self, groups: &GroupsProxy) -> Series {
// faster paths
match (self.is_sorted2(), self.null_count()) {
(IsSorted::Ascending, 0) => {
return self.clone().into_series().agg_last(groups);
}
(IsSorted::Descending, 0) => {
return self.clone().into_series().agg_first(groups);
}
_ => {}
}
match groups {
GroupsProxy::Idx(groups) => _agg_helper_idx::<T, _>(groups, |(first, idx)| {
debug_assert!(idx.len() <= self.len());
if idx.is_empty() {
None
} else if idx.len() == 1 {
self.get(first as usize)
} else {
match (self.has_validity(), self.chunks.len()) {
(false, 1) => Some({
take_agg_no_null_primitive_iter_unchecked(
self.downcast_iter().next().unwrap(),
idx.iter().map(|i| *i as usize),
take_max,
T::Native::min_value(),
)
}),
(_, 1) => take_agg_primitive_iter_unchecked::<T::Native, _, _>(
self.downcast_iter().next().unwrap(),
idx.iter().map(|i| *i as usize),
take_max,
T::Native::min_value(),
idx.len() as IdxSize,
),
_ => {
let take = { self.take_unchecked(idx.into()) };
take.max()
}
}
}
}),
GroupsProxy::Slice {
groups: groups_slice,
..
} => {
if _use_rolling_kernels(groups_slice, self.chunks()) {
let arr = self.downcast_iter().next().unwrap();
let values = arr.values().as_slice();
let offset_iter = groups_slice.iter().map(|[first, len]| (*first, *len));
let arr = match arr.validity() {
None => _rolling_apply_agg_window_no_nulls::<MaxWindow<_>, _, _>(
values,
offset_iter,
),
Some(validity) => _rolling_apply_agg_window_nulls::<
rolling::nulls::MaxWindow<_>,
_,
_,
>(values, validity, offset_iter),
};
Self::from_chunks("", vec![arr]).into_series()
} else {
_agg_helper_slice::<T, _>(groups_slice, |[first, len]| {
debug_assert!(len <= self.len() as IdxSize);
match len {
0 => None,
1 => self.get(first as usize),
_ => {
let arr_group = _slice_from_offsets(self, first, len);
arr_group.max()
}
}
})
}
}
}
}
src/chunked_array/ops/fill_null.rs (line 258)
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
fn fill_null(&self, strategy: FillNullStrategy) -> PolarsResult<Self> {
// nothing to fill
if !self.has_validity() {
return Ok(self.clone());
}
let mut ca = match strategy {
FillNullStrategy::Forward(None) => fill_forward(self),
FillNullStrategy::Forward(Some(limit)) => fill_forward_limit(self, limit),
FillNullStrategy::Backward(None) => fill_backward(self),
FillNullStrategy::Backward(Some(limit)) => fill_backward_limit(self, limit),
FillNullStrategy::Min => {
self.fill_null_with_values(self.min().ok_or_else(|| {
PolarsError::ComputeError("Could not determine fill value".into())
})?)?
}
FillNullStrategy::Max => {
self.fill_null_with_values(self.max().ok_or_else(|| {
PolarsError::ComputeError("Could not determine fill value".into())
})?)?
}
FillNullStrategy::Mean => self.fill_null_with_values(
self.mean()
.map(|v| NumCast::from(v).unwrap())
.ok_or_else(|| {
PolarsError::ComputeError("Could not determine fill value".into())
})?,
)?,
FillNullStrategy::One => return self.fill_null_with_values(One::one()),
FillNullStrategy::Zero => return self.fill_null_with_values(Zero::zero()),
FillNullStrategy::MinBound => return self.fill_null_with_values(Bounded::min_value()),
FillNullStrategy::MaxBound => return self.fill_null_with_values(Bounded::max_value()),
};
ca.rename(self.name());
Ok(ca)
}
}
impl<T> ChunkFillNullValue<T::Native> for ChunkedArray<T>
where
T: PolarsNumericType,
{
fn fill_null_with_values(&self, value: T::Native) -> PolarsResult<Self> {
Ok(self.apply_kernel(&|arr| Box::new(set_at_nulls(arr, value))))
}
}
impl ChunkFillNull for BooleanChunked {
fn fill_null(&self, strategy: FillNullStrategy) -> PolarsResult<Self> {
// nothing to fill
if !self.has_validity() {
return Ok(self.clone());
}
match strategy {
FillNullStrategy::Forward(limit) => {
let mut out: Self = match limit {
Some(limit) => impl_fill_forward_limit!(self, limit),
None => impl_fill_forward!(self),
};
out.rename(self.name());
Ok(out)
}
FillNullStrategy::Backward(limit) => {
let mut out: Self = match limit {
None => fill_backward_bool(self),
Some(limit) => fill_backward_limit_bool(self, limit),
};
out.rename(self.name());
Ok(out)
}
FillNullStrategy::Min => self.fill_null_with_values(
1 == self.min().ok_or_else(|| {
PolarsError::ComputeError("Could not determine fill value".into())
})?,
),
FillNullStrategy::Max => self.fill_null_with_values(
1 == self.max().ok_or_else(|| {
PolarsError::ComputeError("Could not determine fill value".into())
})?,
),
FillNullStrategy::Mean => Err(PolarsError::InvalidOperation(
"mean not supported on array of Boolean type".into(),
)),
FillNullStrategy::One | FillNullStrategy::MaxBound => self.fill_null_with_values(true),
FillNullStrategy::Zero | FillNullStrategy::MinBound => {
self.fill_null_with_values(false)
}
}
}
sourcefn mean(&self) -> Option<f64>
fn mean(&self) -> Option<f64>
Returns the mean value in the array.
Returns None
if the array is empty or only contains null values.
Examples found in repository?
More examples
src/functions.rs (line 34)
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
pub fn cov_f<T>(a: &ChunkedArray<T>, b: &ChunkedArray<T>) -> Option<T::Native>
where
T: PolarsFloatType,
T::Native: Float,
<T::Native as Simd>::Simd: Add<Output = <T::Native as Simd>::Simd>
+ compute::aggregate::Sum<T::Native>
+ compute::aggregate::SimdOrd<T::Native>,
{
if a.len() != b.len() {
None
} else {
let tmp = (a - a.mean()?) * (b - b.mean()?);
let n = tmp.len() - tmp.null_count();
Some(tmp.sum()? / NumCast::from(n - 1).unwrap())
}
}
/// Compute the covariance between two columns.
pub fn cov_i<T>(a: &ChunkedArray<T>, b: &ChunkedArray<T>) -> Option<f64>
where
T: PolarsIntegerType,
T::Native: ToPrimitive,
<T::Native as Simd>::Simd: Add<Output = <T::Native as Simd>::Simd>
+ compute::aggregate::Sum<T::Native>
+ compute::aggregate::SimdOrd<T::Native>,
{
if a.len() != b.len() {
None
} else {
let a_mean = a.mean()?;
let b_mean = b.mean()?;
let a = a.apply_cast_numeric::<_, Float64Type>(|a| a.to_f64().unwrap() - a_mean);
let b = b.apply_cast_numeric(|b| b.to_f64().unwrap() - b_mean);
let tmp = a * b;
let n = tmp.len() - tmp.null_count();
Some(tmp.sum()? / (n - 1) as f64)
}
}
src/chunked_array/ops/aggregate.rs (line 466)
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
fn var(&self, ddof: u8) -> Option<f64> {
if self.len() == 1 {
return Some(0.0);
}
let n_values = self.len() - self.null_count();
if ddof as usize > n_values {
return None;
}
let n_values = n_values as f64;
let mean = self.mean()?;
let squared = self.apply_cast_numeric::<_, Float64Type>(|value| {
let tmp = value.to_f64().unwrap() - mean;
tmp * tmp
});
// Note, this is similar behavior to numpy if DDOF=1.
// in statistics DDOF often = 1.
// this last step is similar to mean, only now instead of 1/n it is 1/(n-1)
squared.sum().map(|sum| sum / (n_values - ddof as f64))
}
fn std(&self, ddof: u8) -> Option<f64> {
self.var(ddof).map(|var| var.sqrt())
}
}
impl ChunkVar<f32> for Float32Chunked {
fn var(&self, ddof: u8) -> Option<f32> {
if self.len() == 1 {
return Some(0.0);
}
let n_values = self.len() - self.null_count();
if ddof as usize > n_values {
return None;
}
let n_values = n_values as f32;
let mean = self.mean()? as f32;
let squared = self.apply(|value| {
let tmp = value - mean;
tmp * tmp
});
squared.sum().map(|sum| sum / (n_values - ddof as f32))
}
fn std(&self, ddof: u8) -> Option<f32> {
self.var(ddof).map(|var| var.sqrt())
}
}
impl ChunkVar<f64> for Float64Chunked {
fn var(&self, ddof: u8) -> Option<f64> {
if self.len() == 1 {
return Some(0.0);
}
let n_values = self.len() - self.null_count();
if ddof as usize > n_values {
return None;
}
let n_values = n_values as f64;
let mean = self.mean()?;
let squared = self.apply(|value| {
let tmp = value - mean;
tmp * tmp
});
squared.sum().map(|sum| sum / (n_values - ddof as f64))
}
src/chunked_array/ops/fill_null.rs (line 263)
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
fn fill_null(&self, strategy: FillNullStrategy) -> PolarsResult<Self> {
// nothing to fill
if !self.has_validity() {
return Ok(self.clone());
}
let mut ca = match strategy {
FillNullStrategy::Forward(None) => fill_forward(self),
FillNullStrategy::Forward(Some(limit)) => fill_forward_limit(self, limit),
FillNullStrategy::Backward(None) => fill_backward(self),
FillNullStrategy::Backward(Some(limit)) => fill_backward_limit(self, limit),
FillNullStrategy::Min => {
self.fill_null_with_values(self.min().ok_or_else(|| {
PolarsError::ComputeError("Could not determine fill value".into())
})?)?
}
FillNullStrategy::Max => {
self.fill_null_with_values(self.max().ok_or_else(|| {
PolarsError::ComputeError("Could not determine fill value".into())
})?)?
}
FillNullStrategy::Mean => self.fill_null_with_values(
self.mean()
.map(|v| NumCast::from(v).unwrap())
.ok_or_else(|| {
PolarsError::ComputeError("Could not determine fill value".into())
})?,
)?,
FillNullStrategy::One => return self.fill_null_with_values(One::one()),
FillNullStrategy::Zero => return self.fill_null_with_values(Zero::zero()),
FillNullStrategy::MinBound => return self.fill_null_with_values(Bounded::min_value()),
FillNullStrategy::MaxBound => return self.fill_null_with_values(Bounded::max_value()),
};
ca.rename(self.name());
Ok(ca)
}
src/frame/groupby/aggregations/mod.rs (line 731)
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
pub(crate) unsafe fn agg_mean(&self, groups: &GroupsProxy) -> Series {
match groups {
GroupsProxy::Idx(groups) => {
_agg_helper_idx::<T, _>(groups, |(first, idx)| {
// this can fail due to a bug in lazy code.
// here users can create filters in aggregations
// and thereby creating shorter columns than the original group tuples.
// the group tuples are modified, but if that's done incorrect there can be out of bounds
// access
debug_assert!(idx.len() <= self.len());
let out = if idx.is_empty() {
None
} else if idx.len() == 1 {
self.get(first as usize).map(|sum| sum.to_f64().unwrap())
} else {
match (self.has_validity(), self.chunks.len()) {
(false, 1) => {
take_agg_no_null_primitive_iter_unchecked(
self.downcast_iter().next().unwrap(),
idx.iter().map(|i| *i as usize),
|a, b| a + b,
T::Native::zero(),
)
}
.to_f64()
.map(|sum| sum / idx.len() as f64),
(_, 1) => {
take_agg_primitive_iter_unchecked_count_nulls::<T::Native, _, _, _>(
self.downcast_iter().next().unwrap(),
idx.iter().map(|i| *i as usize),
|a, b| a + b,
T::Native::zero(),
idx.len() as IdxSize,
)
}
.map(|(sum, null_count)| {
sum.to_f64()
.map(|sum| sum / (idx.len() as f64 - null_count as f64))
.unwrap()
}),
_ => {
let take = { self.take_unchecked(idx.into()) };
take.mean()
}
}
};
out.map(|flt| NumCast::from(flt).unwrap())
})
}
GroupsProxy::Slice { groups, .. } => {
if _use_rolling_kernels(groups, self.chunks()) {
let arr = self.downcast_iter().next().unwrap();
let values = arr.values().as_slice();
let offset_iter = groups.iter().map(|[first, len]| (*first, *len));
let arr = match arr.validity() {
None => _rolling_apply_agg_window_no_nulls::<MeanWindow<_>, _, _>(
values,
offset_iter,
),
Some(validity) => _rolling_apply_agg_window_nulls::<
rolling::nulls::MeanWindow<_>,
_,
_,
>(values, validity, offset_iter),
};
ChunkedArray::<T>::from_chunks("", vec![arr]).into_series()
} else {
_agg_helper_slice::<T, _>(groups, |[first, len]| {
debug_assert!(len <= self.len() as IdxSize);
match len {
0 => None,
1 => self.get(first as usize),
_ => {
let arr_group = _slice_from_offsets(self, first, len);
arr_group.mean().map(|flt| NumCast::from(flt).unwrap())
}
}
})
}
}
}
}
pub(crate) unsafe fn agg_var(&self, groups: &GroupsProxy, ddof: u8) -> Series {
let ca = &self.0;
match groups {
GroupsProxy::Idx(groups) => agg_helper_idx_on_all::<T, _>(groups, |idx| {
debug_assert!(idx.len() <= ca.len());
if idx.is_empty() {
return None;
}
let take = { ca.take_unchecked(idx.into()) };
take.var_as_series(ddof).unpack::<T>().unwrap().get(0)
}),
GroupsProxy::Slice { groups, .. } => {
if _use_rolling_kernels(groups, self.chunks()) {
let arr = self.downcast_iter().next().unwrap();
let values = arr.values().as_slice();
let offset_iter = groups.iter().map(|[first, len]| (*first, *len));
let arr = match arr.validity() {
None => _rolling_apply_agg_window_no_nulls::<VarWindow<_>, _, _>(
values,
offset_iter,
),
Some(validity) => _rolling_apply_agg_window_nulls::<
rolling::nulls::VarWindow<_>,
_,
_,
>(values, validity, offset_iter),
};
ChunkedArray::<T>::from_chunks("", vec![arr]).into_series()
} else {
_agg_helper_slice::<T, _>(groups, |[first, len]| {
debug_assert!(len <= self.len() as IdxSize);
match len {
0 => None,
1 => NumCast::from(0),
_ => {
let arr_group = _slice_from_offsets(self, first, len);
arr_group.var(ddof).map(|flt| NumCast::from(flt).unwrap())
}
}
})
}
}
}
}
pub(crate) unsafe fn agg_std(&self, groups: &GroupsProxy, ddof: u8) -> Series {
let ca = &self.0;
match groups {
GroupsProxy::Idx(groups) => agg_helper_idx_on_all::<T, _>(groups, |idx| {
debug_assert!(idx.len() <= ca.len());
if idx.is_empty() {
return None;
}
let take = { ca.take_unchecked(idx.into()) };
take.std_as_series(ddof).unpack::<T>().unwrap().get(0)
}),
GroupsProxy::Slice { groups, .. } => {
if _use_rolling_kernels(groups, self.chunks()) {
let arr = self.downcast_iter().next().unwrap();
let values = arr.values().as_slice();
let offset_iter = groups.iter().map(|[first, len]| (*first, *len));
let arr = match arr.validity() {
None => _rolling_apply_agg_window_no_nulls::<StdWindow<_>, _, _>(
values,
offset_iter,
),
Some(validity) => _rolling_apply_agg_window_nulls::<
rolling::nulls::StdWindow<_>,
_,
_,
>(values, validity, offset_iter),
};
ChunkedArray::<T>::from_chunks("", vec![arr]).into_series()
} else {
_agg_helper_slice::<T, _>(groups, |[first, len]| {
debug_assert!(len <= self.len() as IdxSize);
match len {
0 => None,
1 => NumCast::from(0),
_ => {
let arr_group = _slice_from_offsets(self, first, len);
arr_group.std(ddof).map(|flt| NumCast::from(flt).unwrap())
}
}
})
}
}
}
}
pub(crate) unsafe fn agg_quantile(
&self,
groups: &GroupsProxy,
quantile: f64,
interpol: QuantileInterpolOptions,
) -> Series {
let ca = &self.0;
let invalid_quantile = !(0.0..=1.0).contains(&quantile);
match groups {
GroupsProxy::Idx(groups) => agg_helper_idx_on_all::<T, _>(groups, |idx| {
debug_assert!(idx.len() <= ca.len());
if idx.is_empty() | invalid_quantile {
return None;
}
let take = { ca.take_unchecked(idx.into()) };
take.quantile_as_series(quantile, interpol)
.unwrap() // checked with invalid quantile check
.unpack::<T>()
.unwrap()
.get(0)
}),
GroupsProxy::Slice { groups, .. } => {
if _use_rolling_kernels(groups, self.chunks()) {
let arr = self.downcast_iter().next().unwrap();
let values = arr.values().as_slice();
let offset_iter = groups.iter().map(|[first, len]| (*first, *len));
let arr = match arr.validity() {
None => rolling::no_nulls::rolling_quantile_by_iter(
values,
quantile,
interpol,
offset_iter,
),
Some(validity) => rolling::nulls::rolling_quantile_by_iter(
values,
validity,
quantile,
interpol,
offset_iter,
),
};
ChunkedArray::<T>::from_chunks("", vec![arr]).into_series()
} else {
_agg_helper_slice::<T, _>(groups, |[first, len]| {
debug_assert!(first + len <= self.len() as IdxSize);
match len {
0 => None,
1 => self.get(first as usize),
_ => {
let arr_group = _slice_from_offsets(self, first, len);
// unwrap checked with invalid quantile check
arr_group
.quantile(quantile, interpol)
.unwrap()
.map(|flt| NumCast::from(flt).unwrap())
}
}
})
}
}
}
}
pub(crate) unsafe fn agg_median(&self, groups: &GroupsProxy) -> Series {
let ca = &self.0;
match groups {
GroupsProxy::Idx(groups) => agg_helper_idx_on_all::<T, _>(groups, |idx| {
debug_assert!(idx.len() <= ca.len());
if idx.is_empty() {
return None;
}
let take = { ca.take_unchecked(idx.into()) };
take.median_as_series().unpack::<T>().unwrap().get(0)
}),
GroupsProxy::Slice { .. } => {
self.agg_quantile(groups, 0.5, QuantileInterpolOptions::Linear)
}
}
}
}
impl<T> ChunkedArray<T>
where
T: PolarsIntegerType,
ChunkedArray<T>: IntoSeries,
T::Native: NumericNative + Ord,
<T::Native as Simd>::Simd: std::ops::Add<Output = <T::Native as Simd>::Simd>
+ arrow::compute::aggregate::Sum<T::Native>
+ arrow::compute::aggregate::SimdOrd<T::Native>,
{
pub(crate) unsafe fn agg_mean(&self, groups: &GroupsProxy) -> Series {
match groups {
GroupsProxy::Idx(groups) => {
_agg_helper_idx::<Float64Type, _>(groups, |(first, idx)| {
// this can fail due to a bug in lazy code.
// here users can create filters in aggregations
// and thereby creating shorter columns than the original group tuples.
// the group tuples are modified, but if that's done incorrect there can be out of bounds
// access
debug_assert!(idx.len() <= self.len());
if idx.is_empty() {
None
} else if idx.len() == 1 {
self.get(first as usize).map(|sum| sum.to_f64().unwrap())
} else {
match (self.has_validity(), self.chunks.len()) {
(false, 1) => {
take_agg_no_null_primitive_iter_unchecked(
self.downcast_iter().next().unwrap(),
idx.iter().map(|i| *i as usize),
|a, b| a + b,
0.0f64,
)
}
.to_f64()
.map(|sum| sum / idx.len() as f64),
(_, 1) => {
{
take_agg_primitive_iter_unchecked_count_nulls::<
T::Native,
f64,
_,
_,
>(
self.downcast_iter().next().unwrap(),
idx.iter().map(|i| *i as usize),
|a, b| a + b,
0.0,
idx.len() as IdxSize,
)
}
.map(|(sum, null_count)| {
sum / (idx.len() as f64 - null_count as f64)
})
}
_ => {
let take = { self.take_unchecked(idx.into()) };
take.mean()
}
}
}
})
}
GroupsProxy::Slice {
groups: groups_slice,
..
} => {
if _use_rolling_kernels(groups_slice, self.chunks()) {
let ca = self.cast(&DataType::Float64).unwrap();
ca.agg_mean(groups)
} else {
_agg_helper_slice::<Float64Type, _>(groups_slice, |[first, len]| {
debug_assert!(first + len <= self.len() as IdxSize);
match len {
0 => None,
1 => self.get(first as usize).map(|v| NumCast::from(v).unwrap()),
_ => {
let arr_group = _slice_from_offsets(self, first, len);
arr_group.mean()
}
}
})
}
}
}
}