Skip to main content

Overview

A Series is a one-dimensional labeled array that can hold any data type. It is similar to a column in a spreadsheet or database table, combining:
  • An array of data values
  • An array of index labels (can be strings or numbers)
  • An optional name

Constructor

data
T[]
required
Array of values to store in the Series
options
SeriesOptions
Configuration options for Series construction.
import { Series } from 'deepbox/dataframe';

// Create a numeric series
const s = new Series([1, 2, 3, 4], { name: 'numbers' });

// Create a series with custom index
const s2 = new Series(['a', 'b', 'c'], {
  index: ['row1', 'row2', 'row3'],
  name: 'letters'
});

Properties

data

Get the underlying data array.
data
readonly T[]
Read-only view of the data array

index

Get the index labels.
index
readonly (string | number)[]
Read-only view of the index array

name

Get the Series name.
name
string | undefined
The name of this Series, or undefined if not set

length

Get the number of elements in the Series.
length
number
Length of the Series

Data Access

get()

Get a value by label. This method is an alias for loc(). It performs strict label-based lookup. For positional access, use iloc().
label
string | number
required
The index label to look up
return
T | undefined
The value at that label, or undefined if not found
const s = new Series([10, 20, 30], { index: ['a', 'b', 'c'] });
s.get('a');  // 10
s.get('z');  // undefined

loc()

Access a value by label (label-based indexing).
label
string | number
required
The index label to look up
return
T | undefined
The value at that label, or undefined if not found
const s = new Series([10, 20], { index: ['a', 'b'] });
s.loc('a');  // 10

iloc()

Access a value by integer position (position-based indexing).
position
number
required
The integer position (0-based)
return
T | undefined
The value at that position, or undefined if out of bounds
const s = new Series([10, 20, 30]);
s.iloc(0);  // 10
s.iloc(2);  // 30

Selection

Return the first n elements.
n
number
default:"5"
Number of elements to return
return
Series<T>
New Series with the first n elements
const s = new Series([1, 2, 3, 4, 5, 6]);
s.head(3);  // Series([1, 2, 3])

tail()

Return the last n elements.
n
number
default:"5"
Number of elements to return
return
Series<T>
New Series with the last n elements
const s = new Series([1, 2, 3, 4, 5, 6]);
s.tail(3);  // Series([4, 5, 6])

filter()

Filter Series by a boolean predicate function. Filters both data AND index to maintain alignment.
predicate
(value: T, index: number) => boolean
required
Function that returns true for elements to keep
return
Series<T>
New Series with only elements that passed the predicate
const s = new Series([1, 2, 3, 4, 5]);
s.filter(x => x > 2);  // Series([3, 4, 5])

Transformation

map()

Transform each element using a mapping function.
fn
(value: T, index: number) => U
required
Function to apply to each element
return
Series<U>
New Series with transformed values
const s = new Series([1, 2, 3]);
s.map(x => x * 2);  // Series([2, 4, 6])

sort()

Sort the Series values. Preserves index-value mapping by sorting [value, index] pairs.
ascending
boolean
default:"true"
Sort in ascending order
return
Series<T>
New sorted Series with index reordered to match
const s = new Series([3, 1, 2], { index: ['a', 'b', 'c'] });
s.sort();  // Series([1, 2, 3]) with index ['b', 'c', 'a']

Unique Values

unique()

Get unique values in the Series.
return
T[]
Array of unique values (order preserved)
const s = new Series([1, 2, 2, 3, 1]);
s.unique();  // [1, 2, 3]

valueCounts()

Count occurrences of unique values. Returns a Series where index is the unique values and data is their counts.
return
Series<number>
Series where index is unique values and data is their counts
const s = new Series(['a', 'b', 'a', 'c', 'a']);
s.valueCounts();  // Series([3, 1, 1]) with index ['a', 'b', 'c']

Statistical Methods

All statistical methods skip null, undefined, and NaN values.

sum()

Calculate the sum of all values.
return
number
Sum of all numeric values
const s = new Series([1, 2, null, 3, 4]);
s.sum();  // 10

mean()

Calculate the arithmetic mean (average) of all values.
return
number
Mean of all numeric values
const s = new Series([1, 2, null, 3, 4]);
s.mean();  // 2.5

median()

Calculate the median (middle value) of all values. For even-length Series, returns the average of the two middle values.
return
number
Median value
const s = new Series([1, 2, 3, 4, 5]);
s.median();  // 3

std()

Calculate the standard deviation of all values. Uses sample standard deviation (divides by n-1).
return
number
Standard deviation
const s = new Series([2, 4, 6, 8]);
s.std();  // ~2.58

var()

Calculate the variance of all values. Uses sample variance (divides by n-1).
return
number
Variance
const s = new Series([2, 4, 6, 8]);
s.var();  // ~6.67

min()

Find the minimum value in the Series.
return
number
Minimum value
const s = new Series([5, 2, 8, 1, 9]);
s.min();  // 1

max()

Find the maximum value in the Series.
return
number
Maximum value
const s = new Series([5, 2, 8, 1, 9]);
s.max();  // 9

Conversion

toArray()

Convert the Series to a plain JavaScript array. Returns a shallow copy of the data.
return
T[]
Array copy of the data
const s = new Series([1, 2, 3]);
const arr = s.toArray();  // [1, 2, 3]

toTensor()

Convert the Series to an ndarray Tensor.
return
Tensor
Tensor containing the Series data
import { Series } from 'deepbox/dataframe';

const s = new Series([1, 2, 3, 4]);
const t = s.toTensor();  // Tensor([1, 2, 3, 4])

toString()

Return a human-readable string representation of this Series. Each row is printed as index value, with an optional name/length footer. Large Series are truncated with an ellipsis.
maxRows
number
default:"20"
Maximum rows to display before summarizing
return
string
Formatted string representation
const s = new Series([10, 20, 30], { name: 'values' });
s.toString();
// "0  10
// 1  20
// 2  30
// Name: values, Length: 3"

Type Information

The Series class is generic and supports type parameters:
// Explicitly typed Series
const numbers: Series<number> = new Series([1, 2, 3]);
const strings: Series<string> = new Series(['a', 'b', 'c']);

// Type inference
const inferred = new Series([1, 2, 3]);  // Series<number>

// Mixed types
const mixed = new Series([1, 'a', true]);  // Series<string | number | boolean>

Build docs developers (and LLMs) love