# Mean of Period

So reading the documentation, if you take a difference of DateTime you get an Period outputs (year, minute etc) and these are supposed to behave like integers. But if I would like the average period time (ex `mean([Second(2), Second(2), Second(3)])`). I get an error. But you can easily take the mean of an integer vector. Is this a design flaw? I know there is the workaround of `mean(Dates.value.([Second(2), Second(2), Second(3)]))`

this is because you canâ€™t have float number of `Second`.

``````julia> Second(2.33)
ERROR: InexactError: Int64(2.33)
``````
2 Likes

What is happening there is that the return value of `mean` is of the same type of the elements of the input array or a type to which a promotion makes sense.

Since the mean can be fractional, the mean cannot be of type `Second` (although it works when the mean is an integer:

``````julia> d = [ Second(2), Second(4) ]
2-element Vector{Second}:
2 seconds
4 seconds

julia> mean(d)
3 seconds

``````

That said, there is a inner function in `Statistics` which is `_mean` and receives a function that will allow you to promote â€śby handâ€ť the result to float:

``````julia> Statistics._mean(Dates.value,[ Second(1), Second(2) ])
1.5

``````

The problem, thus, is to decide to what that result should be promoted. A specific method of `mean` could be implemented for that if it turns out to be used, but the workarounds are fairly quick, so I am not sure if it would be worth the case.

1 Like