
x\c        	   @   s@  d  Z  d d l m Z m Z m Z d d l Z d d l m Z d d l Z d d l m	 Z	 d d l
 Z d d l j Z d d l m Z m Z d d l m Z d d l m Z m Z d d	 l m Z m Z m Z m Z m Z d d
 l m Z m Z m  Z  m! Z! m" Z" m# Z# m$ Z$ m% Z% m& Z& m' Z' m( Z( m) Z) m* Z* m+ Z+ m, Z, m- Z- m. Z. m/ Z/ d d l0 m1 Z1 d d l2 m3 Z3 m4 Z4 m5 Z5 d d l6 m7 Z7 m8 Z8 d d l9 m: Z: d d l; m< Z< m= Z= m> Z> d d l? m@ Z@ mA ZA d d lB mC ZC mD ZD d d lE mF ZF mG ZH mI ZJ y d d lK ZK d   ZL Wn eM k
 r:d   ZL n Xe d  eL _  y d d lN ZN d   ZO Wn eM k
 rd   ZO n Xe d  eO _  d aQ d   ZR d eS d  ZT i e jU d  d 6e jU d  e d  6e jU d  e d   6e jU d!  d" 6e jU d!  e d#  6e jU d$  e d%  6e jU d&  d' 6d( d( 6ZV d)   ZW i e jX d* 6e jX d+ 6e jY d, 6ZZ e[ e d-  re j\ eZ d. <n  d/   Z] d0   Z^ d d1  Z_ d2   Z` d3   Za e` d d4 eS d5 d5 d6  Zb ea d eS d d4 d d7 eF d8  Zc d9 eH f d:     YZG d; eJ f d<     YZI d= ed f d>     YZe d S(?   s  
Msgpack serializer support for reading and writing pandas data structures
to disk

portions of msgpack_numpy package, by Lev Givon were incorporated
into this module (and tests_packers.py)

License
=======

Copyright (c) 2013, Lev Givon.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the following
  disclaimer in the documentation and/or other materials provided
  with the distribution.
* Neither the name of Lev Givon nor the names of any
  contributors may be used to endorse or promote products derived
  from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
i(   t   datet   datetimet	   timedeltaN(   t   dedent(   t   parse(   t   ut   u_safe(   t   PerformanceWarning(   t   BadMovet   move_into_mutable_buffer(   t   is_categorical_dtypet   is_datetime64tz_dtypet   is_object_dtypet   needs_i8_conversiont   pandas_dtype(   t   Categoricalt   CategoricalIndext	   DataFramet   DatetimeIndext   Float64Indext   Indext
   Int64Indext   Intervalt   IntervalIndext
   MultiIndext   NaTt   Panelt   Periodt   PeriodIndext
   RangeIndext   Seriest   TimedeltaIndext	   Timestamp(   t	   internals(   t   DatetimeArrayt   IntervalArrayt   PeriodArray(   t
   BlockIndext   IntIndex(   t   NDFrame(   t   BlockManagert   _safe_reshapet
   make_block(   t   SparseDataFramet   SparseSeries(   t   _stringify_patht   get_filepath_or_buffer(   t   ExtTypet   Packert   Unpackerc           C   s   d  S(   N(    (    (    (    s0   lib/python2.7/site-packages/pandas/io/packers.pyt   _check_zlibM   s    c           C   s   t  d   d  S(   Ns   zlib is not installed(   t   ImportError(    (    (    s0   lib/python2.7/site-packages/pandas/io/packers.pyR2   P   s    sv       Check if zlib is installed.

    Raises
    ------
    ImportError
        Raised when zlib is not installed.
    c           C   s   d  S(   N(    (    (    (    s0   lib/python2.7/site-packages/pandas/io/packers.pyt   _check_blosca   s    c           C   s   t  d   d  S(   Ns   blosc is not installed(   R3   (    (    (    s0   lib/python2.7/site-packages/pandas/io/packers.pyR4   d   s    sx       Check if blosc is installed.

    Raises
    ------
    ImportError
        Raised when blosc is not installed.
    c            s    j  d d  a t r' t t  a n   j  d d  } | rH d } n d }    f d   } t |   }  t |  t j  r t |  |   } | |  Wd QXn6 |  d k r t j	   } | |  | j
   S| |   d S(   sF  
    msgpack (serialize) object to input file path

    THIS IS AN EXPERIMENTAL LIBRARY and the storage format
    may not be stable until a future release.

    Parameters
    ----------
    path_or_buf : string File path, buffer-like, or None
                  if None, return generated string
    args : an object or objects to serialize
    encoding : encoding for unicode objects
    append : boolean whether to append to an existing msgpack
             (default is False)
    compress : type of compressor (zlib or blosc), default to None (no
               compression)
    t   compresst   appends   a+bt   wbc            s+   x$   D] } |  j  t |    q Wd  S(   N(   t   writet   pack(   t   fht   a(   t   argst   kwargs(    s0   lib/python2.7/site-packages/pandas/io/packers.pyt   writer   s    N(   t   popt   Nonet
   compressorR   R-   t
   isinstancet   compatt   string_typest   opent   BytesIOt   getvalue(   t   path_or_bufR<   R=   R6   t   modeR>   R:   t   buf(    (   R<   R=   s0   lib/python2.7/site-packages/pandas/io/packers.pyt
   to_msgpackw   s"    	

s   utf-8c            se  t    \  } }  | r( t   S     f d   } t  t j  r y t j j   } Wn t t	 f k
 r t
 } n X| r t  d   } | |  SWd QXq n  t  t j  r*d } z< t j s t  d  d k r	t j   } | |  SWd | d k	 r&| j   n  Xn+ t  d  rUt j  j  rU|   St	 d   d S(   s  
    Load msgpack pandas object from the specified
    file path

    THIS IS AN EXPERIMENTAL LIBRARY and the storage format
    may not be stable until a future release.

    Parameters
    ----------
    path_or_buf : string File path, BytesIO like or string
    encoding : Encoding for decoding msgpack str type
    iterator : boolean, if True, return an iterator to the unpacker
               (default is False)

    Returns
    -------
    obj : same type as object stored in file
    c            sd   t  t |  d     } t |  d k r5 | d S r` y  j   Wq` t k
 r\ q` Xn  | S(   Nt   encodingi   i    (   t   listt   unpackt   lent   closet   IOError(   R:   t   unpacked_obj(   RL   R=   RH   t   should_close(    s0   lib/python2.7/site-packages/pandas/io/packers.pyt   read   s    t   rbNi    i   RT   s7   path_or_buf needs to be a string file path or file-like(   R.   t   IteratorRB   RC   RD   t   ost   patht   existst	   TypeErrort
   ValueErrort   FalseRE   t   binary_typeR@   t   PY3t   ordRF   RP   t   hasattrt   callableRT   (   RH   RL   t   iteratorR=   t   _RT   RY   R:   (    (   RL   R=   RH   RS   s0   lib/python2.7/site-packages/pandas/io/packers.pyt   read_msgpack   s.    

!
s   M8[ns]i   s   datetime64[ns]s   M8[us]s   datetime64[us]s   m8[ns]i   s   timedelta64[ns]s   m8[us]s   timedelta64[us]t   int64i   t   categoryc         C   s'   |  t  k r t  |  St j j |  |   S(   s1    return my dtype mapping, whether number or name (   t
   dtype_dictt   npt   typeDictt   get(   t   t(    (    s0   lib/python2.7/site-packages/pandas/io/packers.pyt	   dtype_for   s    t   complext
   complex128t	   complex64t   float128t
   complex256c         C   s/   t  | } t j | | |   d | |   S(   sO   
    Convert strings to complex number instance with specified numpy type.
    y              ?(   t   c2f_dictRh   Ri   (   t   rt   it
   ctype_namet   ftype(    (    s0   lib/python2.7/site-packages/pandas/io/packers.pyt   c2f  s    
c         C   s  |  j  } t |   r |  St |  r5 |  j   j   St |  rS |  j d  }  n  |  j   } t d k r t   | t	 j
 k r | j   S| j   } t d t j |   St d k rt   | t	 j
 k r | j   S| j   } t d t j | d | j  St d | j    S(   s$    convert the numpy values to a list t   i8t   zlibi    t   blosct   typesize(   t   dtypeR
   R   t   ravelt   tolistR   t   viewRA   R2   Rh   t   object_t   tostringR/   Ry   R5   R4   Rz   t   itemsize(   t   valuesR|   t   v(    (    s0   lib/python2.7/site-packages/pandas/io/packers.pyt   convert  s*    	

c         C   su  t  |  t  o |  j d k } | r0 |  j }  n  t |  r@ |  St |  r_ t j |  d t St	 |  j
 } | s |  j d  }  n  | rD| d k r t   t j } n+ | d k r t   t j } n t d   y# t j t | |    d | SWqDt k
 r@} | j d }  t |   d k rAt j d t  qAqDXn  t j |  d | } | j   } t | j _ | S(	   Ni    R|   t   latin1u   zlibu   bloscs)   compress must be one of 'zlib' or 'blosc'i   sU   copying data after decompressing; this may mean that decompress is caching its result(   RB   R/   t   codet   dataR
   R   Rh   t   arrayt   objectR   t   baset   encodeR2   Ry   t
   decompressR4   Rz   R[   t
   frombuffert   _move_into_mutable_buffert   _BadMoveR<   RO   t   warningst   warnR   t   copyt   Truet   flagst	   writeable(   R   R|   R5   t	   as_is_extR   t   eRJ   (    (    s0   lib/python2.7/site-packages/pandas/io/packers.pyt	   unconvert5  s@    c         C   s;	  t  |   } t |  t  rEt |  t  r i d d 6t |  j j  d 6t |  d dJ  d 6t |  d dJ  d 6t |  d dJ  d	 6t |  d
 dJ  d 6St |  t	  ri d d 6t |  j j  d 6t |  d dJ  d 6t
 t |  d dJ   d 6t |  j j  d 6t |  j  d 6t d 6St |  t  rt |  d dJ  } | dJ k	 ret | j  } |  j d  }  n  i d d 6t |  j j  d 6t |  d dJ  d 6t |  j j  d 6t |  j  d 6t
 t |  d dJ   d 6| d 6t d 6St |  t t f  r}t |  t  rd } n d } i | d 6t |  j j  d 6t |  d dJ  d 6t |  d dJ  d 6t |  d dJ  d 6t |  d dJ  d 6St |  t  ri d d 6t |  j j  d 6t |  d dJ  d  6t |  j j  d 6t |  j  d 6t d 6Si d! d 6t |  j j  d 6t |  d dJ  d 6t |  j j  d 6t |  j  d 6t d 6Snt |  t  ri d" d 6t |  j j  d 6t |  d dJ  d 6|  j d# 6|  j d$ 6|  j d% 6t d 6St |  t  r?t |  t  rt d&   q7	i d' d 6t |  j j  d 6t |  d dJ  d 6|  j d! 6t |  j j  d 6t |  j  d 6t d 6Snt | t   r/t |  t!  rlt d(   q7	|  j" } | j#   s| j$   } n  i d) d 6t |  j j  d 6| j% d* 6g  | j& D]c } i | j' j( d+ 6t | j  d, 6| j j) d- 6t | j j  d 6t | j j  d 6t d 6^ qd. 6Snt |  t* t+ t, j- t. t, j/ f  s_|  t0 k rt |  t1  r|  j2 } | dJ k	 rt | j  } n  |  j3 } | dJ k	 rt | j4  } n  i d/ d 6|  j5 d0 6| d 6| d 6S|  t0 k ri d1 d 6St |  t, j/  r#i d2 d 6|  j6 d3  d 6St |  t.  rVi d4 d 6|  j7 |  j8 |  j9 f d 6St |  t, j-  ri d5 d 6t t: |    d 6St |  t*  ri d6 d 6t |  j;    d 6St |  t+  ri d7 d 6t |  j;    d 6St< d8 j= d9 |     n<t |  t>  r/i d: d 6|  j? d; 6t
 |  j4  d 6St |  t@  rgi d< d 6|  jA d 6|  jB d 6|  jC d 6St |  tD  ri d= d 6t |  j j  d 6|  jE d> 6|  jF d? 6|  jG d@ 6St |  tH  ri dA d 6t |  j j  d 6|  jI dB 6|  jG d@ 6St |  t, jJ  rKi dC d 6|  j) d- 6|  jK dD 6t |  j j  d 6t |   d 6t d 6St |  t, jL  rt, jM |   ri dE d 6dF dG 6t |  j j  d 6t |  jN jO    dH 6t |  jP jO    dI 6Si dE d 6t |  j j  d 6t |  jO    d 6SnF t |  tQ  r7	i dF d 6t |  jN jO    dH 6t |  jP jO    dI 6S|  S(K   s   
    Data encoder
    u   range_indexu   typu   klasst   nameu   namet   _startu   startt   _stopu   stopt   _stepu   stepu   period_indext   freqstru   frequ   dtypeu   datau   compresst   tzt   UTCu   datetime_indexu   tzu   interval_indexu   interval_arrayt   leftu   leftt   rightu   rightt   closedu   closedu   multi_indext   namesu   namesu   indexu   categoryu   codesu
   categoriesu   ordereds(   msgpack sparse series is not implementedu   seriess'   msgpack sparse frame is not implementedu   block_manageru   axesu   locsu   valuesu   shapeu   blocksu	   timestampu   valueu   natu   timedelta64Rx   u	   timedeltau
   datetime64u   datetimeu   dates-   cannot encode this datetimelike object: {obj}t   obju   periodu   ordinalu   intervalu   block_indexu   blocsu   blengthsu   lengthu	   int_indexu   indicesu   ndarrayu   ndimu	   np_scalaru
   np_complexu   sub_typu   realu   imagN(R   t   typeRB   R   R   R   t	   __class__t   __name__t   getattrR@   R   R   R|   R   R   t   asi8RA   R   t   zonet
   tz_convertR   R#   R   R   R   t   codest
   categoriest   orderedR   R,   t   NotImplementedErrort   indext
   issubclassR'   R+   t   _datat   is_consolidatedt   consolidatet   axest   blockst   mgr_locst   as_arrayt   shapeR   R    Rh   t
   datetime64R   t   timedelta64R   R    t   tzinfot   freqR   t   valueR   t   dayst   secondst   microsecondst   strt	   isoformatt	   Exceptiont   formatR   t   ordinalR   R   R   R   R%   t   blocst   blengthst   lengthR&   t   indicest   ndarrayt   ndimt   numbert   iscomplexobjt   realt   __repr__t   imagRm   (   R   t   tobjR   t   typR   t   bR   (    (    s0   lib/python2.7/site-packages/pandas/io/packers.pyR   o  s<   


	








	

u		






















c      	      s+  |  j  d  } | dF k r |  S| d k ri d |  k rA |  d n |  d } t |  d d |  d d | S| d	 k ry t S| d
 k r t d |  d d |  d  S| d k r t |  d  } t |  d | |  j  d   } t | d | d |  d S| d k r't |  d |  d |  d d |  d S| d k rt |  d  } t |  d | |  j  d   } g  | D] } t	 |  ^ qi} t
 j | d |  d S| d k rt |  d t j |  j  d   } t d |  d d |  d  } | j d dF  } t t | |  |  S| d k rt |  d t j |  j  d   } t d |  d d |  d  } t | |  } |  d } | dF k	 r| j d  j |  } n  | S| dG k rt   |  d  j |  d! |  d" |  d# d |  d S| d$ k rt   |  d  j }	 |	 d% |  d& d' |  d( d) |  d*  S| d+ k rGt |  d! |  d" |  d#  S| d, k rt |  d  } t |  }
 |  d } t t |  d | |  d  d- | d |
 d |  d } | S| d. k r|  d/     f d0   } g  |  d1 D] } | |  ^ q} t   |  d  t |     S| d2 k r3t |  d  S| d3 k rVt j t |  d   S| d4 k rvt |  d  j   S| d5 k rt |  d   S| d6 k rt j  t! |  d   S| d7 k rt   |  d  |  d8 |  d9 |  d:  S| d; k rt   |  d  |  d8 |  d<  S| d= k rSt |  d t j" |  d |  j  d   j# |  d>  S| d? k r|  j  d@  dA k rt$ |  dB |  dC |  d  St |  d  } y | |  d  SWq't% t& f k
 r| j' |  d  SXnJ | dA k rt( |  dB dD |  dC dE  St) |  t t* t+ f  r#|  S|  SdF S(H   s5   
    Decoder for deserializing numpy data types.
    u   typu	   timestampR   u   frequ   offsetu   valueR   u   tzu   natu   periodR   u   ordinalu   indexu   dtypeu   datau   compressR|   R   u   nameu   range_indexu   startu   stopu   stepu   multi_indexR   u   namesu   period_indexu   datetime_indexR   u   interval_indext   interval_arrayu   klassu   leftu   rightu   closedu   categoryR   u   codesR   u
   categoriesR   u   orderedu   intervalu   seriesR   u   block_manageru   axesc      
      s   t  t |  d t |  d  |  d  |  d  } d |  k rJ |  d } n   d j |  d  } t |  d  r t | t j  s t t	 |    | j
 d k s t | j
   t | d	 |  d } n  t d
 | d t t |  d  d | d	 |  d  S(   Nu   valuesu   dtypeu   compressu   shapeu   locsi    u   itemss   M8[ns]R|   R   t   klassu   klasst	   placement(   R)   R   Rl   t   get_indexerR   RB   Rh   R   t   AssertionErrorR   R|   R"   R*   R   R!   (   R   R   R   (   R   (    s0   lib/python2.7/site-packages/pandas/io/packers.pyt   create_block  s    $u   blocksu   datetimeu
   datetime64u   dateu	   timedeltau   timedelta64u   block_indexu   lengthu   blocsu   blengthsu	   int_indexu   indicesu   ndarrayu   shapeu	   np_scalaru   sub_typu
   np_complexu   realu   imagu   +u   jN(   u   interval_indexR   (,   Rj   R@   R    R   R   Rl   R   R   R   t   tupleR   t   from_tuplesRh   Re   t   dictR?   R   R$   R   t   tz_localizeR   t   globalst   from_arrayst
   from_codesR   R   R   R(   R   R   R    R   R   t   intRi   t   reshapeRw   R[   RZ   R   Rm   RB   RM   t   set(   R   R   R   R|   R   t   xt   dt   resultR   R   t   pd_dtypeR   R   R   R   (    (   R   s0   lib/python2.7/site-packages/pandas/io/packers.pyt   decode:  s     

""




# t   stricti   c         C   s4   t  d | d | d | d | d | d |  j |   S(   s5   
    Pack an object and return the packed bytes.
    t   defaultRL   t   unicode_errorst   use_single_floatt	   autoresett   use_bin_type(   R0   R9   (   t   oR   RL   R   R   R   R   (    (    s0   lib/python2.7/site-packages/pandas/io/packers.pyR9     s
    i    c	   	      C   s:   t  |  d | d | d | d | d | d | d | d | S(	   sf   
    Unpack a packed object, return an iterator
    Note: packed lists will be returned as tuples
    t   object_hookt	   list_hookt   use_listRL   R   t   object_pairs_hookt   max_buffer_sizet   ext_hook(   R1   (	   t   packedR   R   R   RL   R   R   R   R   (    (    s0   lib/python2.7/site-packages/pandas/io/packers.pyRN     s    	R0   c           B   s#   e  Z e d  d e d d d  Z RS(   s   utf-8R   i   c         C   s;   t  t |   j d | d | d | d | d | d |  d  S(   NR   RL   R   R   R   R   (   t   superR0   t   __init__(   t   selfR   RL   R   R   R   R   (    (    s0   lib/python2.7/site-packages/pandas/io/packers.pyR     s    (   R   t
   __module__R   R\   R   (    (    (    s0   lib/python2.7/site-packages/pandas/io/packers.pyR0     s   R1   c           B   s/   e  Z d d  e e d d d d d  e d 
 Z RS(   i    s   utf-8R   c         C   sS   t  t |   j d | d | d | d | d | d | d | d | d	 |	 d
 |
  
d  S(   Nt	   file_liket	   read_sizeR   R   R   R   RL   R   R   R   (   R   R1   R   (   R   R   R   R   R   R   R   RL   R   R   R   (    (    s0   lib/python2.7/site-packages/pandas/io/packers.pyR     s    N(   R   R   R@   R\   R   R/   R   (    (    (    s0   lib/python2.7/site-packages/pandas/io/packers.pyR1     s   		RV   c           B   s    e  Z d  Z d   Z d   Z RS(   sF    manage the unpacking iteration,
        close the file on completion c         K   s   | |  _  | |  _ d  S(   N(   RX   R=   (   R   RX   R=   (    (    s0   lib/python2.7/site-packages/pandas/io/packers.pyR     s    	c         c   s   t  } z t |  j t j  r y t j j |  j  } Wn t k
 rP t } n X| rl t	 |  j d  } q t j
 |  j  } n6 t |  j d  s t j
 |  j  } n t } |  j } t |  } x | D] } | Vq WWd  | r | j   n  Xd  S(   NRU   RT   (   R   RB   RX   RC   RD   RW   RY   RZ   R\   RE   RF   R`   RN   RP   (   R   t   needs_closingt   path_existsR:   t   unpackerR   (    (    s0   lib/python2.7/site-packages/pandas/io/packers.pyt   __iter__  s&    
	(   R   R   t   __doc__R   R  (    (    (    s0   lib/python2.7/site-packages/pandas/io/packers.pyRV     s   	(f   R  R   R    R   RW   t   textwrapR   R   t   dateutil.parserR   t   numpyRh   t   pandas.compatRC   R   R   t   pandas.errorsR   t   pandas.util._moveR   R   R	   R   t   pandas.core.dtypes.commonR
   R   R   R   R   t   pandasR   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R    t   pandas.coreR!   t   pandas.core.arraysR"   R#   R$   t   pandas.core.arrays.sparseR%   R&   t   pandas.core.genericR'   t   pandas.core.internalsR(   R)   R*   t   pandas.core.sparse.apiR+   R,   t   pandas.io.commonR-   R.   t   pandas.io.msgpackR/   R0   t   _PackerR1   t	   _UnpackerRy   R2   R3   Rz   R4   R@   RA   RK   R\   Rd   R|   Rg   Rl   t   float64t   float32Rr   R`   Rp   Rw   R   R   R   R   R9   RN   R   RV   (    (    (    s0   lib/python2.7/site-packages/pandas/io/packers.pyt   <module>'   s   (v	,B
	
			):				