1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
use crate::prelude::*;
use crate::utils::{align_chunks_ternary, CustomIterTools};
use arrow::compute::if_then_else::if_then_else;
use polars_arrow::array::default_arrays::FromData;
#[cfg(feature = "dtype-categorical")]
use std::ops::Deref;
fn ternary_apply<T>(predicate: bool, truthy: T, falsy: T) -> T {
if predicate {
truthy
} else {
falsy
}
}
fn prepare_mask(mask: &BooleanArray) -> BooleanArray {
match mask.validity() {
Some(validity) => {
let mask = mask.values() | &(!validity);
BooleanArray::from_data_default(mask, None)
}
None => mask.clone(),
}
}
macro_rules! impl_ternary_broadcast {
($self:ident, $self_len:expr, $other_len:expr, $other:expr, $mask:expr, $ty:ty) => {{
match ($self_len, $other_len) {
(1, 1) => {
let left = $self.get(0);
let right = $other.get(0);
let mut val: ChunkedArray<$ty> = $mask
.into_no_null_iter()
.map(|mask_val| ternary_apply(mask_val, left, right))
.collect_trusted();
val.rename($self.name());
Ok(val)
}
(_, 1) => {
let right = $other.get(0);
let mut val: ChunkedArray<$ty> = $mask
.into_no_null_iter()
.zip($self)
.map(|(mask_val, left)| ternary_apply(mask_val, left, right))
.collect_trusted();
val.rename($self.name());
Ok(val)
}
(1, _) => {
let left = $self.get(0);
let mut val: ChunkedArray<$ty> = $mask
.into_no_null_iter()
.zip($other)
.map(|(mask_val, right)| ternary_apply(mask_val, left, right))
.collect_trusted();
val.rename($self.name());
Ok(val)
}
(_, _) => Err(PolarsError::ShapeMisMatch(
"Shape of parameter `mask` and `other` could not be used in zip_with operation"
.into(),
)),
}
}};
}
impl<T> ChunkZip<T> for ChunkedArray<T>
where
T: PolarsNumericType,
{
fn zip_with(&self, mask: &BooleanChunked, other: &ChunkedArray<T>) -> Result<ChunkedArray<T>> {
if self.len() != mask.len() || other.len() != mask.len() {
impl_ternary_broadcast!(self, self.len(), other.len(), other, mask, T)
} else {
let (left, right, mask) = align_chunks_ternary(self, other, mask);
let chunks = left
.downcast_iter()
.zip(right.downcast_iter())
.zip(mask.downcast_iter())
.map(|((left_c, right_c), mask_c)| {
let mask_c = prepare_mask(mask_c);
let arr = if_then_else(&mask_c, left_c, right_c)?.into();
Ok(arr)
})
.collect::<Result<Vec<_>>>()?;
Ok(ChunkedArray::new_from_chunks(self.name(), chunks))
}
}
}
impl ChunkZip<BooleanType> for BooleanChunked {
fn zip_with(&self, mask: &BooleanChunked, other: &BooleanChunked) -> Result<BooleanChunked> {
if self.len() != mask.len() || other.len() != mask.len() {
impl_ternary_broadcast!(self, self.len(), other.len(), other, mask, BooleanType)
} else {
let (left, right, mask) = align_chunks_ternary(self, other, mask);
let chunks = left
.downcast_iter()
.zip(right.downcast_iter())
.zip(mask.downcast_iter())
.map(|((left_c, right_c), mask_c)| {
let mask_c = prepare_mask(mask_c);
let arr = if_then_else(&mask_c, left_c, right_c)?.into();
Ok(arr)
})
.collect::<Result<Vec<_>>>()?;
Ok(ChunkedArray::new_from_chunks(self.name(), chunks))
}
}
}
impl ChunkZip<Utf8Type> for Utf8Chunked {
fn zip_with(&self, mask: &BooleanChunked, other: &Utf8Chunked) -> Result<Utf8Chunked> {
if self.len() != mask.len() || other.len() != mask.len() {
impl_ternary_broadcast!(self, self.len(), other.len(), other, mask, Utf8Type)
} else {
let (left, right, mask) = align_chunks_ternary(self, other, mask);
let chunks = left
.downcast_iter()
.zip(right.downcast_iter())
.zip(mask.downcast_iter())
.map(|((left_c, right_c), mask_c)| {
let mask_c = prepare_mask(mask_c);
let arr = if_then_else(&mask_c, left_c, right_c)?.into();
Ok(arr)
})
.collect::<Result<Vec<_>>>()?;
Ok(ChunkedArray::new_from_chunks(self.name(), chunks))
}
}
}
impl ChunkZip<ListType> for ListChunked {
fn zip_with(
&self,
mask: &BooleanChunked,
other: &ChunkedArray<ListType>,
) -> Result<ChunkedArray<ListType>> {
let (left, right, mask) = align_chunks_ternary(self, other, mask);
let chunks = left
.downcast_iter()
.zip(right.downcast_iter())
.zip(mask.downcast_iter())
.map(|((left_c, right_c), mask_c)| {
let mask_c = prepare_mask(mask_c);
let arr = if_then_else(&mask_c, left_c, right_c)?.into();
Ok(arr)
})
.collect::<Result<Vec<_>>>()?;
Ok(ChunkedArray::new_from_chunks(self.name(), chunks))
}
}
#[cfg(feature = "dtype-categorical")]
impl ChunkZip<CategoricalType> for CategoricalChunked {
fn zip_with(
&self,
mask: &BooleanChunked,
other: &ChunkedArray<CategoricalType>,
) -> Result<ChunkedArray<CategoricalType>> {
let ca: CategoricalChunked = self.deref().zip_with(mask, other.deref())?.into();
Ok(ca.set_state(self))
}
}
#[cfg(feature = "object")]
impl<T: PolarsObject> ChunkZip<ObjectType<T>> for ObjectChunked<T> {
fn zip_with(
&self,
mask: &BooleanChunked,
other: &ChunkedArray<ObjectType<T>>,
) -> Result<ChunkedArray<ObjectType<T>>> {
let (left, right, mask) = align_chunks_ternary(self, other, mask);
let mut ca: Self = left
.as_ref()
.into_iter()
.zip(right.into_iter())
.zip(mask.into_iter())
.map(|((left_c, right_c), mask_c)| match mask_c {
Some(true) => left_c.cloned(),
Some(false) => right_c.cloned(),
None => None,
})
.collect();
ca.rename(self.name());
Ok(ca)
}
}