@@ -1648,6 +1648,37 @@ def df_aslarray(df, sort_rows=True, sort_columns=True, **kwargs):
1648
1648
return LArray (data , axes )
1649
1649
1650
1650
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
+
1651
1682
def read_csv (filepath , nb_index = 0 , index_col = [], sep = ',' , headersep = None ,
1652
1683
na = np .nan , sort_rows = True , sort_columns = True , ** kwargs ):
1653
1684
"""
@@ -1733,14 +1764,12 @@ def read_eurostat(filepath, **kwargs):
1733
1764
return read_csv (filepath , sep = '\t ' , headersep = ',' , ** kwargs )
1734
1765
1735
1766
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 ):
1738
1768
"""
1739
1769
read an LArray from a h5 file with the specified name
1740
1770
"""
1741
1771
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 )
1744
1773
1745
1774
1746
1775
def read_excel (filepath , sheetname = 0 , nb_index = 0 , index_col = [],
0 commit comments