Skip to content

Commit fdf94ae

Browse files
committed
create ndFrame from tensor
1 parent 51ab8d6 commit fdf94ae

3 files changed

Lines changed: 33 additions & 12 deletions

File tree

danfojs/src/core/generic.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ export default class NDframe {
2525

2626
constructor(data, kwargs = {}) {
2727
this.kwargs = kwargs
28-
28+
if (data instanceof tf.Tensor){
29+
data = data.arraySync()
30+
}
2931
if (utils.__is_1D_array(data)) {
3032
this.series = true
3133
this.__read_array(data)

danfojs/tests/core/generic.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { assert } from "chai";
22
import NDframe from '../../src/core/generic';
3+
import * as tf from '@tensorflow/tfjs'
4+
35

46

57
describe("Generic (NDFrame)", function () {
@@ -97,6 +99,23 @@ describe("Generic (NDFrame)", function () {
9799
})
98100
})
99101

102+
describe("NDframe Created from a Tensor", function () {
103+
104+
it("prints the shape of a 2D frame created from a 2D tensor", function () {
105+
let data = tf.tensor([1, 2, 3, 4])
106+
let ndframe = new NDframe(data)
107+
assert.deepEqual(ndframe.ndim, 1)
108+
assert.deepEqual(ndframe.values, [1, 2, 3, 4])
109+
110+
})
111+
it("prints the shape of a 2D frame created from a 1D tensor", function () {
112+
let data = tf.tensor([[2, 3, 4], [4, 5, 6]])
113+
let ndframe = new NDframe(data, {columns: ["alpha", "count", "sum"]})
114+
assert.deepEqual(ndframe.column_names, ["alpha", "count", "sum"])
115+
})
116+
117+
})
118+
100119
describe("index", function () {
101120
it("Returns the index of an NDframe", function () {
102121
let data = [{ alpha: "A", count: 1 }, { alpha: "B", count: 2 }, { alpha: "C", count: 3 }]
@@ -116,12 +135,12 @@ describe("Generic (NDFrame)", function () {
116135
let data = { alpha: ["A", "B", "C", "D"], count: [1, 2, 3, 4] }
117136
let ndframe = new NDframe(data)
118137
// assert.deepEqual(ndframe.shape, [4, 2])
119-
assert.deepEqual(ndframe.col_data, [["A", "B", "C", "D"],[1, 2, 3, 4]])
138+
assert.deepEqual(ndframe.col_data, [["A", "B", "C", "D"], [1, 2, 3, 4]])
120139
})
121140
it("retrieves the col data ", function () {
122-
let data = { alpha: ["A", "B", "C", "D"], count: [1,2,3,4], sum: [20.3, 30.456, 40.90, 90.1]}
141+
let data = { alpha: ["A", "B", "C", "D"], count: [1, 2, 3, 4], sum: [20.3, 30.456, 40.90, 90.1] }
123142
let ndframe = new NDframe(data)
124-
let res = [["A", "B", "C", "D"],[1,2,3,4],[20.3, 30.456, 40.90, 90.1]]
143+
let res = [["A", "B", "C", "D"], [1, 2, 3, 4], [20.3, 30.456, 40.90, 90.1]]
125144
assert.deepEqual(ndframe.col_data, res)
126145
})
127146
it("retrieves the row data created from OA ", function () {
@@ -183,7 +202,7 @@ describe("Generic (NDFrame)", function () {
183202
assert.deepEqual(ndframe.dtypes, ["float32"])
184203
})
185204

186-
205+
187206
it("Returns dtype set during creation of 2DFrame from an Object", function () {
188207
let data = [{ alpha: "A", count: 1 }, { alpha: "B", count: 2 }]
189208
let options = { dtypes: ['string', 'int32'] }
@@ -214,13 +233,13 @@ describe("Generic (NDFrame)", function () {
214233
let data = [{ alpha: "A", count: 1 }, { alpha: "B", count: 2 }, { alpha: "C", count: 3 }]
215234
let df = new NDframe(data)
216235
let result = `alpha,count\nA,1\nB,2\nC,3\n`
217-
df.to_csv().then((csv)=>{
236+
df.to_csv().then((csv) => {
218237
assert.deepEqual(csv, result)
219238
})
220239
})
221240
it("Converts DataFrame of Series to csv format and return string when path is not specified", async function () {
222241
let data = [[12, 2, 20], [90, 5, 23], [45, 56, 70], [9, 10, 19]]
223-
let df = new NDframe(data, {columns: ["A", "B", "C"]})
242+
let df = new NDframe(data, { columns: ["A", "B", "C"] })
224243
let result = `A,B,C\n12,2,20\n90,5,23\n45,56,70\n9,10,19\n`
225244
assert.deepEqual(await df.to_csv(), result)
226245
})
@@ -232,15 +251,15 @@ describe("Generic (NDFrame)", function () {
232251
let result = JSON.stringify([{ alpha: "A", count: 1 }, { alpha: "B", count: 2 }, { alpha: "C", count: 3 }])
233252

234253
let df = new NDframe(data)
235-
df.to_json().then((json)=>{
254+
df.to_json().then((json) => {
236255
assert.deepEqual(json, result)
237256
})
238257
})
239258
it("Converts DataFrame to json format", async function () {
240259
let data = [[12, 2, 20], [90, 5, 23], [45, 56, 70]]
241-
let df = new NDframe(data, {columns: ["A", "B", "C"]})
242-
let result = JSON.stringify([{A: 12, B: 2, C: 20},{A: 90, B: 5, C: 23},{A: 45, B: 56, C: 70}])
243-
df.to_json().then((json)=>{
260+
let df = new NDframe(data, { columns: ["A", "B", "C"] })
261+
let result = JSON.stringify([{ A: 12, B: 2, C: 20 }, { A: 90, B: 5, C: 23 }, { A: 45, B: 56, C: 70 }])
262+
df.to_json().then((json) => {
244263
assert.deepEqual(json, result)
245264
})
246265
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"table": "^5.4.6"
2424
},
2525
"scripts": {
26-
"test": "nyc mocha --require @babel/register danfojs/tests/core/utils",
26+
"test": "nyc mocha --require @babel/register danfojs/tests/core/generic",
2727
"dev": "npm run lint && babel ./danfojs/src -d dist --no-comments",
2828
"build": "babel ./danfojs/src -d ./dist --no-comments",
2929
"lint": "eslint ./danfojs/src",

0 commit comments

Comments
 (0)