@iiasa/ixmp4-ts 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DType = exports.DataFrame = void 0;
4
+ const exceptions_1 = require("./exceptions");
4
5
  /**
5
6
  * Represents the available data types for the DataFrame.
6
7
  */
@@ -24,7 +25,7 @@ const nonValueIamcColumns = [
24
25
  'type',
25
26
  ];
26
27
  /**
27
- * Represents a DataFrame, an immutable two-dimensional tabular data structure with labeled axes.
28
+ * Represents a DataFrame, an immutable two-dimensional row-based tabular data structure with labeled axes.
28
29
  */
29
30
  class DataFrame {
30
31
  /**
@@ -218,6 +219,47 @@ class DataFrame {
218
219
  iat(row, column) {
219
220
  return this._values[column][row];
220
221
  }
222
+ /**
223
+ * Executes a query on the DataFrame and returns a new DataFrame containing
224
+ * the rows that match the specified conditions.
225
+ * @param query.conditions - A single QueryCondition or an array of QueryCondition objects.
226
+ * @param query.conjunction - Optional. Specifies how to combine multiple conditions.
227
+ * Can be 'AND' or 'OR'. Defaults to 'AND'.
228
+ * @returns A new DataFrame containing the rows that match the query.
229
+ */
230
+ query(query) {
231
+ var _a;
232
+ const conditions = Array.isArray(query.conditions)
233
+ ? query.conditions
234
+ : [query.conditions];
235
+ const conjunction = (_a = query.conjunction) !== null && _a !== void 0 ? _a : 'AND';
236
+ conditions.forEach((condition) => {
237
+ if (typeof condition.column === 'string') {
238
+ condition.column = this._columns.indexOf(condition.column);
239
+ }
240
+ });
241
+ const illegalColumnns = conditions
242
+ .filter((condition) => condition.column === -1)
243
+ .map((condition) => condition.column);
244
+ if (illegalColumnns.length > 0) {
245
+ throw new exceptions_1.InvalidColumn(illegalColumnns);
246
+ }
247
+ const indices = this._values
248
+ .map((row, i) => {
249
+ if (conjunction === 'AND') {
250
+ return conditions.every((condition) => condition.predicate(row[condition.column]))
251
+ ? i
252
+ : -1;
253
+ }
254
+ else {
255
+ return conditions.some((condition) => condition.predicate(row[condition.column]))
256
+ ? i
257
+ : -1;
258
+ }
259
+ })
260
+ .filter((i) => i !== -1);
261
+ return this.iloc({ rows: indices });
262
+ }
221
263
  /**
222
264
  * Gets the data type for a specific column.
223
265
  * @param column - The column name.
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InvalidIndex = exports.InvalidColumn = exports.DataFrameError = void 0;
4
+ class DataFrameError extends Error {
5
+ constructor(message) {
6
+ super(message);
7
+ this.name = 'DataFrameError';
8
+ }
9
+ }
10
+ exports.DataFrameError = DataFrameError;
11
+ class InvalidColumn extends DataFrameError {
12
+ constructor(column) {
13
+ super(`Invalid column: ${Array.isArray(column) ? column.join(', ') : column}`);
14
+ this.name = 'InvalidColumn';
15
+ }
16
+ }
17
+ exports.InvalidColumn = InvalidColumn;
18
+ class InvalidIndex extends DataFrameError {
19
+ constructor(index) {
20
+ super(`Invalid index: ${Array.isArray(index) ? index.join(', ') : index}`);
21
+ this.name = 'InvalidIndex';
22
+ }
23
+ }
24
+ exports.InvalidIndex = InvalidIndex;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.merge = exports.toCsv = exports.toJson = exports.concat = exports.DType = exports.DataFrame = void 0;
3
+ exports.InvalidIndex = exports.InvalidColumn = exports.DataFrameError = exports.merge = exports.toCsv = exports.toJson = exports.concat = exports.DType = exports.DataFrame = void 0;
4
4
  var dataframe_1 = require("./dataframe");
5
5
  Object.defineProperty(exports, "DataFrame", { enumerable: true, get: function () { return dataframe_1.DataFrame; } });
6
6
  Object.defineProperty(exports, "DType", { enumerable: true, get: function () { return dataframe_1.DType; } });
@@ -9,3 +9,7 @@ Object.defineProperty(exports, "concat", { enumerable: true, get: function () {
9
9
  Object.defineProperty(exports, "toJson", { enumerable: true, get: function () { return utils_1.toJson; } });
10
10
  Object.defineProperty(exports, "toCsv", { enumerable: true, get: function () { return utils_1.toCsv; } });
11
11
  Object.defineProperty(exports, "merge", { enumerable: true, get: function () { return utils_1.merge; } });
12
+ var exceptions_1 = require("./exceptions");
13
+ Object.defineProperty(exports, "DataFrameError", { enumerable: true, get: function () { return exceptions_1.DataFrameError; } });
14
+ Object.defineProperty(exports, "InvalidColumn", { enumerable: true, get: function () { return exceptions_1.InvalidColumn; } });
15
+ Object.defineProperty(exports, "InvalidIndex", { enumerable: true, get: function () { return exceptions_1.InvalidIndex; } });
package/dist/cjs/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.merge = exports.toCsv = exports.toJson = exports.concat = exports.DType = exports.DataFrame = exports.InvalidCredentials = exports.BadFilterArguments = exports.InvalidRunMeta = exports.NoDefaultRunVersion = exports.SchemaError = exports.OperationNotSupported = exports.DeletionPrevented = exports.NotUnique = exports.NotFound = exports.Forbidden = exports.InvalidToken = exports.MissingToken = exports.PlatformNotUnique = exports.PlatformNotFound = exports.UnknownApiError = exports.ManagerApiError = exports.ImproperlyConfigured = exports.BadRequest = exports.InconsistentIamcType = exports.ProgrammingError = exports.IxmpError = exports.Platform = void 0;
3
+ exports.InvalidIndex = exports.InvalidColumn = exports.DataFrameError = exports.merge = exports.toCsv = exports.toJson = exports.concat = exports.DType = exports.DataFrame = exports.InvalidCredentials = exports.BadFilterArguments = exports.InvalidRunMeta = exports.NoDefaultRunVersion = exports.SchemaError = exports.OperationNotSupported = exports.DeletionPrevented = exports.NotUnique = exports.NotFound = exports.Forbidden = exports.InvalidToken = exports.MissingToken = exports.PlatformNotUnique = exports.PlatformNotFound = exports.UnknownApiError = exports.ManagerApiError = exports.ImproperlyConfigured = exports.BadRequest = exports.InconsistentIamcType = exports.ProgrammingError = exports.IxmpError = exports.Platform = void 0;
4
4
  var platform_1 = require("./core/platform");
5
5
  Object.defineProperty(exports, "Platform", { enumerable: true, get: function () { return platform_1.Platform; } });
6
6
  var exceptions_1 = require("./core/exceptions");
@@ -32,3 +32,6 @@ Object.defineProperty(exports, "concat", { enumerable: true, get: function () {
32
32
  Object.defineProperty(exports, "toJson", { enumerable: true, get: function () { return dataframe_1.toJson; } });
33
33
  Object.defineProperty(exports, "toCsv", { enumerable: true, get: function () { return dataframe_1.toCsv; } });
34
34
  Object.defineProperty(exports, "merge", { enumerable: true, get: function () { return dataframe_1.merge; } });
35
+ Object.defineProperty(exports, "DataFrameError", { enumerable: true, get: function () { return dataframe_1.DataFrameError; } });
36
+ Object.defineProperty(exports, "InvalidColumn", { enumerable: true, get: function () { return dataframe_1.InvalidColumn; } });
37
+ Object.defineProperty(exports, "InvalidIndex", { enumerable: true, get: function () { return dataframe_1.InvalidIndex; } });
@@ -1,3 +1,4 @@
1
+ import { InvalidColumn } from './exceptions';
1
2
  /**
2
3
  * Represents the available data types for the DataFrame.
3
4
  */
@@ -21,7 +22,7 @@ const nonValueIamcColumns = [
21
22
  'type',
22
23
  ];
23
24
  /**
24
- * Represents a DataFrame, an immutable two-dimensional tabular data structure with labeled axes.
25
+ * Represents a DataFrame, an immutable two-dimensional row-based tabular data structure with labeled axes.
25
26
  */
26
27
  class DataFrame {
27
28
  _index;
@@ -218,6 +219,46 @@ class DataFrame {
218
219
  iat(row, column) {
219
220
  return this._values[column][row];
220
221
  }
222
+ /**
223
+ * Executes a query on the DataFrame and returns a new DataFrame containing
224
+ * the rows that match the specified conditions.
225
+ * @param query.conditions - A single QueryCondition or an array of QueryCondition objects.
226
+ * @param query.conjunction - Optional. Specifies how to combine multiple conditions.
227
+ * Can be 'AND' or 'OR'. Defaults to 'AND'.
228
+ * @returns A new DataFrame containing the rows that match the query.
229
+ */
230
+ query(query) {
231
+ const conditions = Array.isArray(query.conditions)
232
+ ? query.conditions
233
+ : [query.conditions];
234
+ const conjunction = query.conjunction ?? 'AND';
235
+ conditions.forEach((condition) => {
236
+ if (typeof condition.column === 'string') {
237
+ condition.column = this._columns.indexOf(condition.column);
238
+ }
239
+ });
240
+ const illegalColumnns = conditions
241
+ .filter((condition) => condition.column === -1)
242
+ .map((condition) => condition.column);
243
+ if (illegalColumnns.length > 0) {
244
+ throw new InvalidColumn(illegalColumnns);
245
+ }
246
+ const indices = this._values
247
+ .map((row, i) => {
248
+ if (conjunction === 'AND') {
249
+ return conditions.every((condition) => condition.predicate(row[condition.column]))
250
+ ? i
251
+ : -1;
252
+ }
253
+ else {
254
+ return conditions.some((condition) => condition.predicate(row[condition.column]))
255
+ ? i
256
+ : -1;
257
+ }
258
+ })
259
+ .filter((i) => i !== -1);
260
+ return this.iloc({ rows: indices });
261
+ }
221
262
  /**
222
263
  * Gets the data type for a specific column.
223
264
  * @param column - The column name.
@@ -0,0 +1,18 @@
1
+ export class DataFrameError extends Error {
2
+ constructor(message) {
3
+ super(message);
4
+ this.name = 'DataFrameError';
5
+ }
6
+ }
7
+ export class InvalidColumn extends DataFrameError {
8
+ constructor(column) {
9
+ super(`Invalid column: ${Array.isArray(column) ? column.join(', ') : column}`);
10
+ this.name = 'InvalidColumn';
11
+ }
12
+ }
13
+ export class InvalidIndex extends DataFrameError {
14
+ constructor(index) {
15
+ super(`Invalid index: ${Array.isArray(index) ? index.join(', ') : index}`);
16
+ this.name = 'InvalidIndex';
17
+ }
18
+ }
@@ -1,2 +1,3 @@
1
1
  export { DataFrame, DType } from './dataframe';
2
2
  export { concat, toJson, toCsv, merge } from './utils';
3
+ export { DataFrameError, InvalidColumn, InvalidIndex } from './exceptions';
package/dist/esm/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export { Platform } from './core/platform';
2
2
  export { IxmpError, ProgrammingError, InconsistentIamcType, BadRequest, ImproperlyConfigured, ManagerApiError, UnknownApiError, PlatformNotFound, PlatformNotUnique, MissingToken, InvalidToken, Forbidden, NotFound, NotUnique, DeletionPrevented, OperationNotSupported, SchemaError, NoDefaultRunVersion, InvalidRunMeta, BadFilterArguments, InvalidCredentials, } from './core/exceptions';
3
- export { DataFrame, DType, concat, toJson, toCsv, merge } from './dataframe';
3
+ export { DataFrame, DType, concat, toJson, toCsv, merge, DataFrameError, InvalidColumn, InvalidIndex, } from './dataframe';
@@ -9,8 +9,12 @@ declare enum DType {
9
9
  DATETIME = "datetime",
10
10
  MIXED = "mixed"
11
11
  }
12
+ export type QueryCondition = {
13
+ column: string | number;
14
+ predicate: (value: any) => boolean;
15
+ };
12
16
  /**
13
- * Represents a DataFrame, an immutable two-dimensional tabular data structure with labeled axes.
17
+ * Represents a DataFrame, an immutable two-dimensional row-based tabular data structure with labeled axes.
14
18
  */
15
19
  declare class DataFrame {
16
20
  private _index;
@@ -122,6 +126,18 @@ declare class DataFrame {
122
126
  * @returns The value at the specified row and column.
123
127
  */
124
128
  iat(row: number, column: number): any;
129
+ /**
130
+ * Executes a query on the DataFrame and returns a new DataFrame containing
131
+ * the rows that match the specified conditions.
132
+ * @param query.conditions - A single QueryCondition or an array of QueryCondition objects.
133
+ * @param query.conjunction - Optional. Specifies how to combine multiple conditions.
134
+ * Can be 'AND' or 'OR'. Defaults to 'AND'.
135
+ * @returns A new DataFrame containing the rows that match the query.
136
+ */
137
+ query(query: {
138
+ conditions: QueryCondition[] | QueryCondition;
139
+ conjunction?: 'AND' | 'OR';
140
+ }): DataFrame;
125
141
  /**
126
142
  * Gets the data type for a specific column.
127
143
  * @param column - The column name.
@@ -0,0 +1,9 @@
1
+ export declare class DataFrameError extends Error {
2
+ constructor(message: string);
3
+ }
4
+ export declare class InvalidColumn extends DataFrameError {
5
+ constructor(column: string | number | (string | number)[]);
6
+ }
7
+ export declare class InvalidIndex extends DataFrameError {
8
+ constructor(index: number | number[]);
9
+ }
@@ -1,2 +1,3 @@
1
- export { DataFrame, DType } from './dataframe';
1
+ export { DataFrame, DType, type QueryCondition } from './dataframe';
2
2
  export { concat, toJson, toCsv, merge } from './utils';
3
+ export { DataFrameError, InvalidColumn, InvalidIndex } from './exceptions';
@@ -21,4 +21,4 @@ export type { TimeSeries as TimeSeriesModel } from './data/iamc/timeseries';
21
21
  export type { PlatformInfo as PlatformInfoModel } from './data/info';
22
22
  export type { Lookup } from './data/filters';
23
23
  export { IxmpError, ProgrammingError, InconsistentIamcType, BadRequest, ImproperlyConfigured, ManagerApiError, UnknownApiError, PlatformNotFound, PlatformNotUnique, MissingToken, InvalidToken, Forbidden, NotFound, NotUnique, DeletionPrevented, OperationNotSupported, SchemaError, NoDefaultRunVersion, InvalidRunMeta, BadFilterArguments, InvalidCredentials, } from './core/exceptions';
24
- export { DataFrame, DType, concat, toJson, toCsv, merge } from './dataframe';
24
+ export { DataFrame, DType, type QueryCondition, concat, toJson, toCsv, merge, DataFrameError, InvalidColumn, InvalidIndex, } from './dataframe';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iiasa/ixmp4-ts",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "license": "MIT",
5
5
  "description": "typescript client for ixmp4, a data warehouse for scenario analysis",
6
6
  "repository": {