from pandas import DataFrame, Series
from msgpackutil.base import Hook, HOOKS

__author__ = 'basca'

_DATA = 'd'
_COLS = 'c'
_INDEX = 'i'


class DataFrameHook(Hook):
    def reduce(self, data_frame):
        # _itertuples = data_frame.itertuples
        # _columns = data_frame.columns
        # return {
        # _DATA: [t for t in _itertuples(index=False)],
        # _COLS: [c for c in _columns]
        # }
        # _columns = data_frame.columns
        return {
            _DATA: data_frame.values.tolist(),
            _COLS: data_frame.columns.tolist()
            # _INDEX: data_frame.index.tolist()
        }


    def create(self, data_frame_dict):
        # data = list(data_frame_dict[_DATA])
        # return DataFrame(
        # data=data,
        # columns=data_frame_dict[_COLS]
        # ) if data else DataFrame(columns=data_frame_dict[_COLS])
        data = list(data_frame_dict[_DATA])
        return DataFrame(
            data=data,
            columns=data_frame_dict[_COLS]
            # index=data_frame_dict[_INDEX]
        ) if data else DataFrame(columns=data_frame_dict[_COLS])


class SeriesHook(Hook):
    def reduce(self, series):
        return {
            _DATA: series.values.tolist(),
            _INDEX: series.index.tolist()
        }


    def create(self, series_dict):
        data = list(series_dict[_DATA])
        return Series(
            data=series_dict[_DATA],
            index=series_dict[_INDEX]
        ) if data else Series()


HOOKS.register(1, DataFrameHook)
HOOKS.register(2, SeriesHook)
@@ -201,18 +201,16 @@
 %0A       
- #
  _itertu
@@ -237,34 +237,32 @@
 ertuples%0A       
- #
  _columns = data
@@ -275,34 +275,32 @@
 .columns%0A       
- #
  return %7B%0A      
@@ -301,17 +301,19 @@
         
-#
+   
  _DATA: 
@@ -359,17 +359,19 @@
         
+  
-#
+ 
  _COLS: 
@@ -399,18 +399,16 @@
 %0A       
- #
  %7D%0A     
@@ -415,46 +415,8 @@
    #
- _columns = data_frame.columns%0A       
  ret
@@ -421,35 +421,33 @@
 eturn %7B%0A        
-   
+#
  _DATA: data_fra
@@ -465,32 +465,34 @@
 olist(),%0A       
+ #
      _COLS: data
@@ -523,20 +523,22 @@
         
+#
     
+ 
 # _INDEX
@@ -564,32 +564,34 @@
 tolist()%0A       
+ #
  %7D%0A%0A%0A    def cre
@@ -621,26 +621,24 @@
 ct):%0A       
- #
  data = list
@@ -661,34 +661,32 @@
 %5B_DATA%5D)%0A       
- #
  return DataFram
@@ -696,17 +696,19 @@
         
-#
+   
  data=da
@@ -719,17 +719,19 @@
         
-#
+   
  columns
@@ -761,18 +761,16 @@
 %0A       
- #
  ) if da
@@ -818,32 +818,34 @@
 %5B_COLS%5D)%0A       
+ #
  data = list(dat
@@ -864,32 +864,34 @@
 %5B_DATA%5D)%0A       
+ #
  return DataFram
@@ -893,35 +893,33 @@
 aFrame(%0A        
-   
+#
  data=data,%0A    
@@ -914,36 +914,38 @@
 a=data,%0A        
+#
     
+ 
 columns=data_fra
@@ -962,24 +962,26 @@
 OLS%5D%0A       
+ #
      # index
@@ -1004,32 +1004,34 @@
 %5B_INDEX%5D%0A       
+ #
  ) if data else 

version 0.1.6