Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding error message to date repeat #149

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions __tests__/functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ describe("concat", () => {
});
it("cant concat empty list", () => {
const fn = () => pl.concat([]);
expect(fn).toThrowError();
expect(fn).toThrow();
});

it("can only concat series and df", () => {
const fn = () => pl.concat([[1] as any, [2] as any]);
expect(fn).toThrowError();
expect(fn).toThrow();
});
test("horizontal concat", () => {
const a = pl.DataFrame({ a: ["a", "b"], b: [1, 2] });
Expand Down Expand Up @@ -75,6 +75,10 @@ describe("repeat", () => {

expect(actual).toSeriesEqual(expected);
});
it("fail to repeat a date value", () => {
const fn = () => pl.repeat(new Date(), 4, "foo");
expect(fn).toThrow();
});
});
describe("horizontal", () => {
it("compute the bitwise AND horizontally across columns.", () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/series.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,10 +546,10 @@ describe("series", () => {
}
});
it("describe", () => {
expect(() => pl.Series([]).describe()).toThrowError(
expect(() => pl.Series([]).describe()).toThrow(
"Series must contain at least one value",
);
expect(() => pl.Series("dt", [null], pl.Date).describe()).toThrowError(
expect(() => pl.Series("dt", [null], pl.Date).describe()).toThrow(
"Invalid operation: describe is not supported for DataType(Date)",
);
let actual = pl.Series([true, false, true]).describe();
Expand Down
55 changes: 25 additions & 30 deletions __tests__/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ expect.extend({
message: () => "series matches",
pass: true,
};
} else {
return {
message: () => `
}
return {
message: () => `
Expected:
>>${expected}
Received:
>>${actual}`,
pass: false,
};
}
pass: false,
};
},
toSeriesEqual(actual, expected) {
const pass = actual.seriesEqual(expected);
Expand All @@ -27,16 +26,15 @@ Received:
message: () => "series matches",
pass: true,
};
} else {
return {
message: () => `
}
return {
message: () => `
Expected:
>>${expected}
Received:
>>${actual}`,
pass: false,
};
}
pass: false,
};
},
toFrameEqual(actual, expected, nullEqual?) {
const pass = actual.frameEqual(expected, nullEqual);
Expand All @@ -45,16 +43,15 @@ Received:
message: () => "dataframes match",
pass: true,
};
} else {
return {
message: () => `
}
return {
message: () => `
Expected:
>>${expected}
Received:
>>${actual}`,
pass: false,
};
}
pass: false,
};
},
toFrameStrictEqual(actual, expected) {
const frameEq = actual.frameEqual(expected);
Expand All @@ -64,16 +61,15 @@ Received:
message: () => "dataframes match",
pass: true,
};
} else {
return {
message: () => `
}
return {
message: () => `
Expected:
>>${expected}
Received:
>>${actual}`,
pass: false,
};
}
pass: false,
};
},
toFrameEqualIgnoringOrder(act: pl.DataFrame, exp: pl.DataFrame) {
const actual = act.sort(act.columns.sort());
Expand All @@ -84,16 +80,15 @@ Received:
message: () => "dataframes match",
pass: true,
};
} else {
return {
message: () => `
}
return {
message: () => `
Expected:
>>${expected}
Received:
>>${actual}`,
pass: false,
};
}
pass: false,
};
},
});

Expand Down
49 changes: 18 additions & 31 deletions polars/dataframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1708,9 +1708,8 @@ export interface DataFrame
function prepareOtherArg(anyValue: any): Series {
if (Series.isSeries(anyValue)) {
return anyValue;
} else {
return Series([anyValue]) as Series;
}
return Series([anyValue]) as Series;
}

function map(df: DataFrame, fn: (...args: any[]) => any[]) {
Expand Down Expand Up @@ -1841,9 +1840,8 @@ export const _DataFrame = (_df: any): DataFrame => {
df.getColumns().map((s) => {
if (s.isNumeric() || s.isBoolean()) {
return s.cast(DataType.Float64);
} else {
return s;
}
return s;
}),
);
};
Expand Down Expand Up @@ -1877,9 +1875,8 @@ export const _DataFrame = (_df: any): DataFrame => {
dropNulls(...subset) {
if (subset.length) {
return wrap("dropNulls", subset.flat(2));
} else {
return wrap("dropNulls");
}
return wrap("dropNulls");
},
distinct(opts: any = false, subset?, keep = "first") {
return this.unique(opts, subset);
Expand Down Expand Up @@ -2037,9 +2034,8 @@ export const _DataFrame = (_df: any): DataFrame => {
max(axis = 0) {
if (axis === 1) {
return _Series(_df.hmax() as any) as any;
} else {
return wrap("max");
}
return wrap("max");
},
mean(axis = 0, nullStrategy = "ignore") {
if (axis === 1) {
Expand All @@ -2057,9 +2053,8 @@ export const _DataFrame = (_df: any): DataFrame => {
min(axis = 0) {
if (axis === 1) {
return _Series(_df.hmin() as any) as any;
} else {
return wrap("min");
}
return wrap("min");
},
nChunks() {
return _df.nChunks();
Expand Down Expand Up @@ -2168,28 +2163,25 @@ export const _DataFrame = (_df: any): DataFrame => {
false,
seed,
);
} else {
throw new TypeError("must specify either 'frac' or 'n'");
}
throw new TypeError("must specify either 'frac' or 'n'");
},
select(...selection) {
const hasExpr = selection.flat().some((s) => Expr.isExpr(s));
if (hasExpr) {
return _DataFrame(_df).lazy().select(selection).collectSync();
} else {
return wrap("select", columnOrColumnsStrict(selection as any));
}
return wrap("select", columnOrColumnsStrict(selection as any));
},
shift: (opt) => wrap("shift", opt?.periods ?? opt),
shiftAndFill(n: any, fillValue?: number | undefined) {
if (typeof n === "number" && fillValue) {
return _DataFrame(_df).lazy().shiftAndFill(n, fillValue).collectSync();
} else {
return _DataFrame(_df)
.lazy()
.shiftAndFill(n.n, n.fillValue)
.collectSync();
}
return _DataFrame(_df)
.lazy()
.shiftAndFill(n.n, n.fillValue)
.collectSync();
},
shrinkToFit(inPlace: any = false): any {
if (inPlace) {
Expand Down Expand Up @@ -2408,9 +2400,8 @@ export const _DataFrame = (_df: any): DataFrame => {
}
if (!options?.columnNames) {
return wrap("transpose", keep_names_as, undefined);
} else {
return wrap("transpose", keep_names_as, options.columnNames);
}
return wrap("transpose", keep_names_as, options.columnNames);
},
unnest(names) {
names = Array.isArray(names) ? names : [names];
Expand All @@ -2428,28 +2419,25 @@ export const _DataFrame = (_df: any): DataFrame => {
withColumn(column: Series | Expr) {
if (Series.isSeries(column)) {
return wrap("withColumn", column.inner());
} else {
return this.withColumns(column);
}
return this.withColumns(column);
},
withColumns(...columns: (Expr | Series)[]) {
if (isSeriesArray(columns)) {
return columns.reduce(
(acc, curr) => acc.withColumn(curr),
_DataFrame(_df),
);
} else {
return this.lazy()
.withColumns(columns)
.collectSync({ noOptimization: true, stringCache: false });
}
return this.lazy()
.withColumns(columns)
.collectSync({ noOptimization: true, stringCache: false });
},
withColumnRenamed(opt, replacement?) {
if (typeof opt === "string") {
return this.rename({ [opt]: replacement });
} else {
return this.rename({ [opt.existing]: opt.replacement });
}
return this.rename({ [opt.existing]: opt.replacement });
},
withRowCount(name = "row_nr") {
return wrap("withRowCount", name);
Expand Down Expand Up @@ -2477,9 +2465,8 @@ export const _DataFrame = (_df: any): DataFrame => {
}
if (typeof prop !== "symbol" && !Number.isNaN(Number(prop))) {
return target.row(Number(prop));
} else {
return Reflect.get(target, prop, receiver);
}
return Reflect.get(target, prop, receiver);
},
set(target: DataFrame, prop, receiver) {
if (Series.isSeries(receiver)) {
Expand Down
19 changes: 7 additions & 12 deletions polars/datatypes/datatype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,8 @@ export abstract class DataType {
toString() {
if (this.inner) {
return `${this.identity}(${this.variant}(${this.inner}))`;
} else {
return `${this.identity}(${this.variant})`;
}
return `${this.identity}(${this.variant})`;
}
toJSON() {
const inner = (this as any).inner;
Expand All @@ -136,11 +135,10 @@ export abstract class DataType {
[this.variant]: inner[0],
},
};
} else {
return {
[this.identity]: this.variant,
};
}
return {
[this.identity]: this.variant,
};
}
[Symbol.for("nodejs.util.inspect.custom")]() {
return this.toJSON();
Expand Down Expand Up @@ -186,9 +184,8 @@ class _Datetime extends DataType {
this.timeUnit === (other as _Datetime).timeUnit &&
this.timeZone === (other as _Datetime).timeZone
);
} else {
return false;
}
return false;
}
}

Expand All @@ -202,9 +199,8 @@ class _List extends DataType {
override equals(other: DataType): boolean {
if (other.variant === this.variant) {
return this.inner[0].equals((other as _List).inner[0]);
} else {
return false;
}
return false;
}
}

Expand Down Expand Up @@ -237,9 +233,8 @@ class _Struct extends DataType {
return otherfld.name === fld.name && otherfld.dtype.equals(fld.dtype);
})
.every((value) => value);
} else {
return false;
}
return false;
}
override toJSON() {
return {
Expand Down
6 changes: 3 additions & 3 deletions polars/datatypes/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export namespace Field {
export function from(nameOrObj, dtype?: DataType): Field {
if (typeof nameOrObj === "string" && dtype) {
return new Field(nameOrObj, dtype);
} else if (Array.isArray(nameOrObj)) {
}
if (Array.isArray(nameOrObj)) {
return new Field(nameOrObj[0], nameOrObj[1]);
} else {
return new Field(nameOrObj.name, nameOrObj.dtype);
}
return new Field(nameOrObj.name, nameOrObj.dtype);
}
}
Loading