Skip to content

Commit 36d9fcb

Browse files
committed
progress on reading hdf via pandas but do not expand them
1 parent de9eb5a commit 36d9fcb

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

larray/core.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,37 @@ def df_aslarray(df, sort_rows=True, sort_columns=True, **kwargs):
16481648
return LArray(data, axes)
16491649

16501650

1651+
class DataFrameWrapper(object):
1652+
def __init__(self, df):
1653+
self.df = df
1654+
1655+
def __getitem__(self, key):
1656+
return self.df[key]
1657+
1658+
def __getattr__(self, key):
1659+
return getattr(self.df, key)
1660+
1661+
1662+
#TODO: implement sort_columns
1663+
def df_aslarray2(df, sort_rows=True, sort_columns=True, **kwargs):
1664+
axes_names = [decode(name, 'utf8') for name in df.index.names]
1665+
if axes_names == [None]:
1666+
last_axis = None, None
1667+
else:
1668+
last_axis = axes_names[-1].split('\\')
1669+
axes_names[-1] = last_axis[0]
1670+
#FIXME: hardcoded "time"
1671+
axes_names.append(last_axis[1] if len(last_axis) > 1 else 'time')
1672+
1673+
axes_labels = df_labels(df, sort=sort_rows)
1674+
# pandas treats the "time" labels as column names (strings) so we need
1675+
# to convert them to values
1676+
axes_labels.append([parse(cell) for cell in df.columns.values])
1677+
1678+
axes = [Axis(name, labels) for name, labels in zip(axes_names, axes_labels)]
1679+
return LArray(DataFrameWrapper(df), axes)
1680+
1681+
16511682
def read_csv(filepath, nb_index=0, index_col=[], sep=',', headersep=None,
16521683
na=np.nan, sort_rows=True, sort_columns=True, **kwargs):
16531684
"""
@@ -1733,14 +1764,12 @@ def read_eurostat(filepath, **kwargs):
17331764
return read_csv(filepath, sep='\t', headersep=',', **kwargs)
17341765

17351766

1736-
def read_hdf(filepath, key, na=np.nan, sort_rows=True, sort_columns=True,
1737-
**kwargs):
1767+
def read_hdf(filepath, key, sort_rows=True, sort_columns=True, **kwargs):
17381768
"""
17391769
read an LArray from a h5 file with the specified name
17401770
"""
17411771
df = pd.read_hdf(filepath, key, **kwargs)
1742-
return df_aslarray(df, sort_rows=sort_rows, sort_columns=sort_columns,
1743-
fill_value=na)
1772+
return df_aslarray2(df, sort_rows=sort_rows, sort_columns=sort_columns)
17441773

17451774

17461775
def read_excel(filepath, sheetname=0, nb_index=0, index_col=[],

0 commit comments

Comments
 (0)