<s> from __future__ import absolute_import , division , print_function <EOL> from datetime import datetime , timedelta <EOL> from functools import partial <EOL> from datashape import discover , Categorical <EOL> from datashape import string , object_ , datetime_ , Option <EOL> import datashape <EOL> import pandas as pd <EOL> import numpy as np <EOL> from . . convert import convert <EOL> possibly_missing = frozenset ( { string , datetime_ } ) <EOL> categorical = type ( pd . Categorical . dtype ) <EOL> def dshape_from_pandas ( col ) : <EOL> if isinstance ( col . dtype , categorical ) : <EOL> return Categorical ( col . cat . categories . tolist ( ) ) <EOL> dshape = datashape . CType . from_numpy_dtype ( col . dtype ) <EOL> dshape = string if dshape == object_ else dshape <EOL> return Option ( dshape ) if dshape in possibly_missing else dshape <EOL> @ discover . register ( pd . DataFrame ) <EOL> def discover_dataframe ( df ) : <EOL> return len ( df ) * datashape . Record ( [ ( k , dshape_from_pandas ( df [ k ] ) ) <EOL> for k in df . columns ] ) <EOL> @ discover . register ( pd . Series ) <EOL> def discover_series ( s ) : <EOL> return len ( s ) * dshape_from_pandas ( s ) <EOL> def coerce_datetimes ( df ) : <EOL> """<STR_LIT>""" <EOL> objects = df . select_dtypes ( include = [ '<STR_LIT:object>' ] ) <EOL> columns = [ <EOL> c for c in objects . columns <EOL> if not np . any ( objects [ c ] . str . isspace ( ) | objects [ c ] . str . isalpha ( ) ) <EOL> ] <EOL> df2 = objects [ columns ] . apply ( partial ( pd . to_datetime , errors = '<STR_LIT:ignore>' ) ) <EOL> for c in df2 . columns : <EOL> df [ c ] = df2 [ c ] <EOL> return df <EOL> @ convert . register ( pd . Timestamp , datetime ) <EOL> def convert_datetime_to_timestamp ( dt , ** kwargs ) : <EOL> return pd . Timestamp ( dt ) <EOL> @ convert . register ( ( pd . Timestamp , pd . Timedelta ) , float ) <EOL> def nan_to_nat ( fl , ** kwargs ) : <EOL> try : <EOL> if np . isnan ( fl ) : <EOL> return pd . NaT <EOL> except TypeError : <EOL> pass <EOL> raise NotImplementedError ( ) <EOL> @ convert . register ( ( pd . Timestamp , pd . Timedelta ) , ( pd . tslib . NaTType , type ( None ) ) ) <EOL> def convert_null_or_nat_to_nat ( n , ** kwargs ) : <EOL> return pd . NaT <EOL> @ convert . register ( pd . Timedelta , timedelta ) <EOL> def convert_timedelta_to_pd_timedelta ( dt , ** kwargs ) : <EOL> if dt is None : <EOL> return pd . NaT <EOL> return pd . Timedelta ( dt ) </s>
