Python
Machine Learning
Error Handling
MinMaxScaler
Data Preprocessing

ValueError Found array with 0 sample s shape 0, 1 while a minimum of 1 is required by MinMaxScaler

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

This MinMaxScaler error means the scaler received an array with zero rows. In other words, the shape may still look two-dimensional, such as (0, 1), but there are no actual samples to fit or transform. The scaler cannot compute a minimum or maximum from nothing, so it raises a ValueError.

Why the error happens

MinMaxScaler expects at least one sample because it must compute feature-wise min and max values. This usually fails after a filtering or slicing step accidentally removes every row.

Here is a simple example:

python
1import pandas as pd
2from sklearn.preprocessing import MinMaxScaler
3
4df = pd.DataFrame({"value": [10, 20, 30]})
5filtered = df[df["value"] > 100]
6
7scaler = MinMaxScaler()
8scaler.fit(filtered[["value"]])

filtered[["value"]] has shape (0, 1), so fit fails.

Check the data before scaling

The first fix is to inspect the array just before it reaches the scaler.

python
print(filtered.shape)
print(filtered.head())

If the row count is zero, the problem is upstream, not in MinMaxScaler itself. Common causes include:

  • overly restrictive filters
  • incorrect joins
  • dropping all rows with dropna
  • train-test splits that produce an empty subset
  • slicing mistakes such as df.iloc[0:0]

Finding the step that produced the empty frame is usually more important than adding a defensive cast or workaround near the scaler.

Guard the transform step explicitly

If empty input is a legitimate possibility in your pipeline, guard for it before scaling.

python
1import pandas as pd
2from sklearn.preprocessing import MinMaxScaler
3
4df = pd.DataFrame({"value": [10, 20, 30]})
5filtered = df[df["value"] > 100]
6
7scaler = MinMaxScaler()
8
9if not filtered.empty:
10    scaled = scaler.fit_transform(filtered[["value"]])
11    print(scaled)
12else:
13    print("No rows to scale")

This does not hide the issue. It makes the empty-case behavior explicit so your pipeline can decide whether to skip, log, or fail fast.

The same idea applies if the data is a NumPy array instead of a DataFrame. Check array.shape[0] before calling fit, transform, or fit_transform when empty batches are possible.

Fit once on training data, then transform valid batches

In machine learning workflows, the scaler should usually be fit on the training set and reused later:

python
1from sklearn.preprocessing import MinMaxScaler
2
3train = pd.DataFrame({"value": [10, 20, 30]})
4new_batch = pd.DataFrame({"value": [15, 25]})
5
6scaler = MinMaxScaler()
7scaler.fit(train[["value"]])
8
9scaled_batch = scaler.transform(new_batch[["value"]])
10print(scaled_batch)

If new_batch can be empty, guard it before transform. The key point is still the same: the scaler cannot operate on zero rows.

Build a safer helper

If this happens often in your codebase, wrap the logic in a helper so the empty-case behavior is consistent.

python
1def safe_minmax_transform(scaler, frame, columns):
2    if frame.empty:
3        return frame.copy()
4
5    result = frame.copy()
6    result[columns] = scaler.transform(frame[columns])
7    return result

This is useful in batch pipelines where some partitions may be empty and you want predictable behavior instead of scattered ad hoc checks.

Common Pitfalls

The biggest pitfall is treating the error as a scaler problem rather than a data problem. MinMaxScaler is only reporting that the input has no samples.

Another issue is fitting the scaler on each batch separately. That can produce inconsistent scaling and makes empty-batch problems more frequent. Fit on training data when appropriate, then transform later inputs.

It is also easy to check only the number of columns and miss that the number of rows is zero. A shape like (0, 1) still has one feature, but it has no examples.

Finally, do not "fix" the error by inserting fake data unless that is a deliberate modeling decision. Most of the time, the correct fix is to understand why the batch became empty.

Summary

  • The error means MinMaxScaler received zero samples.
  • Inspect the data just before scaling to find where rows disappeared.
  • Guard empty DataFrames or arrays explicitly if they are a valid case.
  • Fit the scaler on real training data and reuse it for later transforms.
  • Treat the root cause as a data pipeline issue, not a bug in the scaler.

Course illustration
Course illustration

All Rights Reserved.