Skip to content

Commit

Permalink
Merge pull request #511 from javascriptdata/chore/add-default-support…
Browse files Browse the repository at this point in the history
…-for-datetime

Chore/add default support for datetime
  • Loading branch information
risenW authored Oct 12, 2022
2 parents 6d06a5c + a18426d commit 0d28968
Show file tree
Hide file tree
Showing 12 changed files with 2,801 additions and 2,624 deletions.
5 changes: 3 additions & 2 deletions src/danfojs-base/core/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export default class NDframe implements NDframeInterface {
} else if (utils.is1DArray(data)) {
this.loadArrayIntoNdframe({ data, index, columns, dtypes });
} else {

if (Array.isArray(data) && utils.isObject(data[0])) {
this.loadObjectIntoNdframe({ data, type: 1, index, columns, dtypes });

Expand All @@ -92,6 +91,8 @@ export default class NDframe implements NDframeInterface {
utils.isString((data)[0])
) {
this.loadArrayIntoNdframe({ data, index, columns, dtypes });
} else if (Array.isArray(data) && data.length > 0 && utils.isDate(data[0])) {
this.loadArrayIntoNdframe({ data, index, columns, dtypes });
} else {
throw new Error("File format not supported!");
}
Expand Down Expand Up @@ -504,4 +505,4 @@ export default class NDframe implements NDframeInterface {
print() {
console.log(this + "");
}
}
}
4 changes: 2 additions & 2 deletions src/danfojs-base/core/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1962,7 +1962,7 @@ export default class Series extends NDframe implements SeriesInterface {
* ```
*/
get dt() {
if (this.dtypes[0] == "string") {
if (["string", "datetime"].includes(this.dtypes[0])) {
return new Dt(this)
} else {
throw new Error("Cannot call accessor dt on non-string type");
Expand Down Expand Up @@ -2184,4 +2184,4 @@ export default class Series extends NDframe implements SeriesInterface {
throw new Error("Not supported in NodeJS");
}
}
}
}
2 changes: 1 addition & 1 deletion src/danfojs-base/shared/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class Configs {
tableDisplayConfig: {},
tableMaxRow: 10,
tableMaxColInConsole: 10,
dtypeTestLim: 20,
dtypeTestLim: 500,
lowMemoryMode: false,
...options
}
Expand Down
2 changes: 1 addition & 1 deletion src/danfojs-base/shared/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ export const BASE_CONFIG = {
lowMemoryMode: false,
}

export const DATA_TYPES = ["float32", "int32", "string", "boolean", 'undefined'];
export const DATA_TYPES = ["float32", "int32", "string", "boolean", "datetime",'undefined'];
27 changes: 25 additions & 2 deletions src/danfojs-base/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ export default class Utils {
return value === undefined || value === null || (isNaN(value as any) && typeof value !== "string");
}

/**
* Checks if a value is a date object
* @param value A date object
* @returns boolean
*/
isDate(value: any): boolean {
return value instanceof Date;
}

/**
* Generates an array of integers between specified range
* @param start The starting number.
Expand Down Expand Up @@ -259,6 +268,7 @@ export default class Utils {
let floatTracker: Array<boolean> = [];
let stringTracker: Array<boolean> = [];
let boolTracker: Array<boolean> = [];
let dateTracker: Array<boolean> = [];

if (arr.length < config.getDtypeTestLim) {
lim = arr.length;
Expand All @@ -275,28 +285,39 @@ export default class Utils {
intTracker.push(false);
stringTracker.push(false);
boolTracker.push(true);
dateTracker.push(false);
} else if (this.isEmpty(ele)) {
floatTracker.push(true);
intTracker.push(false);
stringTracker.push(false);
boolTracker.push(false);
dateTracker.push(false);
} else if (this.isDate(ele)) {
floatTracker.push(false);
intTracker.push(false);
stringTracker.push(false);
boolTracker.push(false);
dateTracker.push(true);
} else if (!isNaN(Number(ele))) {
if ((ele as unknown as string).toString().includes(".")) {
floatTracker.push(true);
intTracker.push(false);
stringTracker.push(false);
boolTracker.push(false);
dateTracker.push(false);
} else {
floatTracker.push(false);
intTracker.push(true);
stringTracker.push(false);
boolTracker.push(false);
dateTracker.push(false);
}
} else {
} else {
floatTracker.push(false);
intTracker.push(false);
stringTracker.push(true);
boolTracker.push(false);
dateTracker.push(false);
}
}

Expand All @@ -310,6 +331,8 @@ export default class Utils {
dtypes = "int32";
} else if (boolTracker.some(even)) {
dtypes = "boolean";
} else if (dateTracker.some(even)) {
dtypes = "datetime";
} else {
dtypes = "undefined";
}
Expand Down Expand Up @@ -819,4 +842,4 @@ export default class Utils {

return sortedValues;
}
}
}
56 changes: 56 additions & 0 deletions src/danfojs-browser/tests/core/frame.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2999,4 +2999,60 @@ describe("DataFrame", function () {
});

});

describe("DateTime datetime is supported", function () {
it("DateTime datetime is supported", function () {
let data = [ [ "Alice", 2, new Date(2029, 1, 1) ],
[ "Bob", 5, new Date(2019, 1, 2) ],
[ "Charlie", 30, new Date(2019, 1, 3) ],
[ "Dennis", 89, new Date(2019, 1, 4) ] ];

let columns = [ "Name", "Count", "Date" ];

let df = new dfd.DataFrame(data, { columns: columns });
assert.deepEqual(df.dtypes, [ "string", "int32", "datetime" ]);

const dateValues = [ new Date(2029, 1, 1), new Date(2019, 1, 2), new Date(2019, 1, 3), new Date(2019, 1, 4) ];
assert.deepEqual(df["Date"].values, dateValues);
});

it("datetime column properties can be accessed", function () {
let data = [ [ "Alice", 2, new Date("2029-01-01 01:00:00") ],
[ "Bob", 5, new Date("2019-01-02") ],
[ "Charlie", 30, new Date("2020-01-03 01:00:20") ],
[ "Dennis", 89, new Date("2022-02-04 02:16:00") ] ];

let columns = [ "Name", "Count", "Date" ];

let df = new dfd.DataFrame(data, { columns: columns });

assert.deepEqual(df["Date"].dt.year().values, [ 2029, 2019, 2020, 2022 ]);
assert.deepEqual(df["Date"].dt.month().values, [ 0, 0, 0, 1 ]);
assert.deepEqual(df["Date"].dt.dayOfMonth().values, [ 1, 2, 3, 4 ]);
assert.deepEqual(df["Date"].dt.hours().values, [ 1, 0, 1, 2 ]);
assert.deepEqual(df["Date"].dt.minutes().values, [ 0, 0, 0, 16 ]);
assert.deepEqual(df["Date"].dt.seconds().values, [ 0, 0, 20, 0 ]);

});

it("datetime column created from dtype passed", function () {
let data = [ [ "Alice", 2, "2029-01-01 01:00:00" ],
[ "Bob", 5, "2019-01-02" ],
[ "Charlie", 30, "2020-01-03 01:00:20" ],
[ "Dennis", 89, "2022-02-04 02:16:00" ] ];

let columns = [ "Name", "Count", "Date" ];
let dtypes = [ "string", "int32", "datetime" ];

let df = new dfd.DataFrame(data, { columns, dtypes });

assert.deepEqual(df["Date"].dt.year().values, [ 2029, 2019, 2020, 2022 ]);
assert.deepEqual(df["Date"].dt.month().values, [ 0, 0, 0, 1 ]);
assert.deepEqual(df["Date"].dt.dayOfMonth().values, [ 1, 2, 3, 4 ]);
assert.deepEqual(df["Date"].dt.hours().values, [ 1, 0, 1, 2 ]);
assert.deepEqual(df["Date"].dt.minutes().values, [ 0, 0, 0, 16 ]);
assert.deepEqual(df["Date"].dt.seconds().values, [ 0, 0, 20, 0 ]);

});
});
});
17 changes: 17 additions & 0 deletions src/danfojs-browser/tests/core/generic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,4 +489,21 @@ describe("Generic (NDFrame)", function () {

});

describe("DataFrame supports date object", function () {
it("Can successfully create a DataFrame from an Array with date object", function () {
let data = [ [ "Alice", 2, new Date(2019, 1, 1) ],
[ "Bob", 5, new Date(2019, 1, 2) ],
[ "Charlie", 30, new Date(2019, 1, 3) ],
[ "Dennis", 89, new Date(2019, 1, 4) ] ];
let columns = [ "Name", "Count", "Date" ];

let df = new dfd.NDframe({ data, columns, isSeries: false });
let dfDtypes = [ 'string', 'int32', 'datetime' ];
assert.deepEqual(df.dtypes, dfDtypes);
// @ts-ignore
assert.deepEqual(df.values, data);
assert.deepEqual(df.columns, columns);
});
});

});
Loading

0 comments on commit 0d28968

Please sign in to comment.