-
Notifications
You must be signed in to change notification settings - Fork 0
/
pandas_utils.py
29 lines (26 loc) · 889 Bytes
/
pandas_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from __future__ import print_function, division
from astropy.table import Table
from collections import OrderedDict
import numpy as np
import pandas as pd
def convert_table_to_df(table):
"""
Convert from astropy table to pandas df.
pandas cannot handle multi-dimensional columns, so remove them.
"""
keep = []
for colval, col in zip(table.columns.values(), table.dtype.names):
if getattr(colval, 'ndim', 1) > 1:
print('remove column %s: pandas cannot handle multi-dimensional columns' % col)
else:
keep.append(col)
print('Columns kept:')
print(keep)
return table[keep].to_pandas()
def convert_catalog_to_df(cat):
data_dict = OrderedDict()
for col in cat.columns:
data_dict[col] = cat[col].compute()
data_table = Table(data_dict)
del data_dict
return convert_table_to_df(data_table)