question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ENH: Make concat succeed on empty list.

See original GitHub issue

Is your feature request related to a problem?

Yes, pd.concat([]) currently raises the exception, ValueError(“No objects to concatenate”).

Describe the solution you’d like

To be correct on all input sizes, it should return an empty DataFrame.

API breaking implications

Obviously, concat will no longer detect empty data. However, empty data will detect itself.

Additional context

The concatenation operation is for lists what addition is for numbers. Concatenation of an empty list should produce an empty dataframe, because the empty dataframe represents 0. Consider

sum([])

which python evaluates to 0.

This makes concatenation and sum commute, sum([1] + []) == sum([1]) + sum([]).

This already works for an empty dataframe:

pd.concat([pd.DataFrame({'A':[1]}), pd.DataFrame()])

if is very odd for concat([]) not to produce a DataFrame, since its input has the correct type.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mroeschkecommented, May 7, 2021

I find the current behavior correct as is.

For reference np.concatenate also raises a similar error as pd.concat

In [1]: np.concatenate([])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-7806ca832c55> in <module>
----> 1 np.concatenate([])

<__array_function__ internals> in concatenate(*args, **kwargs)

ValueError: need at least one array to concatenate

And I believe it’s more explicit for the user to decide what to do in code flow if they get to a pd.concat([]) situation.

So I’d be -1 on any changes here.

0reactions
mroeschkecommented, Aug 19, 2021

Thanks for the suggestion, but also from the review on a proposal PR https://github.com/pandas-dev/pandas/pull/41355#pullrequestreview-654681587, I think this behavior is best left for the user to decide manually what the result of empty concatenation should be. Closing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Include empty series when creating a pandas dataframe with ...
I want to create a pandas dataframe from a list of series using .concat. The problem is that when one of the series...
Read more >
Bug using Concatenate/Append on tables having columns ...
I just see there is a forth row in dtA with no entry because of the list check. If you use the menu...
Read more >
List (Java SE 19 & JDK 19) - Oracle Help Center
An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element...
Read more >
Policy Language
If one of the bindings does not yield a successful evaluation of the body, the overall statement is undefined. If the domain is...
Read more >
jQuery API Documentation
jQuery API. jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found