Message194324
> Once we start special-casing types, where will it end?
At the point where all stdlib types are special-cased. :-)
> 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> td(seconds=m)
datetime.timedelta(2, 43200)
Simple, but as simple ways go in this area not correct. Here is the right way:
py> td.resolution * statistics.mean(d//td.resolution for d in data)
datetime.timedelta(2, 43200)
I wish I had a solution to make sum() work properly on timedeltas without special-casing. I thought that start could default to type(data[0])(0), but that would bring in strings. Maybe statistics.mean() should support non-numbers that support addition and division by a number? Will it be too confusing if mean() supports types that sum() does not? |
|
Date |
User |
Action |
Args |
2013-08-04 02:07:58 | belopolsky | set | recipients:
+ belopolsky, gregory.p.smith, ronaldoussoren, pitrou, agthorr, christian.heimes, steven.daprano, tshepang, vajrasky |
2013-08-04 02:07:58 | belopolsky | set | messageid: <1375582078.36.0.764112705453.issue18606@psf.upfronthosting.co.za> |
2013-08-04 02:07:58 | belopolsky | link | issue18606 messages |
2013-08-04 02:07:58 | belopolsky | create | |
|