Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add isordered #26

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
name = "DataAPI"
uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
authors = ["quinnj <[email protected]>"]
version = "1.4.0"
version = "1.5.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

[compat]
julia = "1"
Expand Down
25 changes: 25 additions & 0 deletions src/DataAPI.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module DataAPI

import Dates

"""
defaultarray(T, N)

Expand Down Expand Up @@ -89,6 +91,9 @@ If the collection is not sortable then the order of levels is unspecified.

Contrary to [`unique`](@ref), this function may return values which do not
actually occur in the data, and does not preserve their order of appearance in `x`.

If a type of `A` implements `levels` with a meaningful order then it should also
implement `isordered` as by default `isordered` returns `false`.
"""
function levels(x)
T = Base.nonmissingtype(eltype(x))
Expand All @@ -100,6 +105,26 @@ function levels(x)
levs
end

"""
isordered(A)

Test whether entries in `A` can be compared using `<`, `>` and similar operators,
using the ordering of `levels(A)`.

If a type of `A` implements `levels` with a meaningful order then it should also
Copy link
Member Author

Choose a reason for hiding this comment

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

@nalimilan - could you please comment in the implementation how your proposal should be incorporated? Thank you!

implement `isordered` as by default `isordered` returns `false`.
"""
function isordered end

isordered(::AbstractArray{<:Union{Missing, AbstractString}}) = true
isordered(::AbstractArray{<:Union{Missing, Real}}) = true
isordered(::AbstractArray{<:Union{Missing, Symbol}}) = true
isordered(::AbstractArray{<:Union{Missing, AbstractChar}}) = true
isordered(::AbstractArray{<:Union{Missing, Dates.Period}}) = true
isordered(::AbstractArray{<:Union{Missing, Dates.TimeType}}) = true
isordered(::AbstractArray{Missing}) = true
isordered(::AbstractArray{<:Any}) = false

"""
Between(first, last)

Expand Down
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,17 @@ end
end

end

@testset "isordered" begin

@test DataAPI.isordered([1])
@test DataAPI.isordered(Union{Real,Missing}[1])
@test DataAPI.isordered(["1"])
@test DataAPI.isordered(['1'])
@test !DataAPI.isordered(Any[1])
@test !DataAPI.isordered(["1", '1'])
@test !DataAPI.isordered(Union{Char, String}["1", '1'])

end

end # @testset "DataAPI"