---
title: "USDA Fruit And Vegetables"
description: "USDA Fruit And Vegetables dataset from US Department of Agriculture."
canonical: https://www.quantconnect.com/docs/v2/writing-algorithms/datasets/us-department-of-agriculture/usda-fruit-and-vegetables
markdown: https://www.quantconnect.com/docs/v2/writing-algorithms/datasets/us-department-of-agriculture/usda-fruit-and-vegetables.md
language: python
source: https://github.com/QuantConnect/Documentation/tree/master/03%20Writing%20Algorithms/14%20Datasets/30%20US%20Department%20of%20Agriculture/01%20USDA%20Fruit%20And%20Vegetables
modified: 2026-10-30
index: https://www.quantconnect.com/llms.txt
---

# USDA Fruit And Vegetables

[Documentation](https://www.quantconnect.com/docs/v2.md) > [Writing Algorithms](https://www.quantconnect.com/docs/v2/writing-algorithms.md) > Datasets > [US Department of Agriculture](https://www.quantconnect.com/docs/v2/writing-algorithms/datasets/us-department-of-agriculture.md) > USDA Fruit And Vegetables

> Code examples are shown in Python. Append `?lang=csharp` to this URL for the C# version of this page.

## Introduction

The USDA Fruit And Vegetables dataset by the U.S. Department of Agriculture (USDA) tracks retail price estimates for 75+ commonly consumed fresh and processed fruits and vegetables. Includes price per pound/pint and normalized price per edible cup equivalent.

For more information about the USDA Fruit And Vegetables dataset, including CLI commands and pricing, see the [dataset listing](https://www.quantconnect.com/datasets/usda-fruit-and-vegetables).

## About the Provider

The Economic Research Service (ERS) is a federal statistical agency of the U.S. Department of Agriculture (USDA) dedicated to conducting objective economic research that informs public and private decision-making across agriculture, food, the environment, and rural America. ERS serves policymakers at all levels of government, Congress, and industry groups by providing high-quality analysis on topics ranging from farm sector performance and food security to global trade and rural economic development. The USDA Economic Research Service (ERS) publishes retail price data for fruits and vegetables across multiple forms: Fresh, Canned, Frozen, Dried, and Juice.

## Getting Started

The following snippet demonstrates how to request data from the USDA Fruit and Vegetables dataset:

```python
self.dataset_symbol = self.add_data(USDAFruitAndVegetables,
    USDAFruitAndVegetable.Symbols.APPLES, Resolution.DAILY)
```

## Data Summary

The following table describes the dataset properties:

| Property | Value |
| --- | --- |
| Start Date | January 2013 |
| Asset Coverage | 75 Fruits and Vegetables |
| Resolution | Annual |
| Data Density | Sparse |
| Timezone | New York |

## Requesting Data

To add USDA Fruit And Vegetables data to your algorithm, call the `add_data` method. Save a reference to the dataset `Symbol` so you can access the data later in your algorithm.

```python
class USDAFruitAndVegetablesAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self.set_start_date(2013, 1, 1)
        self.set_end_date(2024, 12, 31)
        self.set_cash(100000)
        self.dataset_symbol = self.add_data(USDAFruitAndVegetables, USDAFruitAndVegetable.Symbols.APPLES, Resolution.DAILY)
```

## Accessing Data

To get the current USDA Fruit And Vegetables data, index the current [`Slice`](https://www.quantconnect.com/docs/v2/writing-algorithms/key-concepts/time-modeling/timeslices.md) with the dataset `Symbol`. Slice objects deliver unique events to your algorithm as they happen, but the `Slice` may not contain data for your dataset at every time step. To avoid issues, check if the `Slice` contains the data you want before you index it.

```python
def on_data(self, slice: Slice) -> None:
    if slice.contains_key(self.dataset_symbol):
        collection = slice[self.dataset_symbol]
        for data in collection.data:
            self.log(f"{symbol} [{data.form}]: ${data.price_per_cup_equivalent}")
```

To iterate through all of the dataset objects in the current `Slice`, call the `get` method.

```python
def on_data(self, slice: Slice) -> None:
    for symbol, collection in slice.get(USDAFruitAndVegetables).items():
        # Iterate over all forms in the collection
        for data in collection.data:
            self.log(f"{symbol} [{data.form}]: ${data.price_per_cup_equivalent}")
```

## Historical Data

To get historical USDA Fruit And Vegetables data, call the `history` method with the dataset `Symbol`. If there is no data in the period you request, the history result is empty.

```python
history_df = self.history(self.dataset_symbol, datetime(2013, 1, 1), self.time, Resolution.DAILY)
```

## Remove Subscriptions

To remove a subscription, call the `remove_security` method.

```python
self.remove_security(self.dataset_symbol)
```

## Supported Assets

The USDA Fruit And Vegetable dataset lists data for the following agricultural products:

*Members of `QuantConnect.DataSource.USDAFruitAndVegetable.Symbols` (JSON): https://www.quantconnect.com/services/inspector?type=T:QuantConnect.DataSource.USDAFruitAndVegetable.Symbols&language=python*

## Example Applications

The USDA Fruit and Vegetable dataset provides insights into the agricultural sector. Examples include the following strategies:

- Compare the prices across sub-products (fresh vs frozen)
- Compare the price trends across products (strawberry vs orange)
- Understand how the agricultural market correlates with the stock market

### Classic Algorithm Example

The following example algorithm compares the price difference between fresh and frozen strawberries:

```python
from AlgorithmImports import *

class FreshVsFrozenAlgorithm(QCAlgorithm):

    def initialize(self):
        self.set_start_date(2018, 1, 1)
        self.set_end_date(2023, 12, 31)

        # Single subscription gets both Fresh and Frozen forms
        self.strawberries = self.add_data(USDAFruitAndVegetables, USDAFruitAndVegetable.Symbols.STRAWBERRIES).symbol

        history = self.history(USDAFruitAndVegetables, self.strawberries, datetime(2013,1,1), self.time, flatten=True)

    def on_data(self, slice):
        if slice.contains_key(self.strawberries):
            collection = slice[self.strawberries]

            # Find Fresh and Frozen forms in the collection
            fresh_price = None
            frozen_price = None

            for data in collection.data:
                if data.form == "Fresh":
                    fresh_price = data.value
                elif data.form == "Frozen":
                    frozen_price = data.value

            if fresh_price and frozen_price:
                diff_pct = ((fresh_price / frozen_price) - 1) * 100
                self.log(f"{self.time.year}: Fresh ${fresh_price:.2f} vs Frozen ${frozen_price:.2f} ({diff_pct:+.1f}%)")
```

## Data Point Attributes

The USDA Fruit And Vegetable dataset provides `USDAFruitAndVegetables` and `USDAFruitAndVegetable` objects, which have the following attributes:

### USDAFruitAndVegetables Attributes

*Members of `QuantConnect.DataSource.USDAFruitAndVegetables` (JSON): https://www.quantconnect.com/services/inspector?type=T:QuantConnect.DataSource.USDAFruitAndVegetables&language=python*

### USDAFruitAndVegetable Attributes

*Members of `QuantConnect.DataSource.USDAFruitAndVegetable` (JSON): https://www.quantconnect.com/services/inspector?type=T:QuantConnect.DataSource.USDAFruitAndVegetable&language=python*
