class documentation

class StringConverter:

View In Hierarchy

Factory class for function transforming a string into another object (int, float).

After initialization, an instance can be called to transform a string into another object. If the string is recognized as representing a missing value, a default value is returned.

Attributes

func : function
Function used for the conversion.
default : any
Default value to return when the input corresponds to a missing value.
type : type
Type of the output.
_status : int
Integer representing the order of the conversion.
_mapper : sequence of tuples
Sequence of tuples (dtype, function, default value) to evaluate in order.
_locked : bool
Holds locked parameter.

Parameters

dtype_or_func : {None, dtype, function}, optional
If a dtype, specifies the input data type, used to define a basic function and a default value for missing data. For example, when dtype is float, the func attribute is set to float and the default value to np.nan. If a function, this function is used to convert a string to another object. In this case, it is recommended to give an associated default value as input.
default : any, optional
Value to return by default, that is, when the string to be converted is flagged as missing. If not given, StringConverter tries to supply a reasonable default value.
missing_values : {None, sequence of str}, optional
None or sequence of strings indicating a missing value. If None then missing values are indicated by empty entries. The default is None.
locked : bool, optional
Whether the StringConverter should be locked to prevent automatic upgrade or not. Default is False.
Class Method upgrade​_mapper Upgrade the mapper of a StringConverter by adding a new function and its corresponding default.
Class Method ​_dtypeortype Returns dtype for datetime64 and type of dtype otherwise.
Class Method ​_find​_map​_entry Undocumented
Class Method ​_getdtype Returns the dtype of the input variable.
Class Method ​_getsubdtype Returns the type of the dtype of the input variable.
Method __call__ Undocumented
Method __init__ Undocumented
Method ​_do​_upgrade Undocumented
Method ​_loose​_call Undocumented
Method ​_strict​_call Undocumented
Method iterupgrade Undocumented
Method update Set StringConverter attributes directly.
Method upgrade Find the best converter for a given string, and return the result.
Class Variable ​_mapper Undocumented
Instance Variable ​_callingfunction Undocumented
Instance Variable ​_checked Undocumented
Instance Variable ​_initial​_default Undocumented
Instance Variable ​_locked Undocumented
Instance Variable ​_status Undocumented
Instance Variable default Undocumented
Instance Variable func Undocumented
Instance Variable missing​_values Undocumented
Instance Variable type Undocumented
@classmethod
def upgrade_mapper(cls, func, default=None):

Upgrade the mapper of a StringConverter by adding a new function and its corresponding default.

The input function (or sequence of functions) and its associated default value (if any) is inserted in penultimate position of the mapper. The corresponding type is estimated from the dtype of the default value.

Parameters

func : var
Function, or sequence of functions

Examples

>>> import dateutil.parser
>>> import datetime
>>> dateparser = dateutil.parser.parse
>>> defaultdate = datetime.date(2000, 1, 1)
>>> StringConverter.upgrade_mapper(dateparser, default=defaultdate)
@classmethod
def _dtypeortype(cls, dtype):
Returns dtype for datetime64 and type of dtype otherwise.
@classmethod
def _find_map_entry(cls, dtype):

Undocumented

@classmethod
def _getdtype(cls, val):
Returns the dtype of the input variable.
@classmethod
def _getsubdtype(cls, val):
Returns the type of the dtype of the input variable.
def __call__(self, value):

Undocumented

def __init__(self, dtype_or_func=None, default=None, missing_values=None, locked=False):

Undocumented

def _do_upgrade(self):

Undocumented

def _loose_call(self, value):

Undocumented

def _strict_call(self, value):

Undocumented

def iterupgrade(self, value):

Undocumented

def update(self, func, default=None, testing_value=None, missing_values='', locked=False):

Set StringConverter attributes directly.

Parameters

func : function
Conversion function.
default : any, optional
Value to return by default, that is, when the string to be converted is flagged as missing. If not given, StringConverter tries to supply a reasonable default value.
testing_value : str, optional
A string representing a standard input value of the converter. This string is used to help defining a reasonable default value.
missing_values : {sequence of str, None}, optional
Sequence of strings indicating a missing value. If None, then the existing missing_values are cleared. The default is ''.
locked : bool, optional
Whether the StringConverter should be locked to prevent automatic upgrade or not. Default is False.

Notes

update takes the same parameters as the constructor of StringConverter, except that func does not accept a dtype whereas dtype_or_func in the constructor does.

def upgrade(self, value):

Find the best converter for a given string, and return the result.

The supplied string value is converted by testing different converters in order. First the func method of the StringConverter instance is tried, if this fails other available converters are tried. The order in which these other converters are tried is determined by the _status attribute of the instance.

Parameters

value : str
The string to convert.

Returns

out : any
The result of converting value with the appropriate converter.
_mapper =

Undocumented

_callingfunction =

Undocumented

_checked: bool =

Undocumented

_initial_default =

Undocumented

_locked =

Undocumented

_status =

Undocumented

default =

Undocumented

func =

Undocumented

missing_values: set =

Undocumented

type =

Undocumented