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

mean has inconsistent behavior when passed an empty collection #117

Open
yurivish opened this issue Jun 27, 2022 · 2 comments
Open

mean has inconsistent behavior when passed an empty collection #117

yurivish opened this issue Jun 27, 2022 · 2 comments

Comments

@yurivish
Copy link

The mean function does not consistently treat empty iterators, arrays, and general iterables. None of the current behaviors are documented.

The behavior for ranges (AbstractRange{<:Real}) is to return NaN:

julia> using Statistics
julia> mean(1:0)
NaN

The behavior for arrays is to throw an error:

julia> mean([])
ERROR: MethodError: no method matching zero(::Type{Any})

The behavior for general iterables is to throw a different error:

julia> mean(x for x in ())
ERROR: ArgumentError: reducing over an empty collection is not allowed
@yurivish yurivish changed the title mean has inconsistent behavior when passed an empty collection mean has inconsistent behavior when passed an empty collection Jun 27, 2022
@jishnub
Copy link

jishnub commented Jul 1, 2022

Note that these are consistent:

julia> mean(Int[])
NaN

julia> mean(1:0)
NaN

although I'm uncertain if this is accidental rather than the intended result.

@ararslan
Copy link
Member

ararslan commented Jul 6, 2022

While I agree that the user experience isn't optimal, note that this isn't specific to mean: sum exhibits identical behaviors for these examples. It comes down to whether the correct output type is knowable based on the element type of the input. Range literals always have a real, concrete element type, so zero(T) is defined, which means we can return zero(T) / 0 for the mean. The same is true of Int[], which is why @jishnub's examples match. The same is not true of [], which has element type Any, and thus you get the MethodError noted in the OP. The reasoning behind the ArgumentError for the generator is the same, and that error is arguably more informative for the user than the MethodError.

I'd vote that the documentation for sum and mean both be updated to note that an error is thrown for empty collections unless the element type supports zero (or the init keyword is passed in the case of sum), without specifying what the precise error is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants