Danfo.js Documentation

repository·dev·Indexed 26 days ago

https://github.com/javascriptdata/danfojs

A JavaScript library for data analysis inspired by Python Pandas, providing high-performance data structures like DataFrames and Series. It supports data manipulation, preprocessing, and visualization in both browser and Node.js environments. Key features include TensorFlow.js support, missing data handling, I/O tools for CSV, JSON, and Excel, and a plotting API via PlotlyLib. The library is organized into core modules within danfojs-base, with environment-specific extensions like danfojs-node and danfojs-browser.

Tokens
5K
Snippets
5
Records
44
Agent score
88%

What's inside Danfo.js

  1. Danfo.js Overview and Features

    dev

    Danfo.js is a JavaScript data analysis toolkit inspired by Python's Pandas. It provides fast, flexible, and expressive data structures for working with relational or labeled data.

    Key Features:

    • TensorFlow.js Support: Convert Danfo data structures to Tensors.
    • Missing Data Handling: Easy handling of NaN in both floating and non-floating point data.
    • Data Manipulation: Support for column insertion/deletion, groupby operations, and automatic/explicit alignment.
    • Indexing & Querying: Label-based slicing (loc), fancy indexing (iloc), and querying.
    • IO Tools: Load data from CSV, JSON, and Excel.
    • Preprocessing: Includes OneHotEncoders, LabelEncoders, and scalers like StandardScaler and MinMaxScaler.
    • Plotting: Intuitive API for interactive plotting of DataFrames and Series.
    • Time Series: Specific functionality for date range generation and time properties.
  2. Understand the Danfo.js architecture and danfojs-base

    dev
    Danfo.js is organized into several packages. danfojs-base is the core module containing the fundamental functions and classes. Other packages like danfojs-node and danfojs-browser are extensions that export or extend the functionality provided by danfojs-base for their respective environments.
  3. Install Danfo.js

    dev

    Depending on your environment, choose one of the following installation methods:

    Node.js Applications

    Install the danfojs-node package using npm or yarn:

    Client-side Applications (React, Vue, Next.js, etc.)

    Install the danfojs package using npm or yarn:

    Direct HTML Usage

    Include the script tag from JsDelivr directly in your HTML file.

    # For Node.js
    npm install danfojs-node
    # or
    yarn add danfojs-node
    
    # For Client-side frameworks
    npm install danfojs
    # or
    yarn add danfojs
    <script src="https://cdn.jsdelivr.net/npm/danfojs@1.1.2/lib/bundle.js"></script>
  4. Example usage in Node.js

    dev

    In Node.js, require danfojs-node. This example demonstrates loading a CSV, inspecting data (head, shape, columns, dtypes), dropping columns, selecting by data type, adding new columns, and calculating value counts.

    const dfd = require("danfojs-node");
    
    const file_url = "https://web.stanford.edu/class/archive/cs/cs109/cs109.1166/stuff/titanic.csv";
    
    dfd
      .readCSV(file_url)
      .then((df) => {
        df.head().print();
        df.describe().print();
        console.log(df.shape);
        console.log(df.columns);
        df.ctypes.print();
    
        df["Name"].print();
    
        let cols_2_remove = ["Age", "Pclass"];
        let df_drop = df.drop({ columns: cols_2_remove, axis: 1 });
        df_drop.print();
    
        let str_cols = df_drop.selectDtypes(["string"]);
        let num_cols = df_drop.selectDtypes(["int32", "float32"]);
        str_cols.print();
        num_cols.print();
    
        let new_vals = df["Fare"].round(1);
        df_drop.addColumn("fare_round", new_vals, { inplace: true });
        df_drop.print();
    
        df_drop["fare_round"].round(2).print(5);
        df_drop["Survived"].valueCounts().print();
        df_drop.tail(10).print();
        df_drop.isNa().sum().print();
      })
      .catch((err) => {
        console.log(err);
      });
  5. Example usage in the Browser

    dev

    You can use Danfo.js in the browser to load CSV data and perform interactive plotting. The dfd global object is provided by the script tag. Common tasks include creating box plots, displaying data as tables, and generating time-series line plots.

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <script src="https://cdn.jsdelivr.net/npm/danfojs@1.1.2/lib/bundle.js"></script>
        <title>Document</title>
      </head>
      <body>
        <div id="div1"></div>
        <div id="div2"></div>
        <div id="div3"></div>
        <script>
          dfd.readCSV("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
              .then(df => {
                  df['AAPL.Open'].plot("div1").box() // makes a box plot
                  df.plot("div2").table() // display csv as table
                  let new_df = df.setIndex({ column: "Date", drop: true }); // resets the index to Date column
                  new_df.head().print(); 
                  new_df.plot("div3").line({
                      config: {
                          columns: ["AAPL.Open", "AAPL.High"]
                      }
                  }) // makes a timeseries plot
              }).catch(err => {
                  console.log(err);
              })
        </script>
      </body>
    </html>
  6. View default configuration constants

    dev

    Danfo.js uses a BASE_CONFIG object to manage internal defaults for table rendering and memory management. While these are typically handled internally, they define the following parameters:

    • tableMaxRow: Maximum number of rows to display in a table.
    • tableMaxColInConsole: Maximum number of columns to display in the console.
    • dtypeTestLim: Limit for data type testing.
    • lowMemoryMode: Boolean flag to enable/disable low memory mode.
    export const BASE_CONFIG = {
        tableMaxRow: 10,
        tableMaxColInConsole: 10,
        dtypeTestLim: 20,
        lowMemoryMode: false,
    }
  7. Configure Danfo.js global settings via ConfigsType

    dev

    Use the ConfigsType object to control how data is displayed and processed. This is useful for managing console output and memory usage.

    Key options:

    • tableDisplayConfig: Configuration for table rendering (using table package).
    • tableMaxRow: Maximum number of rows to display in the console.
    • tableMaxColInConsole: Maximum number of columns to display in the console.
    • dtypeTestLim: Limit for dtype testing.
    • lowMemoryMode: Boolean to enable low memory mode.
    • tfInstance: Custom TensorFlow instance.
  8. Reference the Danfo.js core modules and classes

    dev

    The core functionality of Danfo.js is organized into several functional modules within danfojs-base:

    • Core Classes:
      • frame: Represents a DataFrame.
      • series: Represents a Series.
      • datetime: Represents date and time.
      • daterange: Represents a date range.
      • indexing: Handles indexing operations.
      • math.ops: Handles mathematical operations.
      • strings: Handles string operations.
      • generic: Represents a generic object.
    • Aggregators: Functions for aggregating data.
    • Transformers:
      • Encoders: one.hot.encoder, label.encoder, and dummy.encoder.
      • Scalers: min.max.scalers and standard.scalers.
      • Structural: concat (concatenation) and merge.
    • Plotting: Support for plotly and vega (stub).
    • IO (Input/Output): Environment-specific classes for csv, json, and excel (available in both browser and node sub-modules).
  9. Use BaseDataOptionType for data loading

    dev

    When loading data into a DataFrame or Series, you can provide a BaseDataOptionType object to specify the structure and types.

    Options:

    • type: Data type identifier.
    • index: Array of strings or numbers to use as the index.
    • columns: Array of strings for column names.
    • dtypes: Array of strings specifying data types for columns.
    • config: An object of type ConfigsType.
  10. Use PlotlyLib for data visualization

    dev

    The PlotlyLib class provides a wrapper around the Plotly.js library to visualize Danfo.js DataFrame or Series objects. To use it, instantiate the class with your data and the ID of the HTML element where the plot should be rendered.

    Supported plot types include line, bar, scatter, histogram, pie, box, violin, and table plots. Each method accepts an optional plotConfig object to pass Plotly-specific config and layout parameters.