# Unable to instantiate astropy.io.fits.TableHDU class

**URL:** <https://community.openastronomy.org/t/unable-to-instantiate-astropy-io-fits-tablehdu-class/519>\
**Category:** Astropy\
**Created:** [November 22, 2022, 10:04pm UTC](https://community.openastronomy.org/t/unable-to-instantiate-astropy-io-fits-tablehdu-class/519 "2022-11-22T22:04:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpelliott](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@jpelliott](https://community.openastronomy.org/u/jpelliott)\
**Post date:** [November 22, 2022, 10:04pm UTC](https://community.openastronomy.org/t/unable-to-instantiate-astropy-io-fits-tablehdu-class/519/1 "2022-11-22T22:04:52Z")

</div>

I’m attempting to instantiate a simple ASCII HDU, but it doesn’t seem to work the same way as `astropy.io` `.fits.BinTableHDU` . For example, this works just fine:

```auto
from astropy.table import Table
from astropy.io.fits import TableHDU, BinTableHDU
t1 = Table()
t1['col1'] = ['test 1', 'test 2', 'test 3']
t1['col2'] = [1, 2, 3]
hdu = BinTableHDU(data=t1, name='MY_BINARY_HDU')
hdu.writeto('binary_test.fits', overwrite=True)

```

but attempting to do the same thing as an ASCII table doesn’t:

```auto
t2 = Table()
t2['col1'] = ['test 1', 'test 2', 'test 3']
t2['col2'] = [1, 2, 3]

hdu = TableHDU(data=t2, name='MY_ASCII_HDU')
hdu.writeto('ascii_test.fits', overwrite=True)

```

yielding this error:

```auto
TypeError: Table data has incorrect type.

```

Am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![saimn](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/saimn/32/40_2.png) [@saimn](https://community.openastronomy.org/u/saimn)\
**Post date:** [November 23, 2022, 1:10pm UTC](https://community.openastronomy.org/t/unable-to-instantiate-astropy-io-fits-tablehdu-class/519/2 "2022-11-23T13:10:01Z")

</div>

It’s not possible to build a `TableHDU` from a `Table`, so you have to build it directly with the `io.fits` API: [Less Familiar Objects — Astropy v5.3.dev0](https://docs.astropy.org/en/latest/io/fits/usage/unfamiliar.html#creating-an-ascii-table)

---

<div class="post-metadata">

**Author:** ![dhomeier](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/dhomeier/32/65_2.png) [@dhomeier](https://community.openastronomy.org/u/dhomeier)\
**Post date:** [November 23, 2022, 1:59pm UTC](https://community.openastronomy.org/t/unable-to-instantiate-astropy-io-fits-tablehdu-class/519/3 "2022-11-23T13:59:02Z")

</div>

In your example you might also instantiate it directly from the previously written table

```auto
with fits.open('binary_test.fits') as hdulist:
    hdu = TableHDU(data=hdulist[1].data, name='MY_ASCII_HDU')

```

but that is probably not very helpful in the general case.  
The docstring for that class is a bit confusing as there seems to be almost no other way to instantiate it through the `data` parameter, but the ASCII table format is rarely used.

---

<div class="post-metadata">

**Author:** ![dhomeier](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/dhomeier/32/65_2.png) [@dhomeier](https://community.openastronomy.org/u/dhomeier)\
**Post date:** [November 23, 2022, 2:14pm UTC](https://community.openastronomy.org/t/unable-to-instantiate-astropy-io-fits-tablehdu-class/519/4 "2022-11-23T14:14:30Z")

</div>

`TableHDU.from_columns` as explained in the link above is probably the most useful way to create it; it should also directly handle numpy structured arrays, if that is more convenient.

```auto
t2 = np.recarray(3, dtype=[('col1', 'S6'), ('col2', 'i4')])
t2['col1'] = ['test 1', 'test 2', 'test 3']
t2['col2'] = np.arange(1, 4)

hdu = TableHDU.from_columns(t2, name='MY_ASCII_HDU')

```

---

<div class="post-metadata">

**Author:** ![jpelliott](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@jpelliott](https://community.openastronomy.org/u/jpelliott)\
**Post date:** [November 28, 2022, 7:28pm UTC](https://community.openastronomy.org/t/unable-to-instantiate-astropy-io-fits-tablehdu-class/519/5 "2022-11-28T19:28:25Z")

</div>

Thanks, this is a huge help. I think this is exactly what I was looking for. It would be nice however if astropy used the same interface for both kinds of tables, that would help to encapsulate many of these details. Although… perhaps it’s better to use the fits Column method in both instances?

---

<div class="post-metadata">

**Author:** ![jpelliott](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@jpelliott](https://community.openastronomy.org/u/jpelliott)\
**Post date:** [November 28, 2022, 7:29pm UTC](https://community.openastronomy.org/t/unable-to-instantiate-astropy-io-fits-tablehdu-class/519/6 "2022-11-28T19:29:23Z")

</div>

Thanks dhomeier, this also works great, as does the suggestion by saimn. Thanks!

---

<div class="post-metadata">

**Author:** ![saimn](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/saimn/32/40_2.png) [@saimn](https://community.openastronomy.org/u/saimn)\
**Post date:** [November 28, 2022, 8:23pm UTC](https://community.openastronomy.org/t/unable-to-instantiate-astropy-io-fits-tablehdu-class/519/7 "2022-11-28T20:23:34Z")

</div>

The `Table` to `TableHDU` interface doesn’t exist because ASCII table are not so common, binary table are much more used.  
So on one side there is the API from `astropy.io.fits`, for ASCII and binary tables :

- [Table Data — Astropy v5.3.dev23+gb2b32a4dd](https://docs.astropy.org/en/latest/io/fits/usage/table.html#creating-a-fits-table)
- [Less Familiar Objects — Astropy v5.3.dev23+gb2b32a4dd](https://docs.astropy.org/en/latest/io/fits/usage/unfamiliar.html#creating-an-ascii-table)

And then there is `astropy.table.Table`, which can currently be converted only to a `io.fits` binary table (`BinTableHDU`).
