Home > front end >  Can I avoid a ValueError concatenating empty dataframes?
Can I avoid a ValueError concatenating empty dataframes?

Time:12-04

I have df = pd.concat(dict_of_df, axis=0) and sometimes [rarely] it might be the case that all of the df in the dictionary are empty in which case I would like Pandas to cheerfully return an empty dataframe. But instead I get a ValueError.

I can write a loop to check for the length of each df before calling concat, but would prefer to not always do that, so at the moment I just embed the concat into a try/except... which doesnt make be really happy either because if there was a "true" ValueError I would like to have know it. So then I could do a try/except loop and if exception is thrown then do a count of all the dicts and ... ugh. This is getting crazy.

Is there something more clean? Thanks.

CodePudding user response:

I don't have this problem:

pd.concat([pd.DataFrame()] * 3, axis=0)

What version of pandas are you using?

CodePudding user response:

Sorry, I am going to withdraw the question. I now realize that pd.concat([None,None]) produces the ValueError, whereas as noted above pd.concat(pd.DataFrame(),pd.DataFrame()) does exactly what you would hope. Also pd.concat([None,pd.DataFrame()]) is fine too. So it's not really fair of me to complain about concat. I need to stop feeding my routine non-existent datasets !

Thanks for feedback

  • Related