Skip to content

Commit bfc6326

Browse files
authored
Fix quantile with Date and DateTime (#153)
Before #145 `Date` and `DateTime` were supported with `quantile` as long as the cut point falls between two equal values. Restore this behavior as some code may rely on this given that it is the most common situation with large datasets.
1 parent b8ea3d2 commit bfc6326

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1919
SparseArraysExt = ["SparseArrays"]
2020

2121
[extras]
22+
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
2223
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2324
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
2425
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2526

2627
[targets]
27-
test = ["Random", "SparseArrays", "Test"]
28+
test = ["Dates", "Random", "SparseArrays", "Test"]

src/Statistics.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,8 +1026,8 @@ end
10261026

10271027
# When a ≉ b, b-a may overflow
10281028
# When a ≈ b, (1-γ)*a + γ*b may not be increasing with γ due to rounding
1029-
# Call to float is to work around JuliaLang/julia#50380
1030-
if isfinite(a) && isfinite(b) && float(a) float(b)
1029+
if isfinite(a) && isfinite(b) &&
1030+
(!(a isa Number) || !(b isa Number) || a b)
10311031
return a + γ*(b-a)
10321032
else
10331033
return (1-γ)*a + γ*b

test/runtests.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This file is a part of Julia. License is MIT: https://julialang.org/license
22

3-
using Statistics, Test, Random, LinearAlgebra, SparseArrays
3+
using Statistics, Test, Random, LinearAlgebra, SparseArrays, Dates
44
using Test: guardseed
55

66
Random.seed!(123)
@@ -784,6 +784,17 @@ end
784784
@test issorted(quantile([1.0, 1.0+2eps(), 1.0+4eps(), 1.0+6eps()], range(0, 1, length=100)))
785785
end
786786

787+
@testset "quantiles with Date and DateTime" begin
788+
# this is the historical behavior
789+
@test quantile([Date(2023, 09, 02)], .1) == Date(2023, 09, 02)
790+
@test quantile([Date(2023, 09, 02), Date(2023, 09, 02)], .1) == Date(2023, 09, 02)
791+
@test_throws InexactError quantile([Date(2023, 09, 02), Date(2023, 09, 03)], .1)
792+
793+
@test quantile([DateTime(2023, 09, 02)], .1) == DateTime(2023, 09, 02)
794+
@test quantile([DateTime(2023, 09, 02), DateTime(2023, 09, 02)], .1) == DateTime(2023, 09, 02)
795+
@test_throws InexactError quantile([DateTime(2023, 09, 02), DateTime(2023, 09, 03)], .1)
796+
end
797+
787798
@testset "variance of complex arrays (#13309)" begin
788799
z = rand(ComplexF64, 10)
789800
@test var(z) invoke(var, Tuple{Any}, z) cov(z) var(z,dims=1)[1] sum(abs2, z .- mean(z))/9

0 commit comments

Comments
 (0)