Creating a Series
danfo.Series(data, options)
data
1D Array, 1D Tensor, JSON object.
Flat data structure to load into DataFrame
options
Object
Optional configuration object. Supported properties are:
index: Array of numeric or string names for subseting array. If not specified, indexes are auto-generated.
dtypes: Array of data types for each the column. If not specified, dtypes are/is inferred.
config: General configuration object for extending or setting NDframe behavior. See full options here
In order to create a Series, you need to call the new Keyword and pass a flat data structure. In the following examples, we show you how to create a Series by specifying different config options.
Creating a Series from an object:
const dfd = require("danfojs-node")
obj_data = { 'B': ["bval1", "bval2", "bval3", "bval4"] }
df = new dfd.Series(obj_data)
df.print()
╔═══╤═══════╗
║ 0 │ bval1 ║
╟───┼───────╢
║ 1 │ bval2 ║
╟───┼───────╢
║ 2 │ bval3 ║
╟───┼───────╢
║ 3 │ bval4 ║
╚═══╧═══════╝
Creating a Series from an array
const dfd = require("danfojs-node")
obj_data = ["bval1", "bval2", "bval3", "bval4"]
df = new dfd.Series(obj_data)
df.print()
╔═══╤═══════╗
║ 0 │ bval1 ║
╟───┼───────╢
║ 1 │ bval2 ║
╟───┼───────╢
║ 2 │ bval3 ║
╟───┼───────╢
║ 3 │ bval4 ║
╚═══╧═══════╝
Creating a Series and specifying index and dtypes
You can create a Series and specify options like index, dtypes, as well as configuration options for display, and memory mode etc.
Note: Specifing dtypes, and index on Series creation makes the process slightly faster.
import { Series } from "danfojs"
let data1 = [1, 2, 3, 4, 5];
let index = ["a", "b", "c", "d", "e"];
let dtypes = ["int32",]
let df = new Series(data1, { index, dtypes });
df.print()
╔═══╤═══╗
║ a │ 1 ║
╟───┼───╢
║ b │ 2 ║
╟───┼───╢
║ c │ 3 ║
╟───┼───╢
║ d │ 4 ║
╟───┼───╢
║ e │ 5 ║
╚═══╧═══╝
Creating a Series and specifying memory mode
To use less space on Series creation, you can set the low memory mode as demonstrated below:
import { Series } from "danfojs"
let data1 = [1, 2.3, 3, 4, 5, "girl"];
let df = new Series(data1, {
config: { lowMemoryMode: true }
});
df.print()
Last updated
Was this helpful?