Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Huggingface Datasets Dataset Features Property

From Leeroopedia
Knowledge Sources
Domains Data_Engineering, NLP
Last Updated 2026-02-14 18:00 GMT

Overview

Concrete tool for inspecting the feature type schema of a loaded dataset provided by the HuggingFace Datasets library.

Description

Dataset.features is a read-only property that returns the Features object describing the dataset's column schema. It is defined in the Dataset class (overriding the base DatasetInfoMixin.features property) and adds a guard ensuring that features are never None for a constructed Dataset object. The base class implementation in DatasetInfoMixin returns a copy of the features from self._info.features to prevent external mutation. The Features object is an ordered dictionary mapping column names to feature type descriptors (Value, ClassLabel, Sequence, Image, Audio, etc.).

Usage

Access dataset.features whenever you need to understand the structure of a dataset before processing. This is commonly done to inspect column names and types, enumerate class labels, determine appropriate preprocessing steps, or validate schema compatibility.

Code Reference

Source Location

  • Repository: datasets
  • File: src/datasets/arrow_dataset.py
  • Lines: L779-L784 (Dataset override), L204-L206 (DatasetInfoMixin base)

Signature

# In Dataset class (override):
@property
def features(self) -> Features:
    features = super().features
    if features is None:  # this is already checked in __init__
        raise ValueError("Features can't be None in a Dataset object")
    return features

# In DatasetInfoMixin base class:
@property
def features(self) -> Optional[Features]:
    return self._info.features.copy() if self._info.features is not None else None

Import

from datasets import load_dataset
ds = load_dataset("dataset_name", split="train")
# Access as a property:
features = ds.features

I/O Contract

Inputs

Name Type Required Description
(none) -- -- This is a property with no parameters. It reads from the internal self._info.features attribute.

Outputs

Name Type Description
(return value) Features An ordered dictionary mapping column names to feature type descriptors. Returns a copy of the internal features to prevent mutation.

Usage Examples

Basic Usage

from datasets import load_dataset

ds = load_dataset("cornell-movie-review-data/rotten_tomatoes", split="train")

# Inspect the features schema
print(ds.features)
# {'text': Value(dtype='string', id=None),
#  'label': ClassLabel(names=['neg', 'pos'], id=None)}

# Get column names
print(list(ds.features.keys()))
# ['text', 'label']

# Check a specific column's type
print(ds.features["label"])
# ClassLabel(names=['neg', 'pos'], id=None)

# Get class label names
print(ds.features["label"].names)
# ['neg', 'pos']

Using Features for Preprocessing Decisions

from datasets import load_dataset, ClassLabel, Value

ds = load_dataset("cornell-movie-review-data/rotten_tomatoes", split="train")

for col_name, feature_type in ds.features.items():
    if isinstance(feature_type, ClassLabel):
        print(f"{col_name}: classification with {feature_type.num_classes} classes")
    elif isinstance(feature_type, Value) and feature_type.dtype == "string":
        print(f"{col_name}: text column")

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment