1

I'm trying to concatenate files in pyfits. Because they weren't done correctly, one of the column name and one of the column format are not the same between my input files, so I can't concatenate the ndarray using np.concatenate ...

I'd like to change the column name (in a clever way). I've found in pyfits documentation the pyfits.Coldefs.change_name function (http://pythonhosted.org/pyfits/api_docs/api_tables.html#coldefs) , so here's what I'm doing right now :

hdu = pyfits.open(myfile,memmap=True)[1]
new_columns = pyfits.ColDefs(hdu.columns).change_name(prev_name,new_name)

I've also tried :

hdu = pyfits.open(myfile,memmap=True)[1]
new_columns = pyfits.ColDefs(hdu.columns).change_attrib(prev_name,'names',new_name)

This returns a None type object. Is there something I'm missing ?

Thanks

Herr
  • 11
  • 1
  • This should be in the FAQ, but basically don't use the `ColDefs` interface it is old and broken. Try using the Astropy Table interface instead: http://docs.astropy.org/en/stable/table/index.html – Iguananaut Jan 09 '15 at 22:16

2 Answers2

1

Although my previous comment on this question still stands, I actually fixed this in Astropy v1.0.2 so that these methods basically work now (https://github.com/astropy/astropy/pull/3283). To rename a column it's as simple as:

hdu.columns[prev_name] = new_name

This will rename the column both in the Numpy array itself, and when the table is written back out to FITS.

Changing the format is less simple than that, and necessitates creating a new table, at the moment.

Iguananaut
  • 15,675
  • 4
  • 43
  • 50
0

In version 4.0.1post1 the solution

hdu.columns[prev_name] = new_name

does not work. It complains about 'ColDefs' not supporting item assignment. To solve this we need to change its name directly:

hdu.columns[prev_name].name = new_name
iprafols
  • 1
  • 1