ENH: Make concat succeed on empty list.
See original GitHub issueIs 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:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I find the current behavior correct as is.
For reference
np.concatenate
also raises a similar error aspd.concat
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.
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.