Skip to content

ValueError: No objects to concatenate

Did a loop that reads a directory of files end in this?

pd.concat([])
ValueError: No objects to concatenate

The list is empty. concat has nothing to work with and no way to guess what the result should look like — not the columns, not the dtypes, not even whether it should be a Series or a data frame — so it stops.

Note that a list of empty frames is a different thing and works fine:

pd.concat([pd.DataFrame(), pd.DataFrame()])
Empty DataFrame
Columns: []
Index: []

So the message is precise: no objects, rather than no rows.

It is almost always a glob that matched nothing

The shape that produces it in real code:

pd.concat([pd.read_csv(f) for f in glob.glob('data/*.csv')])

If data/ is empty, the path is wrong, or the extension is .CSV on a case-sensitive filesystem, the comprehension produces [] and concat raises. The error names concat, but the bug is one line earlier.

Check the list before using it, and say something useful when it is empty:

paths = sorted(glob.glob('data/*.csv'))
if not paths:
    raise SystemExit('no CSV files found in data/ — check the path')

sales = pd.concat([pd.read_csv(p) for p in paths], ignore_index=True)

Three extra lines, and the next person to run it on a fresh checkout gets told what is wrong instead of a message about concatenation.

The filtered-groups version

The other common source is a comprehension with a condition that everything failed:

pd.concat([g for _, g in sales.groupby('region') if len(g) > 100])

Perfectly good code until the day no group has more than a hundred rows. If an empty result is legitimate, handle it rather than letting concat decide:

big = [g for _, g in sales.groupby('region') if len(g) > 100]
result = pd.concat(big, ignore_index=True) if big else sales.iloc[:0]

sales.iloc[:0] gives an empty frame with the right columns and dtypes, which is far friendlier to whatever comes next than either an exception or a bare pd.DataFrame().

The mistakes I see most often

Passing a generator. pd.concat(pd.read_csv(f) for f in paths) works, but if the generator yields nothing you get this same error with no list to inspect afterwards. A list comprehension is easier to debug.

Catching the exception and moving on. It hides a missing-data problem that will be worse later. The empty list is information.

Relative paths in notebooks. The working directory is wherever the kernel started, which is not always where the notebook lives.

Practice

See concat in Pandas for combining frames, and read_csv in Pandas for loading them.