MCPcopy Create free account
hub / github.com/numpy/numpy / StringConverter

Class StringConverter

numpy/lib/_iotools.py:450–819  ·  view source on GitHub ↗

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. Attribute

Source from the content-addressed store, hash-verified

448
449
450class StringConverter:
451 """
452 Factory class for function transforming a string into another object
453 (int, float).
454
455 After initialization, an instance can be called to transform a string
456 into another object. If the string is recognized as representing a
457 missing value, a default value is returned.
458
459 Attributes
460 ----------
461 func : function
462 Function used for the conversion.
463 default : any
464 Default value to return when the input corresponds to a missing
465 value.
466 type : type
467 Type of the output.
468 _status : int
469 Integer representing the order of the conversion.
470 _mapper : sequence of tuples
471 Sequence of tuples (dtype, function, default value) to evaluate in
472 order.
473 _locked : bool
474 Holds `locked` parameter.
475
476 Parameters
477 ----------
478 dtype_or_func : {None, dtype, function}, optional
479 If a `dtype`, specifies the input data type, used to define a basic
480 function and a default value for missing data. For example, when
481 `dtype` is float, the `func` attribute is set to `float` and the
482 default value to `np.nan`. If a function, this function is used to
483 convert a string to another object. In this case, it is recommended
484 to give an associated default value as input.
485 default : any, optional
486 Value to return by default, that is, when the string to be
487 converted is flagged as missing. If not given, `StringConverter`
488 tries to supply a reasonable default value.
489 missing_values : {None, sequence of str}, optional
490 ``None`` or sequence of strings indicating a missing value. If ``None``
491 then missing values are indicated by empty entries. The default is
492 ``None``.
493 locked : bool, optional
494 Whether the StringConverter should be locked to prevent automatic
495 upgrade or not. Default is False.
496
497 """
498 _mapper = [(nx.bool_, str2bool, False),
499 (nx.int_, int, -1),]
500
501 # On 32-bit systems, we need to make sure that we explicitly include
502 # nx.int64 since ns.int_ is nx.int32.
503 if nx.dtype(nx.int_).itemsize < nx.dtype(nx.int64).itemsize:
504 _mapper.append((nx.int64, int, -1))
505
506 _mapper.extend([(nx.float64, float, nx.nan),
507 (nx.complex128, complex, nx.nan + 0j),

Callers 11

test_creationMethod · 0.90
test_upgradeMethod · 0.90
test_missingMethod · 0.90
test_upgrademapperMethod · 0.90
test_string_to_objectMethod · 0.90
test_keep_defaultMethod · 0.90
test_int64_dtypeMethod · 0.90
test_uint64_dtypeMethod · 0.90
genfromtxtFunction · 0.85

Calls 1

dtypeMethod · 0.45

Tested by 10

test_creationMethod · 0.72
test_upgradeMethod · 0.72
test_missingMethod · 0.72
test_upgrademapperMethod · 0.72
test_string_to_objectMethod · 0.72
test_keep_defaultMethod · 0.72
test_int64_dtypeMethod · 0.72
test_uint64_dtypeMethod · 0.72