-
classmethod craftutils.wrap.dragons.Table.from_pandas(dataframe, index=
False, units=None) Create a ~astropy.table.Table from a
pandas.DataFrameinstanceIn addition to converting generic numeric or string columns, this supports conversion of pandas Date and Time delta columns to ~astropy.time.Time and ~astropy.time.TimeDelta columns, respectively.
Parameters¶
- dataframe
pandas.DataFrame A pandas
pandas.DataFrameinstance- indexbool
Include the index column in the returned table (default=False)
- units: dict
A dict mapping column names to to a ~astropy.units.Unit. The columns will have the specified unit in the Table.
Returns¶
- table~astropy.table.Table
A ~astropy.table.Table (or subclass) instance
Raises¶
- ImportError
If pandas is not installed
Examples¶
Here we convert a
pandas.DataFrameinstance to a ~astropy.table.QTable.>>> import numpy as np >>> import pandas as pd >>> from astropy.table import QTable>>> time = pd.Series(['1998-01-01', '2002-01-01'], dtype='datetime64[ns]') >>> dt = pd.Series(np.array([1, 300], dtype='timedelta64[s]')) >>> df = pd.DataFrame({'time': time}) >>> df['dt'] = dt >>> df['x'] = [3., 4.] >>> with pd.option_context('display.max_columns', 20): ... print(df) time dt x 0 1998-01-01 0 days 00:00:01 3.0 1 2002-01-01 0 days 00:05:00 4.0>>> QTable.from_pandas(df) <QTable length=2> time dt x Time TimeDelta float64 ----------------------- --------- ------- 1998-01-01T00:00:00.000 1.0 3.0 2002-01-01T00:00:00.000 300.0 4.0- dataframe