You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is it possible to convert an astropy Table to a pandas DataFrame and vise versa? It would be nice to make use of the features of both tools (e.g., the pandas read_sql/write_sql methods). @eteq
@taldcroft - have you thought much about this? An idea here might be a Table.to_pandas method (which of course fails if pandas is not installed), and perhaps also a Table.from_pandas class method (although Table(pandasdataframe) might be a more natural way?)
from astropy.table import Table
from pandas import DataFrame
t = Table(...)
df = DataFrame(...)
df_t = DataFrame(np.array(t))
t_df = Table(np.array(df))
If you have an astropy masked table this will not propagate the masked values, and unfortunately DataFrame does not appear to accept a numpy masked array as input. I don't know enough about Pandas to know the right way to do this. It looks like you need to set missing values to np.nan, but that's just a guess.
@eteq - this has definitely been discussed. I thought there was an issue open but apparently not. From our side it would just be Table(pandas_dataframe) and probably Table.to_pandas(). It would be nice if pandas provided a conversion hook like __array__. Does anyone know if something like that exists?
Activity
eteq commentedon Jul 30, 2014
@taldcroft - have you thought much about this? An idea here might be a
Table.to_pandasmethod (which of course fails if pandas is not installed), and perhaps also aTable.from_pandasclass method (althoughTable(pandasdataframe)might be a more natural way?)taldcroft commentedon Jul 30, 2014
For unmasked tables do:
If you have an astropy masked table this will not propagate the masked values, and unfortunately
DataFramedoes not appear to accept a numpy masked array as input. I don't know enough about Pandas to know the right way to do this. It looks like you need to set missing values tonp.nan, but that's just a guess.taldcroft commentedon Jul 30, 2014
@eteq - this has definitely been discussed. I thought there was an issue open but apparently not. From our side it would just be
Table(pandas_dataframe)and probablyTable.to_pandas(). It would be nice if pandas provided a conversion hook like__array__. Does anyone know if something like that exists?eteq commentedon Jul 30, 2014
@taldcroft - this may be sort of a bug in pandas, but note the following:
It looks like
arraydoesn't correctly convert aDataFrameto a numpy structured array... But the other way does work:So you can do
astropy->pandaseasily, but not vice-versa... so perhaps a reason to at least accept it in theTableinitializer.cdeil commentedon Sep 30, 2014
There's an open issue that's very similar to this one: #1156.
Malewhaler commentedon Oct 9, 2022
A way from DataFrame to Table:
t=astropy.table.Table(f.values,names=f.columns)
taldcroft commentedon Oct 9, 2022
@Malewhaler - the preferred way from DataFrame to Table is below, which has some options and deals with a few subtleties.