Skip to content

Commit 26d4bc8

Browse files
committed
Fix dtype check bug
1 parent 315b6c0 commit 26d4bc8

4 files changed

Lines changed: 50 additions & 22 deletions

File tree

danfojs-browser/src/core/utils.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ export class Utils {
213213
//infer types from an array of array
214214
__get_t(arr_val) {
215215
if (this.__is_1D_array(arr_val)) {
216-
const dtypes = [];
216+
let dtypes = [];
217217
let int_tracker = [];
218218
let float_tracker = [];
219219
let string_tracker = [];
@@ -228,6 +228,10 @@ export class Utils {
228228
}
229229
});
230230

231+
if (arr.length == 0){
232+
dtypes.push("string");
233+
}
234+
231235
if (arr.length < config.get_dtype_test_lim) {
232236
lim = arr.length - 1;
233237
} else {
@@ -260,7 +264,7 @@ export class Utils {
260264
}
261265

262266
if (count == lim) {
263-
//if atleast one string appears return string dtype
267+
//if atleast one string appears return string dtype
264268
const even = (element) => element == true;
265269
if (string_tracker.some(even)) {
266270
dtypes.push("string");
@@ -278,13 +282,9 @@ export class Utils {
278282

279283
return dtypes;
280284
} else {
281-
const dtypes = [];
285+
let dtypes = [];
282286
let lim;
283-
if (arr_val[0].length < config.get_dtype_test_lim) {
284-
lim = arr_val[0].length - 1;
285-
} else {
286-
lim = config.get_dtype_test_lim - 1;
287-
}
287+
288288
arr_val.forEach((ele) => {
289289
let int_tracker = [];
290290
let float_tracker = [];
@@ -296,11 +296,18 @@ export class Utils {
296296
ele.map((val) => {
297297
if (!(isNaN(val) && typeof val != "string")) {
298298
arr.push(val);
299-
} else {
300-
arr.push("NaN"); //set NaN to string and return dtype ""string". The caller should explicitly convert the dtype
301299
}
302300
});
303301

302+
if (arr.length == 0){
303+
dtypes.push("string");
304+
}
305+
306+
if (arr.length < config.get_dtype_test_lim) {
307+
lim = arr.length - 1;
308+
} else {
309+
lim = config.get_dtype_test_lim - 1;
310+
}
304311
arr.forEach((ele, indx) => {
305312
let count = indx;
306313
if (typeof ele == "boolean") {
@@ -328,7 +335,7 @@ export class Utils {
328335
}
329336

330337
if (count == lim) {
331-
//if atleast one string appears return string dtype
338+
//if atleast one string appears return string dtype
332339
const even = (element) => element == true;
333340
if (string_tracker.some(even)) {
334341
dtypes.push("string");
@@ -349,6 +356,7 @@ export class Utils {
349356
}
350357
}
351358

359+
352360
__unique(data) {
353361
let unique = new Set();
354362

danfojs-browser/tests/core/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ describe("Utils Functions", function () {
9696
let result = [ 'boolean', 'string', 'string' ];
9797
assert.deepEqual(utils.__get_t(data), result);
9898
});
99+
it("Returns correct dtype if NaN present in data", function () {
100+
let data = [
101+
[ 18.7, 17.4, 18, NaN, 19.3 ],
102+
[ 20, NaN, 19, 18, 20 ] ];
103+
let result = [ 'float32', 'int32' ];
104+
assert.deepEqual(utils.__get_t(data), result);
105+
});
99106
});
100107

101108
describe("__map_int_to_bool", function () {

danfojs-node/src/core/utils.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ export class Utils {
213213
//infer types from an array of array
214214
__get_t(arr_val) {
215215
if (this.__is_1D_array(arr_val)) {
216-
const dtypes = [];
216+
let dtypes = [];
217217
let int_tracker = [];
218218
let float_tracker = [];
219219
let string_tracker = [];
@@ -228,6 +228,10 @@ export class Utils {
228228
}
229229
});
230230

231+
if (arr.length == 0){
232+
dtypes.push("string");
233+
}
234+
231235
if (arr.length < config.get_dtype_test_lim) {
232236
lim = arr.length - 1;
233237
} else {
@@ -278,13 +282,9 @@ export class Utils {
278282

279283
return dtypes;
280284
} else {
281-
const dtypes = [];
285+
let dtypes = [];
282286
let lim;
283-
if (arr_val[0].length < config.get_dtype_test_lim) {
284-
lim = arr_val[0].length - 1;
285-
} else {
286-
lim = config.get_dtype_test_lim - 1;
287-
}
287+
288288
arr_val.forEach((ele) => {
289289
let int_tracker = [];
290290
let float_tracker = [];
@@ -296,11 +296,18 @@ export class Utils {
296296
ele.map((val) => {
297297
if (!(isNaN(val) && typeof val != "string")) {
298298
arr.push(val);
299-
} else {
300-
arr.push("NaN"); //set NaN to string and return dtype ""string". The caller should explicitly convert the dtype
301299
}
302300
});
303301

302+
if (arr.length == 0){
303+
dtypes.push("string");
304+
}
305+
306+
if (arr.length < config.get_dtype_test_lim) {
307+
lim = arr.length - 1;
308+
} else {
309+
lim = config.get_dtype_test_lim - 1;
310+
}
304311
arr.forEach((ele, indx) => {
305312
let count = indx;
306313
if (typeof ele == "boolean") {

danfojs-node/tests/core/utils.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@ describe("Utils Functions", function () {
7070
let result = [ 'float32' ];
7171
assert.deepEqual(utils.__get_t(data), result);
7272
});
73-
73+
it("Returns correct dtype if NaN present in data", function () {
74+
let data = [
75+
[18.7, 17.4, 18, NaN, 19.3],
76+
[ 20, NaN, 19, 18, 20 ]];
77+
let result = [ 'float32', 'int32' ];
78+
assert.deepEqual(utils.__get_t(data), result);
79+
});
7480
it("Returns the data type present in an 2D array", function () {
7581
let data = [ [ 'Alice', 'Boy', 'Girl', "39" ], [ 2, 5, 30, 89 ], [ 3.1, 6.1, 40.1, 78.2 ] ];
7682
let result = [ 'string', 'int32', 'float32' ];
@@ -91,7 +97,7 @@ describe("Utils Functions", function () {
9197
let result = [ 'boolean', 'string', 'int32' ];
9298
assert.deepEqual(utils.__get_t(data), result);
9399
});
94-
it("Returns string type if values are NaN", function () {
100+
it("Returns string type if values are all NaN", function () {
95101
let data = [ [ true, false, true ], [ "boy", "girl", "boy" ], [ NaN, undefined, NaN ] ];
96102
let result = [ 'boolean', 'string', 'string' ];
97103
assert.deepEqual(utils.__get_t(data), result);

0 commit comments

Comments
 (0)