Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add CategoricalValue(x, source) and disallow mixed isless and < #346

Merged
merged 2 commits into from
Apr 23, 2021

Conversation

nalimilan
Copy link
Member

isless and < between CategoricalValue and other types can breaks transitivity. Require converting the value to a CategoricalValue, attaching it to a pool, using CategoricalValue(x, source::Union{CategoricalValue, CategoricalArray).
Deprecate CategoricalValue(i::Integer, pool::CategoricalPool) in favor of pool[i] or CategoricalValue(pool, i), as the order is more logical and it avoids a potential ambiguity if source::CategoricalPool is allowed in the future.

Fixes #319.

`isless` and `<` between `CategoricalValue` and other types can breaks
transitivity. Require converting the value to a `CategoricalValue`,
attaching it to a pool, using
`CategoricalValue(x, source::Union{CategoricalValue, CategoricalArray)`.
Deprecate `CategoricalValue(i::Integer, pool::CategoricalPool)` in favor
of `pool[i]` or `CategoricalValue(pool, i)`, as the order is more logical
and it avoids a potential ambiguity if `source::CategoricalPool` is allowed
in the future.
@nalimilan nalimilan requested a review from bkamins April 22, 2021 21:42
@@ -34,10 +34,10 @@ in(x::Missing, y::CategoricalArray{>:Missing}) = !all(v -> v > 0, y.refs)

function Missings.replace(a::CategoricalArray{S, N, R, V, C}, replacement::V) where {S, N, R, V, C}
pool = copy(a.pool)
v = C(get!(pool, replacement), pool)
v = C(pool, get!(pool, replacement))
Missings.replace(a, v)
end

function collect(r::Missings.EachReplaceMissing{<:CategoricalArray{S, N, R, C}}) where {S, N, R, C}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is a bit confusing that you use C to mean what is named V above. Maybe make it consistent?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. Better always use the same names as in the type definition.

pool::CategoricalPool{T, R, CategoricalValue{T, R}}
ref::R
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

smart - I was thinking how you have changed it - and it is just super simple :).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I wanted to change their order for a long time, but there's never been a strong enough reason to do it. :-)

@@ -140,8 +153,11 @@ function Base.isless(x::CategoricalValue, y::CategoricalValue)
end
end

Base.isless(x::CategoricalValue, y::SupportedTypes) = levelcode(x) < levelcode(x.pool[get(x.pool, y)])
Base.isless(y::SupportedTypes, x::CategoricalValue) = levelcode(x.pool[get(x.pool, y)]) < levelcode(x)
Base.isless(x::CategoricalValue, y::SupportedTypes) =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so you have decided to go forward fast and be breaking here? (I am OK with this, but usually you wanted one round of releases with deprecations 😄 )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes maybe I can backport a deprecation to 0.9. Though the error message is basically the same as a deprecation so I wonder whether it's worth it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am OK with error as commented.

Copy link
Member

@bkamins bkamins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good except for small comments

@nalimilan nalimilan merged commit 4abf776 into master Apr 23, 2021
@nalimilan nalimilan deleted the nl/comp2 branch April 23, 2021 20:46
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generic test for allowing to use isless
2 participants