pylegs.io.write_table#

write_table(data: DataFrame | Table, path: str | Path | BytesIO, fmt: str | None = None)[source]#

Write a table to a file in various formats.

Parameters:
dataTableLike

The table data to write. This can be an astropy.table.Table or a pandas.DataFrame.

pathPathOrFile

The path or file where the table will be written. This can be a string or a pathlib.Path object.

fmtstr, optional

The format in which to write the table. If not provided, the format is inferred from the file extension.

Raises:
ValueError

If the provided format is not supported.

Notes

The function supports multiple output formats based on the file extension or the fmt parameter:

  • 'fits', 'fit': writes to a FITS file.

  • 'csv': writes to a CSV file.

  • 'parquet': writes to a Parquet file.

  • 'dat': writes to a space-separated text file.

  • 'tsv': writes to a tab-separated text file.

  • 'html': writes to an HTML file.

  • 'feather': writes to a Feather file.

  • 'vo', 'vot', 'votable', 'xml': writes to a VOTable XML file.

Examples

>>> from astropy.table import Table
>>> import pandas as pd
>>> data = Table({'col1': [1, 2], 'col2': [3, 4]})
>>> write_table(data, 'output.fits')
>>> df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
>>> write_table(df, 'output.csv')