Skip to content

Commit 5530985

Browse files
Copilotmrjf
andauthored
Address review feedback in CI typecheck fixes
Agent-Logs-Url: https://github.com/githubnext/tsessebe/sessions/2715e479-20de-4885-bb80-bc9bbae1f654 Co-authored-by: mrjf <180956+mrjf@users.noreply.github.com>
1 parent 29f95d9 commit 5530985

11 files changed

Lines changed: 43 additions & 34 deletions

File tree

src/core/align.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export function alignSeries<L extends Scalar, R extends Scalar>(
125125
left: Series<L>,
126126
right: Series<R>,
127127
options: AlignSeriesOptions = {},
128-
): [Series<Scalar>, Series<Scalar>] {
128+
): [Series<L>, Series<R>] {
129129
const { join = "outer", fillValue = null } = options;
130130
const targetIdx = resolveIndex(left.index, right.index, join);
131131

src/core/base-index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ export class Index<T extends Label = Label> {
7070
}
7171

7272
/** Snapshot of the underlying values as a plain array. */
73-
get values(): T[] {
74-
return [...this._values];
73+
get values(): readonly T[] {
74+
return this._values;
7575
}
7676

7777
/** True when every label appears exactly once. */

src/core/frame.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -888,27 +888,15 @@ function reindexColumns(
888888
): Map<string, Series<Scalar>> {
889889
const result = new Map<string, Series<Scalar>>();
890890
for (const [name, series] of colMap) {
891-
if (series.size === newIndex.size) {
892-
result.set(
893-
name,
894-
new Series({
895-
data: series.values,
896-
index: newIndex,
897-
dtype: series.dtype,
898-
...(series.name !== null ? { name: series.name } : {}),
899-
}),
891+
if (series.size !== newIndex.size) {
892+
throw new RangeError(
893+
`Index length ${newIndex.size} does not match data length ${series.size} for column "${name}"`,
900894
);
901-
continue;
902-
}
903-
904-
const resized: Scalar[] = new Array<Scalar>(newIndex.size);
905-
for (let i = 0; i < newIndex.size; i++) {
906-
resized[i] = series.values[i] ?? null;
907895
}
908896
result.set(
909897
name,
910898
new Series({
911-
data: resized,
899+
data: series.values,
912900
index: newIndex,
913901
dtype: series.dtype,
914902
...(series.name !== null ? { name: series.name } : {}),

src/core/reindex.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export function reindexSeries<T extends Scalar>(
252252
series: Series<T>,
253253
newIndex: readonly Label[] | Index<Label>,
254254
options: ReindexSeriesOptions = {},
255-
): Series<Scalar> {
255+
): Series<T> {
256256
const { fillValue = null, method, limit } = options;
257257

258258
const newIdx = toIndex(newIndex);
@@ -279,8 +279,8 @@ export function reindexSeries<T extends Scalar>(
279279
const finalValues =
280280
method !== undefined ? applyFillMethod(resultValues, present, method, limit) : resultValues;
281281

282-
return new Series<Scalar>({
283-
data: finalValues,
282+
return new Series<T>({
283+
data: finalValues as T[],
284284
index: newIdx,
285285
name: series.name,
286286
});

src/groupby/groupby.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,25 @@ export class DataFrameGroupBy {
340340
throw new TypeError(`aggNamed: "${outCol}" must be a NamedAgg`);
341341
}
342342
const src = named.column;
343-
const fn = resolveAgg(named.aggfunc as AggName | AggFn);
343+
const aggSpec = named.aggfunc;
344+
let fn: AggFn;
345+
if (typeof aggSpec === "function") {
346+
fn = resolveAgg(aggSpec);
347+
} else if (
348+
aggSpec === "sum" ||
349+
aggSpec === "mean" ||
350+
aggSpec === "min" ||
351+
aggSpec === "max" ||
352+
aggSpec === "count" ||
353+
aggSpec === "std" ||
354+
aggSpec === "first" ||
355+
aggSpec === "last" ||
356+
aggSpec === "size"
357+
) {
358+
fn = resolveAgg(aggSpec);
359+
} else {
360+
throw new TypeError(`aggNamed: unsupported aggfunc "${aggSpec}" for column "${outCol}"`);
361+
}
344362
const srcSeries = this._df.col(src);
345363
const srcVals = srcSeries.values as readonly Scalar[];
346364
resultCols[outCol] = this._groups.map((g) => fn(g.positions.map((p) => srcVals[p] as Scalar)));

src/groupby/named_agg.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ export class NamedAgg {
4040
/** Source column to read from the DataFrame. */
4141
readonly column: string;
4242
/** Aggregation to apply — a built-in name or a custom function. */
43-
readonly aggfunc: AggName | AggFn | string;
43+
readonly aggfunc: AggName | AggFn;
4444

45-
constructor(column: string, aggfunc: AggName | AggFn | string) {
45+
constructor(column: string, aggfunc: AggName | AggFn) {
4646
this.column = column;
4747
this.aggfunc = aggfunc;
4848
}
@@ -59,7 +59,7 @@ export class NamedAgg {
5959
* });
6060
* ```
6161
*/
62-
export function namedAgg(column: string, aggfunc: AggName | AggFn | string): NamedAgg {
62+
export function namedAgg(column: string, aggfunc: AggName | AggFn): NamedAgg {
6363
return new NamedAgg(column, aggfunc);
6464
}
6565

src/stats/crosstab.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export function crosstab(
202202
const buckets: Array<Array<number[] | undefined>> | null =
203203
values !== undefined
204204
? Array.from({ length: nRows }, () =>
205-
Array.from({ length: nCols }, () => undefined as number[] | undefined),
205+
Array.from<undefined, number[] | undefined>({ length: nCols }, () => undefined),
206206
)
207207
: null;
208208

tests/core/align.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import { DataFrame } from "../../src/core/frame.ts";
44
import { Index } from "../../src/core/base-index.ts";
55
import { Series } from "../../src/core/series.ts";
66
import { alignSeries, alignDataFrame } from "../../src/core/align.ts";
7+
import type { Scalar } from "../../src/index.ts";
78

89
// ─── helpers ──────────────────────────────────────────────────────────────────
910

10-
function ns(data: number[], labels: string[], name?: string): Series<number> {
11-
return new Series<number>({
11+
function ns(data: number[], labels: string[], name?: string): Series<Scalar> {
12+
return new Series<Scalar>({
1213
data,
1314
index: new Index<string>(labels),
1415
name: name ?? null,

tests/core/reindex.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import { DataFrame } from "../../src/core/frame.ts";
44
import { Index } from "../../src/core/base-index.ts";
55
import { Series } from "../../src/core/series.ts";
66
import { reindexDataFrame, reindexSeries } from "../../src/core/reindex.ts";
7+
import type { Scalar } from "../../src/index.ts";
78

89
// ─── helpers ──────────────────────────────────────────────────────────────────
910

1011
function makeNumericSeries(
1112
data: number[],
1213
labels: string[],
1314
name?: string,
14-
): Series<number> {
15-
return new Series<number>({
15+
): Series<Scalar> {
16+
return new Series<Scalar>({
1617
data,
1718
index: new Index<string>(labels),
1819
name: name ?? null,

tests/groupby/named_agg.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { describe, expect, it } from "bun:test";
55
import fc from "fast-check";
66
import { DataFrame, NamedAgg, isNamedAggSpec, namedAgg } from "../../src/index.ts";
77
import type { Scalar } from "../../src/types.ts";
8+
import type { AggName } from "../../src/groupby/index.ts";
89

910
// ─── helpers ──────────────────────────────────────────────────────────────────
1011

@@ -270,7 +271,7 @@ describe("NamedAgg property tests", () => {
270271
fc.assert(
271272
fc.property(
272273
fc.string({ minLength: 1 }),
273-
fc.constantFrom("sum", "mean", "min", "max", "count" as const),
274+
fc.constantFrom<AggName>("sum", "mean", "min", "max", "count"),
274275
(col, agg) => {
275276
const spec = new NamedAgg(col, agg);
276277
expect(spec.column).toBe(col);

0 commit comments

Comments
 (0)