data-forge
JavaScript data transformation and analysis toolkit inspired by Pandas and LINQ.
Works in both NodeJS and the browser.
For articles and information please see The Data Wrangler.
The book
If you have an interest in data wrangling and analysis in JavaScript please check out my new book on the subject!
This respository is no longer maintained
For the latest version of Data-Forge please go to the repo for the new TypeScript version: https://github.com/data-forge/data-forge-ts
New version is still available at the usual place on NPM:
https://www.npmjs.com/package/data-forge
Breaking changes
I've removed the DataForge functions that apply to MongoDB and REST APIs. Sorry if you were depending on these, but they had to go.
They limited DataForge's application and it was my mistake to have added them in the first place. If they belong at all they should live in
separate packages (and be plugins to DataForge). If you were relying on these functions please log an issue here and I'll help you rectify the situation.
Generated API docs
See here for generated API docs that are taking shape.
Examples
Examples and some tests have been removed to a separate repository.
Contents
Table of Contents generated with DocToc
Project Overview
A short overview of the aims, principles and implementation methods for this project.
Project Aims
The aims of this project:
- To combine the best aspects of Pandas and LINQ and make them available in JavaScript.
- To be able to load, transform and save data.
- To be able to prepare data for visualization.
- Be able to work with massive data files.
Driving Principles
The principles that drive decision making and tradeoffs:
- The API should be simple, easy to learn and easy to use.
- Minimize the magic, everything should be understandable, the API should be orthogonal.
- The library should have high performance.
- Be able to use a similar API in both Javascript and C# (although I may never get time to work on the C# version).
- The code you build during interactive data exploration should be transplantable to a webapp, server or microservice.
Implementation
General implementation goals:
- Immutable: every operation generates a new immutable data set.
- Lazy evaluation, to make the performance of immutability acceptable.
- Should be easily extensible.
- Core code is created through test driven development.
Installation
NodeJS installation and setup
Install via NPM:
npm install --save data-forge
Require the module into your script:
var dataForge = require('data-forge');
Data-Forge plugins under Node.js
Plugins are typically loaded into the Data-Forge namespace as follows, using data-forge-from-yahoo as an example.
Install via NPM:
npm install --save data-forge-from-yahoo
Import the module and use it:
var dataForge = require('data-forge');
dataForge.use(require('data-forge-from-yahoo'));
You can use functions defined by the plugin, eg
dataForge.fromYahoo('MSFT')
.then(function (dataFrame) {
// ... use the data returned from Yahoo ...
});
Or if you have a recent implementation of JavaScript:
var dataFrame = await dataForge.fromYahoo('MSFT');
Browser installation and setup
Install via Bower:
bower install --save data-forge
Include the main script in your HTML file:
<script src="bower_components/data-forge/data-forge.js"></script>
You can now use data-forge through the global dataForge variable.
Data-Forge plugins under the browser
As in the Node.js example, plugins are typically loaded into the Data-Forge namespace. Example using data-forge-from-yahoo.
Install via Bower:
bower install --save data-forge-from-yahoo
Include in your HTML file:
<script src="bower_components/data-forge/data-forge.js"></script>
<script src="bower_components/data-forge-from-yahoo/data-forge-from-yahoo.js"></script>
Use functions defined by the plugin, eg:
dataForge.fromYahoo('MSFT')
.then(function (dataFrame) {
// ... use the data returned from Yahoo ...
});
Getting the code
Install via NPM and Bower as described in previous sections or clone, fork or download the code from GitHub:
https://github.com/data-forge/data-forge-js
Key Concepts
The concepts section has been moved to a separate file.
A note about JavaScript anonymous functions
Use of DataForge relies heavily on the use of JavaScript anonymous functions. Note that for some of the examples I use the new arrow syntax that was introduced in ES6.
An example of an arrow syntax anonymous function:
(param1, param2) => 1 + 1
Most times however in the examples I have used the traditional syntax:
function (param1, param2) {
return 1 + 1;
}
Even though I haven't used it much in this readme, I prefer the new syntax as it is more concise and the code flows better when multiple anonymous functions are chained together.
Basic Usage
Getting data in
DataFrame
The DataFrame constructor is passed a config object that specifies the initial contents of the DataFrame and additional options.
Create a data frame from column names and rows:
var dataFrame = new dataForge.DataFrame({
columnNames: ["Col1", "Col2", "Col3"],
values: [
[1, 'hello', new Date(...)],
[5, 'computer', new Date(...)],
[10, 'good day', new Date(...)]
]
});
A data frame can also be created from an array of JavaScript objects and the column names are inferred from the fields in the first object:
var dataFrame = new dataForge.DataFrame({
values: [
{
Col1: 1,
Col2: 'hello',
Col3: new Date(....)
},
{
Col1: 5,
Col2: 'computer',
Col3: new Date(....)
},
{
Col1: 10,
Col2: 'good day',
Col3: new Date(....)
}
]
});
If you don't need additional options, you can simply pass in an array of objects:
var dataFrame = new dataForge.DataFrame([
{
Col1: 1,
Col2: 'hello',
Col3: new Date(....)
},
{
Col1: 5,
Col2: 'computer',
Col3: new Date(....)
},
{
Col1: 10,
Col2: 'good day',
Col3: new Date(....)
}
]);
If you have irregular data you can enable considerAllRows, but be warned that this can be expensive as every value must be examined to determine column names:
var dataFrame = new dataForge.DataFrame({
values: [
{
Col1: 1,
Col2: 'hello',
Col3: new Date(....)
},
{
Col1: 5,
Col5: 'these are irregular columns',
Col6: new Date(....)
},
{
Col5: 10,
Col7: 'another irregular column',
Col10: new Date(....)
}
],
considerAllRows: true, // Examine all rows to determine column names.
});
A DataFrame can also be constructed from separate columns as follows using arrays or Series objects:
var dataFrame = new dataForge.DataFrame({
columns: {
Col1: [1, 2, 3, 4],
Col2: ['a', 'b', 'c', 'd'],
Col3: new dataForge.Series(...),
},
});
Series
This is very similar to creating a DataFrame. You pass in a configuration object with the values and additional options:
var series = new dataForge.Series({
values: [1, 2, 3]
});
If you don't need additional options you can simply pass in an array:
var series = new dataForge.Series([1, 2, 3]);
Get data back out
DataFrame
To get back the names of columns:
var columnNames = dataFrame.getColumnNames();
To get back an array of objects (with column names as field names):
var objects = dataFrame.toArray();
To get back an array of rows (in column order):
var rows = dataFrame.toRows();
To get back index and value pairs:
var pairs = dataFrame.toPairs();
Series
To retreive the data from Series as an array:
var values = series.toArray();
To get back index and value pairs:
var pairs = series.toPairs();
Setting an index
In the previous examples of creating Series and DataFrames no index was specified, so a default zero-based index was generated.
An index can also be set explicitly when creating a Series or DataFrame:
var dataFrame = new dataForge.DataFrame({
columnNames: <column-names>,
values: <initial-values>,
index: [5, 10, 100, ...]
});
var series = new dataForge.Series({
values: <initial-values>,
index: [5, 10, 100, ...]
});
A new index can easily be assigned to either Series or DataFrame using the withIndex function:
var dataFrameWithNewIndex = dataFrame.withIndex([1, 2, 3, ...]);
Most likely when using a DataFrame you will want to promote an existing column to an index:
var dataFrame = new dataForge.DataFrame(someConfig).setIndex("Col3");
Be aware that promoting a column to an index in Data-Forge doesn't remove the column (as it does in Pandas), however you can easily achieve this by calling dropSeries:
var dataFrame = new dataForge.DataFrame(someConfig).setIndex("Col3").dropSeries("Col3");
An index is required for certain operations like merge.
Working with CSV files
NOTE: Data-Forge us the NodeJS fs module, this doesn't work in the browser which has no access to the local file system.
Reading CSV files
If your CSV has a header with column names:
var dataFrame = dataForge
.readFileSync('some-csv-file.csv')
.parseCSV()
;
If your CSV doesn't have a header:
var csvOptions = { columnNames: ["some", "explicit", "column", "names"] };
var dataFrame = dataForge
.readFileSync('some-csv-file.csv')
.parseCSV(csvOptions)
;
Writing CSV files
dataFrame.asCSV().writeFileSync('some-other-csv-file.csv');
Working with CSV data
If you already have CSV data (loaded into a string) you can parse it into a dataframe via fromCSV:
var inputCsvData = ... some string with CSV data ...
var dataFrame = dataForge.fromCSV(inputCsvData);
You can stringify a dataframe by calling toCSV:
var outputCsvData = dataFrame.toCSV();
Working with JSON files
NOTE: Data-Forge us the NodeJS fs module, this doesn't work in the browser which has no access to the local file system.
Reading JSON files
var dataFrame = dataForge
.readFileSync('some-json-file.json')
.parseJSON()
;
Writing JSON files
dataFrame.asJSON().writeFileSync('some-json-file.json');
Working with JSON data
If you already have JSON data (loaded into a string) you can parse it into a dataframe via fromJSON: