Table read fails with duplicated column names (with CSV format)
Trying to read in a CSV where there are multiple columns with the same name results in a rather cryptic ValueError:
ValueError: Arguments "names" and "dtype" must match number of columns
Opting out the fast reader it becomes a bit clear:
ValueError: Duplicate column names
In the zeroth order it would be great to receive the second message even in the first case. Then we may want to think about the pro and con of adding a new kwarg rename_duplicate here, too (we already have it for add_column().
>>> from astropy.table import Table
>>> input_text ='angDist,ra,dec,JNAME,ra,dec,errHalfMaj,errHalfMin,errPosAng,W1mag,W2mag,W3mag,W4mag,Jmag,Hmag,Kmag,e_W1mag,e_W2mag,e_W3mag,e_W4mag,e_Jmag,e_Hmag,e_Kmag,cntr,cc_flags,ext_flg,var_flg,ph_qual,r_2mass\n0.167960,2.11044940645274,0.688137082702486,J000826.51+004117.4,2.110478,+0.688174,0.150,0.143,142,14.232,14.199,12.449,8.773,15.150,14.637,14.347,0.031,0.056,,,0.043,0.052,0.064,15100001241034545,0000,0,10nn,AAUU,0.256\n0.537585,2.08882950663451,0.719216362471814,J000821.29+004308.8,2.088721,+0.719113,0.163,0.152,118,14.432,14.408,12.500,8.595,15.042,14.683,14.481,0.033,0.064,,,0.037,0.045,0.062,15100001241034551,0000,0,00nn,AAUU,0.534\n'
>>> Table.read(input_text, format='csv')
ValueError: Arguments "names" and "dtype" must match number of columns
>>> Table.read(input_text, format='csv', fast_reader=False)
ValueError: Duplicate column names
This example is coming from using the remote XMatch service with astroquery. I suppose as a workaround we can switch to use votable rather than csv (as the votable reader renames the columns automatically), but nevertheless it would be nice to fix this.
I am interested in changing the error message here, What about rename_duplicate()
@Himanshu-Garg - if you are interested then you should stick with a small fix to improve the exception message for this particular case. That should be straightforward.
The larger issue of column renaming is a bit tricky because of possible interactions with format guessing and existing args names, include_names, exclude_names. But agreed one should be able to read in a table that has non-unique names.
I'm working on a fix for the generalized case of duplicate column names. The basic fix is quite straightforward, but I'm hitting some problems in the case of duplicate column names with include_names or exclude_names also provided.
But in general the idea is to always iteratively update column names like col => col_1 => col_2 until all names are unique.