Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.
How to use
Install via yarn add locutus and import:
import { median_grouped } from 'locutus/python/statistics/median_grouped'.
Or with CommonJS: const { median_grouped } = require('locutus/python/statistics/median_grouped')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
#
code
expected result
1
median_grouped([52, 52, 53, 54])
52.5
2
median_grouped([52, 52, 53, 54], 2)
52
3
median_grouped([true, false, true])
0.75
Notes
Estimates the median for data binned around interval midpoints using grouped interpolation.
Dependencies
This function uses the following Locutus functions:
functionassertStatisticsArray(data: unknown, functionName: string): unknown[] { if (!Array.isArray(data)) { thrownewTypeError(`${functionName}() data must be an array`) }
const firstKind = getStatisticsSortKind(values[0], functionName) for (let index = 1; index < values.length; index += 1) { const currentKind = getStatisticsSortKind(values[index], functionName) if (currentKind !== firstKind) { thrownewTypeError(`${functionName}() requires data values that can be ordered together`) } }
functiontoFloatStatisticNumber(value: unknown, functionName: string): number { if (typeof value === 'number') { return value }
if (typeof value === 'boolean') { return value ? 1 : 0 }
if (typeof value === 'bigint') { returnNumber(value) }
if (typeof value === 'string') { const parsed = Number(value) if (!Number.isNaN(parsed)) { return parsed } thrownewTypeError('Value cannot be converted to a float') }
thrownewTypeError(`${functionName}() data must contain only real numbers`) }
functiongetStatisticsSortKind(value: unknown, functionName: string): 'number' | 'string' { if (typeof value === 'string') { return'string' }
if (typeof value === 'number' || typeof value === 'boolean' || typeof value === 'bigint') { return'number' }
// python/_helpers/_statistics (Locutus helper dependency) functionassertStatisticsArray(data, functionName) { if (!Array.isArray(data)) { thrownewTypeError(`${functionName}() data must be an array`) }
const firstKind = getStatisticsSortKind(values[0], functionName) for (let index = 1; index < values.length; index += 1) { const currentKind = getStatisticsSortKind(values[index], functionName) if (currentKind !== firstKind) { thrownewTypeError(`${functionName}() requires data values that can be ordered together`) } }
functiontoFloatStatisticNumber(value, functionName) { if (typeof value === 'number') { return value }
if (typeof value === 'boolean') { return value ? 1 : 0 }
if (typeof value === 'bigint') { returnNumber(value) }
if (typeof value === 'string') { const parsed = Number(value) if (!Number.isNaN(parsed)) { return parsed } thrownewTypeError('Value cannot be converted to a float') }
thrownewTypeError(`${functionName}() data must contain only real numbers`) }
functiongetStatisticsSortKind(value, functionName) { if (typeof value === 'string') { return'string' }
if (typeof value === 'number' || typeof value === 'boolean' || typeof value === 'bigint') { return'number' }
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.
Click "New file" in the appropriate folder
on GitHub.
This will fork the project to your account, directly add the file to it, and send a
Pull Request to us.
We will then review it. If it's useful to the project and in line with our
contributing guidelines
your work will become part of Locutus and you'll be automatically credited
in the authors
section accordingly.