Steven D'Aprano added the comment:

On 04/08/13 05:31, Alexander Belopolsky wrote:
>
> Alexander Belopolsky added the comment:
>
> Here is the use-case that was presented to support adding additional 
> operations on timedelta objects:
>
> """
> I'm conducting a series of observation experiments where I
> measure the duration of an event.  I then want to do various
> statistical analysis such as computing the mean, median,
> etc.  Originally, I tried using standard functions such as
> lmean from the stats.py package.  However, these sorts of
> functions divide by a float at the end, causing them to fail
> on timedelta objects.  Thus, I have to either write my own
> special functions, or convert the timedelta objects to
> integers first (then convert them back afterwards).
> """  (Daniel Stutzbach, in msg26267 on issue1289118.)
>
> The proposed statistics module does not support this use case:
[...]
> TypeError: sum only accepts numbers

That's a nice use-case, but I'm not sure how to solve it, or whether it needs 
to be.

I'm not going to add support for timedelta objects as a special-case. Once we 
start special-casing types, where will it end?

At first I thought that registering timedelta as a numeric type would help, but 
that is a slightly dubious thing to do since timedelta doesn't support all 
numeric operations:

py> datetime.timedelta(1, 1, 1)+2
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'datetime.timedelta' and 'int'

(What would that mean, anyway? Add two days, two seconds, or two milliseconds?)

Perhaps timedelta objects should be enhanced to be (Integral?) numbers. In the 
meantime, there's a simple way to do this:

py> from datetime import timedelta as td
py> data = [td(2), td(1), td(3), td(4)]
py> m = statistics.mean([x.total_seconds() for x in data])
py> m
216000.0
py> td(seconds=m)
datetime.timedelta(2, 43200)

And for standard deviation:

py> s = statistics.stdev([x.total_seconds() for x in data])
py> td(seconds=s)
datetime.timedelta(1, 25141, 920371)

median works without any wrapper:

py> statistics.median(data)
datetime.timedelta(2, 43200)

I'm now leaning towards "will not fix" for supporting timedelta objects. If 
they become proper numbers, then they should just work, and if they don't, 
supporting them just requires a tiny bit of extra code.

However, I will add documentation and tests for them.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue18606>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to