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
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
use crate::prelude::*;
use crate::utils::{get_supertype, CustomIterTools};
use hashbrown::hash_set::HashSet;
use std::hash::Hash;
unsafe fn is_in_helper<T, P>(ca: &ChunkedArray<T>, other: &Series) -> Result<BooleanChunked>
where
T: PolarsNumericType,
P: Eq + Hash + Copy,
{
let mut set = HashSet::with_capacity(other.len());
let other = ca.unpack_series_matching_type(other)?;
other.downcast_iter().for_each(|iter| {
iter.into_iter().for_each(|opt_val| {
let ptr = &opt_val.copied() as *const Option<T::Native> as *const Option<P>;
let opt_val = *ptr;
set.insert(opt_val);
})
});
let name = ca.name();
let mut ca: BooleanChunked = ca
.into_iter()
.map(|opt_val| {
let ptr = &opt_val as *const Option<T::Native> as *const Option<P>;
let opt_val = *ptr;
set.contains(&opt_val)
})
.collect_trusted();
ca.rename(name);
Ok(ca)
}
impl<T> IsIn for ChunkedArray<T>
where
T: PolarsNumericType,
{
fn is_in(&self, other: &Series) -> Result<BooleanChunked> {
match other.dtype() {
DataType::List(dt) => {
let st = get_supertype(self.dtype(), dt)?;
if &st != self.dtype() {
let left = self.cast(&st)?;
let right = other.cast(&DataType::List(Box::new(st)))?;
return left.is_in(&right);
}
let mut ca: BooleanChunked = if self.len() == 1 && other.len() != 1 {
let value = self.get(0);
other
.list()?
.amortized_iter()
.map(|opt_s| {
opt_s.map(|s| {
let ca = s.as_ref().unpack::<T>().unwrap();
ca.into_iter().any(|a| a == value)
}) == Some(true)
})
.collect_trusted()
} else {
self.into_iter()
.zip(other.list()?.amortized_iter())
.map(|(value, series)| match (value, series) {
(val, Some(series)) => {
let ca = series.as_ref().unpack::<T>().unwrap();
ca.into_iter().any(|a| a == val)
}
_ => false,
})
.collect_trusted()
};
ca.rename(self.name());
Ok(ca)
}
_ => {
let st = get_supertype(self.dtype(), other.dtype())?;
if self.dtype() != other.dtype() {
let left = self.cast(&st)?;
let right = other.cast(&st)?;
return left.is_in(&right);
}
match self.dtype() {
DataType::UInt64 | DataType::Int64 | DataType::Float64 => unsafe {
is_in_helper::<T, u64>(self, other)
},
DataType::UInt32 | DataType::Int32 | DataType::Float32 => unsafe {
is_in_helper::<T, u32>(self, other)
},
DataType::UInt8 | DataType::Int8 => unsafe {
is_in_helper::<T, u8>(self, other)
},
DataType::UInt16 | DataType::Int16 => unsafe {
is_in_helper::<T, u16>(self, other)
},
_ => Err(PolarsError::ComputeError(
format!(
"Data type {:?} not supported in is_in operation",
self.dtype()
)
.into(),
)),
}
}
}
.map(|mut ca| {
ca.rename(self.name());
ca
})
}
}
impl IsIn for Utf8Chunked {
fn is_in(&self, other: &Series) -> Result<BooleanChunked> {
match other.dtype() {
DataType::List(dt) if self.dtype() == &**dt => {
let mut ca: BooleanChunked = if self.len() == 1 && other.len() != 1 {
let value = self.get(0);
other
.list()?
.amortized_iter()
.map(|opt_s| {
opt_s.map(|s| {
let ca = s.as_ref().unpack::<Utf8Type>().unwrap();
ca.into_iter().any(|a| a == value)
}) == Some(true)
})
.collect_trusted()
} else {
self.into_iter()
.zip(other.list()?.amortized_iter())
.map(|(value, series)| match (value, series) {
(val, Some(series)) => {
let ca = series.as_ref().unpack::<Utf8Type>().unwrap();
ca.into_iter().any(|a| a == val)
}
_ => false,
})
.collect_trusted()
};
ca.rename(self.name());
Ok(ca)
}
DataType::Utf8 => {
let mut set = HashSet::with_capacity(other.len());
let other = other.utf8()?;
other.downcast_iter().for_each(|iter| {
iter.into_iter().for_each(|opt_val| {
set.insert(opt_val);
})
});
let mut ca: BooleanChunked = self
.into_iter()
.map(|opt_val| set.contains(&opt_val))
.collect_trusted();
ca.rename(self.name());
Ok(ca)
}
_ => Err(PolarsError::SchemaMisMatch(
format!(
"cannot do is_in operation with left a dtype: {:?} and right a dtype {:?}",
self.dtype(),
other.dtype()
)
.into(),
)),
}
.map(|mut ca| {
ca.rename(self.name());
ca
})
}
}
impl IsIn for BooleanChunked {
fn is_in(&self, other: &Series) -> Result<BooleanChunked> {
match other.dtype() {
DataType::List(dt) if self.dtype() == &**dt => {
let mut ca: BooleanChunked = if self.len() == 1 && other.len() != 1 {
let value = self.get(0);
unsafe {
other
.list()?
.amortized_iter()
.map(|opt_s| {
opt_s.map(|s| {
let ca = s.as_ref().unpack::<BooleanType>().unwrap();
ca.into_iter().any(|a| a == value)
}) == Some(true)
})
.trust_my_length(other.len())
.collect_trusted()
}
} else {
self.into_iter()
.zip(other.list()?.amortized_iter())
.map(|(value, series)| match (value, series) {
(val, Some(series)) => {
let ca = series.as_ref().unpack::<BooleanType>().unwrap();
ca.into_iter().any(|a| a == val)
}
_ => false,
})
.collect_trusted()
};
ca.rename(self.name());
Ok(ca)
}
_ => Err(PolarsError::SchemaMisMatch(
format!(
"cannot do is_in operation with left a dtype: {:?} and right a dtype {:?}",
self.dtype(),
other.dtype()
)
.into(),
)),
}
.map(|mut ca| {
ca.rename(self.name());
ca
})
}
}
#[cfg(test)]
mod test {
use crate::prelude::*;
#[test]
fn test_is_in() -> Result<()> {
let a = Int32Chunked::new("a", &[1, 2, 3, 4]);
let b = Int64Chunked::new("b", &[4, 5, 1]);
let out = a.is_in(&b.into_series())?;
assert_eq!(
Vec::from(&out),
[Some(true), Some(false), Some(false), Some(true)]
);
let a = Utf8Chunked::new("a", &["a", "b", "c", "d"]);
let b = Utf8Chunked::new("b", &["d", "e", "c"]);
let out = a.is_in(&b.into_series())?;
assert_eq!(
Vec::from(&out),
[Some(false), Some(false), Some(true), Some(true)]
);
Ok(())
}
}