This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author steven.daprano
Recipients agthorr, belopolsky, gregory.p.smith, pitrou, ronaldoussoren, steven.daprano, tshepang, vajrasky
Date 2013-08-04.01:40:35
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <51FDB0FE.205@pearwood.info>
In-reply-to <1375558286.11.0.0812868181552.issue18606@psf.upfronthosting.co.za>
Content
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.
History
Date User Action Args
2013-08-04 01:40:36steven.dapranosetrecipients: + steven.daprano, gregory.p.smith, ronaldoussoren, belopolsky, pitrou, agthorr, tshepang, vajrasky
2013-08-04 01:40:36steven.dapranolinkissue18606 messages
2013-08-04 01:40:35steven.dapranocreate