Skip to content

Commit

Permalink
fix: project should work with non-existing columns
Browse files Browse the repository at this point in the history
  • Loading branch information
fatmatto committed Sep 27, 2023
1 parent 01bed3a commit 243de56
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/lib/timeframe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,11 @@ test("TimeFrame.project() should correctly aggregate columns", (t) => {
t.is(tf.columns().length, 2);
t.is(projected.columns().length, 1);
t.is(projected.metadata.hello, "world");


const projected2 = tf.project({ columns: ['energy1', 'nonexisting'], skipMissingColumns: true })

t.is(projected2.columns().length, 1);
});

test("TimeFrame.mul() should correctly multiply values", (t) => {
Expand Down
17 changes: 11 additions & 6 deletions src/lib/timeframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,19 @@ export class TimeFrame {
* Returns a new timeframe with a subset of columns.
*/
project(config: ProjectionOptions): TimeFrame {
const nonExisting = config.columns.filter(
(name: string) => !this.columnNames.includes(name),
);
if (nonExisting.length > 0 && config.skipMissingColumns === false) {
throw new Error(`Non existing columns ${nonExisting.join(",")}`);
if (config.skipMissingColumns === false) {
const nonExisting = config.columns.filter(
(name: string) => !this.columnNames.includes(name),
);
if (nonExisting.length > 0) {
throw new Error(`Non existing columns ${nonExisting.join(",")}`);
}
}
const existingColumns = config.columns.filter(
(name: string) => this.columnNames.includes(name),
);
const tf = TimeFrame.fromTimeseries(
config.columns.map((columnName: string) => this.column(columnName)),
existingColumns.map((columnName: string) => this.column(columnName)),
);
tf.metadata = this.metadata;
return tf;
Expand Down

0 comments on commit 243de56

Please sign in to comment.