Skip to content

Commit

Permalink
fix offsets in categorical merge (#3242)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Apr 27, 2022
1 parent 0230a2f commit 5f4c91b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ impl CategoricalChunked {
other: &CategoricalChunked,
) -> Result<Self> {
let cats = match &**self.get_rev_map() {
RevMapping::Local(_) => {
RevMapping::Local(rev_map) => {
// the logic for merging the rev maps will concatenate utf8 arrays
// to make sure the indexes still make sense we need to offset the right hand side
self.logical()
.zip_with(mask, &(other.logical() + self.logical.len() as u32))?
.zip_with(mask, &(other.logical() + rev_map.len() as u32))?
}
_ => self.logical().zip_with(mask, other.logical())?,
};

let new_state = self.merge_categorical_map(other);
Ok(CategoricalChunked::from_cats_and_rev_map(cats, new_state))
}
Expand Down
36 changes: 36 additions & 0 deletions polars/tests/it/lazy/expressions/arity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,39 @@ fn includes_null_predicate_3038() -> Result<()> {

Ok(())
}

#[test]
fn test_when_then_otherwise_cats() -> Result<()> {
let lf = df!["book" => [Some("bookA"),
None,
Some("bookB"),
None,
Some("bookA"),
Some("bookC"),
Some("bookC"),
Some("bookC")],
"user" => [Some("bob"), Some("bob"), Some("bob"), Some("tim"), Some("lucy"), Some("lucy"), None, None]
]?.lazy();

let out = lf
.with_column(col("book").cast(DataType::Categorical(None)))
.with_column(col("user").cast(DataType::Categorical(None)))
.with_column(
when(col("book").eq(Null {}.lit()))
.then(col("user"))
.otherwise(col("book"))
.alias("a"),
)
.collect()?;

assert_eq!(
out.column("a")?
.categorical()?
.iter_str()
.flatten()
.collect::<Vec<_>>(),
&["bookA", "bob", "bookB", "tim", "bookA", "bookC", "bookC", "bookC"]
);

Ok(())
}

0 comments on commit 5f4c91b

Please sign in to comment.