Python numpy.string() Examples

The following are 30 code examples of numpy.string(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module numpy , or try the search function .
Example #1
Source File: matstudio.py    From VASPy with MIT License 6 votes vote down vote up
def get_bases(self):
        "get bases from SpaceGroup element"
        # lattice parameters
        bases = []
        for elem in self.tree.iter():
            if elem.tag == 'SpaceGroup':
                for attr in ['AVector', 'BVector', 'CVector']:
                    basis = elem.attrib[attr]  # string
                    basis = [float(i.strip()) for i in basis.split(',')]
                    bases.append(basis)
                break
        bases = np.array(bases)
        #set base constant as 1.0
        self.bases_const = 1.0

        return bases 
Example #2
Source File: dtypes.py    From lambda-packs with MIT License 6 votes vote down vote up
def max(self):
    """Returns the maximum representable value in this data type.

    Raises:
      TypeError: if this is a non-numeric, unordered, or quantized type.

    """
    if (self.is_quantized or self.base_dtype in
        (bool, string, complex64, complex128)):
      raise TypeError("Cannot find maximum value of %s." % self)

    # there is no simple way to get the max value of a dtype, we have to check
    # float and int types separately
    try:
      return np.finfo(self.as_numpy_dtype()).max
    except:  # bare except as possible raises by finfo not documented
      try:
        return np.iinfo(self.as_numpy_dtype()).max
      except:
        raise TypeError("Cannot find maximum value of %s." % self) 
Example #3
Source File: dtypes.py    From lambda-packs with MIT License 6 votes vote down vote up
def min(self):
    """Returns the minimum representable value in this data type.

    Raises:
      TypeError: if this is a non-numeric, unordered, or quantized type.

    """
    if (self.is_quantized or self.base_dtype in
        (bool, string, complex64, complex128)):
      raise TypeError("Cannot find minimum value of %s." % self)

    # there is no simple way to get the min value of a dtype, we have to check
    # float and int types separately
    try:
      return np.finfo(self.as_numpy_dtype()).min
    except:  # bare except as possible raises by finfo not documented
      try:
        return np.iinfo(self.as_numpy_dtype()).min
      except:
        raise TypeError("Cannot find minimum value of %s." % self) 
Example #4
Source File: pytables.py    From recruit with Apache License 2.0 6 votes vote down vote up
def generate(self, where):
        """ where can be a : dict,list,tuple,string """
        if where is None:
            return None

        q = self.table.queryables()
        try:
            return Expr(where, queryables=q, encoding=self.table.encoding)
        except NameError:
            # raise a nice message, suggesting that the user should use
            # data_columns
            raise ValueError(
                "The passed where expression: {0}\n"
                "            contains an invalid variable reference\n"
                "            all of the variable references must be a "
                "reference to\n"
                "            an axis (e.g. 'index' or 'columns'), or a "
                "data_column\n"
                "            The currently defined references are: {1}\n"
                .format(where, ','.join(q.keys()))
            ) 
Example #5
Source File: pytables.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def validate_col(self, itemsize=None):
        """ validate this column: return the compared against itemsize """

        # validate this column for string truncation (or reset to the max size)
        if _ensure_decoded(self.kind) == u'string':
            c = self.col
            if c is not None:
                if itemsize is None:
                    itemsize = self.itemsize
                if c.itemsize < itemsize:
                    raise ValueError(
                        "Trying to store a string with len [{itemsize}] in "
                        "[{cname}] column but\nthis column has a limit of "
                        "[{c_itemsize}]!\nConsider using min_itemsize to "
                        "preset the sizes on these columns".format(
                            itemsize=itemsize, cname=self.cname,
                            c_itemsize=c.itemsize))
                return c.itemsize

        return None 
Example #6
Source File: dtypes.py    From auto-alt-text-lambda-api with MIT License 6 votes vote down vote up
def min(self):
    """Returns the minimum representable value in this data type.

    Raises:
      TypeError: if this is a non-numeric, unordered, or quantized type.

    """
    if (self.is_quantized or self.base_dtype in
        (bool, string, complex64, complex128)):
      raise TypeError("Cannot find minimum value of %s." % self)

    # there is no simple way to get the min value of a dtype, we have to check
    # float and int types separately
    try:
      return np.finfo(self.as_numpy_dtype()).min
    except:  # bare except as possible raises by finfo not documented
      try:
        return np.iinfo(self.as_numpy_dtype()).min
      except:
        raise TypeError("Cannot find minimum value of %s." % self) 
Example #7
Source File: dtypes.py    From auto-alt-text-lambda-api with MIT License 6 votes vote down vote up
def max(self):
    """Returns the maximum representable value in this data type.

    Raises:
      TypeError: if this is a non-numeric, unordered, or quantized type.

    """
    if (self.is_quantized or self.base_dtype in
        (bool, string, complex64, complex128)):
      raise TypeError("Cannot find maximum value of %s." % self)

    # there is no simple way to get the max value of a dtype, we have to check
    # float and int types separately
    try:
      return np.finfo(self.as_numpy_dtype()).max
    except:  # bare except as possible raises by finfo not documented
      try:
        return np.iinfo(self.as_numpy_dtype()).max
      except:
        raise TypeError("Cannot find maximum value of %s." % self) 
Example #8
Source File: dtypes.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def max(self):
    """Returns the maximum representable value in this data type.

    Raises:
      TypeError: if this is a non-numeric, unordered, or quantized type.

    """
    if (self.is_quantized or self.base_dtype in
        (bool, string, complex64, complex128)):
      raise TypeError("Cannot find maximum value of %s." % self)

    # there is no simple way to get the max value of a dtype, we have to check
    # float and int types separately
    try:
      return np.finfo(self.as_numpy_dtype()).max
    except:  # bare except as possible raises by finfo not documented
      try:
        return np.iinfo(self.as_numpy_dtype()).max
      except:
        raise TypeError("Cannot find maximum value of %s." % self) 
Example #9
Source File: dtypes.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def min(self):
    """Returns the minimum representable value in this data type.

    Raises:
      TypeError: if this is a non-numeric, unordered, or quantized type.

    """
    if (self.is_quantized or self.base_dtype in
        (bool, string, complex64, complex128)):
      raise TypeError("Cannot find minimum value of %s." % self)

    # there is no simple way to get the min value of a dtype, we have to check
    # float and int types separately
    try:
      return np.finfo(self.as_numpy_dtype()).min
    except:  # bare except as possible raises by finfo not documented
      try:
        return np.iinfo(self.as_numpy_dtype()).min
      except:
        raise TypeError("Cannot find minimum value of %s." % self) 
Example #10
Source File: pytables.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def generate(self, where):
        """ where can be a : dict,list,tuple,string """
        if where is None:
            return None

        q = self.table.queryables()
        try:
            return Expr(where, queryables=q, encoding=self.table.encoding)
        except NameError:
            # raise a nice message, suggesting that the user should use
            # data_columns
            raise ValueError(
                "The passed where expression: {0}\n"
                "            contains an invalid variable reference\n"
                "            all of the variable references must be a "
                "reference to\n"
                "            an axis (e.g. 'index' or 'columns'), or a "
                "data_column\n"
                "            The currently defined references are: {1}\n"
                .format(where, ','.join(q.keys()))
            ) 
Example #11
Source File: pytables.py    From recruit with Apache License 2.0 6 votes vote down vote up
def validate_col(self, itemsize=None):
        """ validate this column: return the compared against itemsize """

        # validate this column for string truncation (or reset to the max size)
        if _ensure_decoded(self.kind) == u'string':
            c = self.col
            if c is not None:
                if itemsize is None:
                    itemsize = self.itemsize
                if c.itemsize < itemsize:
                    raise ValueError(
                        "Trying to store a string with len [{itemsize}] in "
                        "[{cname}] column but\nthis column has a limit of "
                        "[{c_itemsize}]!\nConsider using min_itemsize to "
                        "preset the sizes on these columns".format(
                            itemsize=itemsize, cname=self.cname,
                            c_itemsize=c.itemsize))
                return c.itemsize

        return None 
Example #12
Source File: pytables.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def generate(self, where):
        """ where can be a : dict,list,tuple,string """
        if where is None:
            return None

        q = self.table.queryables()
        try:
            return Expr(where, queryables=q, encoding=self.table.encoding)
        except NameError:
            # raise a nice message, suggesting that the user should use
            # data_columns
            raise ValueError(
                "The passed where expression: {0}\n"
                "            contains an invalid variable reference\n"
                "            all of the variable references must be a "
                "reference to\n"
                "            an axis (e.g. 'index' or 'columns'), or a "
                "data_column\n"
                "            The currently defined references are: {1}\n"
                .format(where, ','.join(q.keys()))
            ) 
Example #13
Source File: dtypes.py    From tensorboard with Apache License 2.0 6 votes vote down vote up
def max(self):
        """Returns the maximum representable value in this data type.

        Raises:
          TypeError: if this is a non-numeric, unordered, or quantized type.
        """
        if self.is_quantized or self.base_dtype in (
            bool,
            string,
            complex64,
            complex128,
        ):
            raise TypeError("Cannot find maximum value of %s." % self)

        # there is no simple way to get the max value of a dtype, we have to check
        # float and int types separately
        try:
            return np.finfo(self.as_numpy_dtype).max
        except:  # bare except as possible raises by finfo not documented
            try:
                return np.iinfo(self.as_numpy_dtype).max
            except:
                if self.base_dtype == bfloat16:
                    return _np_bfloat16(float.fromhex("0x1.FEp127"))
                raise TypeError("Cannot find maximum value of %s." % self) 
Example #14
Source File: matstudio.py    From VASPy with MIT License 6 votes vote down vote up
def __init__(self, filename):
        """
        Create a Material Studio *.arc file class.

        Example:

        >>> a = ArcFile("00-05.arc")

        Class attributes descriptions
        ================================================================
         Attribute         Description
         ===============  ==============================================
         filename          string, name of arc file.
         coords_iterator   generator, yield Cartisan coordinates in
                           numpy array.
         lengths           list of float, lengths of lattice axes.
         angles            list of float, angles of lattice axes.
        ================  ==============================================
        """
        super(ArcFile, self).__init__(filename)

        # Set logger.
        self.__logger = logging.getLogger("vaspy.ArcFile") 
Example #15
Source File: pytables.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def validate_col(self, itemsize=None):
        """ validate this column: return the compared against itemsize """

        # validate this column for string truncation (or reset to the max size)
        if _ensure_decoded(self.kind) == u('string'):
            c = self.col
            if c is not None:
                if itemsize is None:
                    itemsize = self.itemsize
                if c.itemsize < itemsize:
                    raise ValueError(
                        "Trying to store a string with len [%s] in [%s] "
                        "column but\nthis column has a limit of [%s]!\n"
                        "Consider using min_itemsize to preset the sizes on "
                        "these columns" % (itemsize, self.cname, c.itemsize))
                return c.itemsize

        return None 
Example #16
Source File: atomco.py    From VASPy with MIT License 6 votes vote down vote up
def __init__(self, filename='XDATCAR'):
        """
        Class to generate XDATCAR objects.

        Example:

        >>> a = XdatCar()

        Class attributes descriptions
        =======================================================================
          Attribute      Description
          ============  =======================================================
          filename       string, name of the file the direct coordiante data
                         stored in
          bases_const    float, lattice bases constant
          bases          np.array, bases of POSCAR
          natom          int, the number of total atom number
          atom_types     list of strings, atom types
          tf             list of list, T&F info of atoms
          info_nline     int, line numbers of lattice info
          ============  =======================================================
        """
        AtomCo.__init__(self, filename)
        self.info_nline = 7  # line numbers of lattice info
        self.load() 
Example #17
Source File: dtypes.py    From tensorboard with Apache License 2.0 6 votes vote down vote up
def min(self):
        """Returns the minimum representable value in this data type.

        Raises:
          TypeError: if this is a non-numeric, unordered, or quantized type.
        """
        if self.is_quantized or self.base_dtype in (
            bool,
            string,
            complex64,
            complex128,
        ):
            raise TypeError("Cannot find minimum value of %s." % self)

        # there is no simple way to get the min value of a dtype, we have to check
        # float and int types separately
        try:
            return np.finfo(self.as_numpy_dtype).min
        except:  # bare except as possible raises by finfo not documented
            try:
                return np.iinfo(self.as_numpy_dtype).min
            except:
                if self.base_dtype == bfloat16:
                    return _np_bfloat16(float.fromhex("-0x1.FEp127"))
                raise TypeError("Cannot find minimum value of %s." % self) 
Example #18
Source File: pytables.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def _unconvert_string_array(data, nan_rep=None, encoding=None,
                            errors='strict'):
    """
    inverse of _convert_string_array

    Parameters
    ----------
    data : fixed length string dtyped array
    nan_rep : the storage repr of NaN, optional
    encoding : the encoding of the data, optional
    errors : handler for encoding errors, default 'strict'

    Returns
    -------
    an object array of the decoded data

    """
    shape = data.shape
    data = np.asarray(data.ravel(), dtype=object)

    # guard against a None encoding in PY3 (because of a legacy
    # where the passed encoding is actually None)
    encoding = _ensure_encoding(encoding)
    if encoding is not None and len(data):

        itemsize = libwriters.max_len_string_array(_ensure_object(data))
        if compat.PY3:
            dtype = "U{0}".format(itemsize)
        else:
            dtype = "S{0}".format(itemsize)

        if isinstance(data[0], compat.binary_type):
            data = Series(data).str.decode(encoding, errors=errors).values
        else:
            data = data.astype(dtype, copy=False).astype(object, copy=False)

    if nan_rep is None:
        nan_rep = 'nan'

    data = libwriters.string_array_replace_from_nan_rep(data, nan_rep)
    return data.reshape(shape) 
Example #19
Source File: pytables.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def _get_converter(kind, encoding, errors):
    kind = _ensure_decoded(kind)
    if kind == 'datetime64':
        return lambda x: np.asarray(x, dtype='M8[ns]')
    elif kind == 'datetime':
        return lambda x: to_datetime(x, cache=True).to_pydatetime()
    elif kind == 'string':
        return lambda x: _unconvert_string_array(x, encoding=encoding,
                                                 errors=errors)
    else:  # pragma: no cover
        raise ValueError('invalid kind %s' % kind) 
Example #20
Source File: pytables.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def _convert_string_array(data, encoding, errors, itemsize=None):
    """
    we take a string-like that is object dtype and coerce to a fixed size
    string type

    Parameters
    ----------
    data : a numpy array of object dtype
    encoding : None or string-encoding
    errors : handler for encoding errors
    itemsize : integer, optional, defaults to the max length of the strings

    Returns
    -------
    data in a fixed-length string dtype, encoded to bytes if needed
    """

    # encode if needed
    if encoding is not None and len(data):
        data = Series(data.ravel()).str.encode(
            encoding, errors).values.reshape(data.shape)

    # create the sized dtype
    if itemsize is None:
        ensured = _ensure_object(data.ravel())
        itemsize = libwriters.max_len_string_array(ensured)

    data = np.asarray(data, dtype="S%d" % itemsize)
    return data 
Example #21
Source File: pytables.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def _need_convert(kind):
    kind = _ensure_decoded(kind)
    if kind in (u('datetime'), u('datetime64'), u('string')):
        return True
    return False 
Example #22
Source File: dtypes.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def name(self):
    """Returns the string name for this `DType`."""
    return _TYPE_TO_STRING[self._type_enum] 
Example #23
Source File: tensor_util.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def GetNumpyAppendFn(dtype):
  # numpy dtype for strings are variable length. We can not compare
  # dtype with a single constant (np.string does not exist) to decide
  # dtype is a "string" type. We need to compare the dtype.type to be
  # sure it's a string type.
  if dtype.type == np.string_ or dtype.type == np.unicode_:
    if _FAST_TENSOR_UTIL_AVAILABLE:
      return fast_tensor_util.AppendObjectArrayToTensorProto
    else:
      return SlowAppendObjectArrayToTensorProto
  return GetFromNumpyDTypeDict(_NP_TO_APPEND_FN, dtype) 
Example #24
Source File: pytables.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def _unconvert_index(data, kind, encoding=None, errors='strict'):
    kind = _ensure_decoded(kind)
    if kind == u('datetime64'):
        index = DatetimeIndex(data)
    elif kind == u('timedelta64'):
        index = TimedeltaIndex(data)
    elif kind == u('datetime'):
        index = np.asarray([datetime.fromtimestamp(v) for v in data],
                           dtype=object)
    elif kind == u('date'):
        try:
            index = np.asarray(
                [date.fromordinal(v) for v in data], dtype=object)
        except (ValueError):
            index = np.asarray(
                [date.fromtimestamp(v) for v in data], dtype=object)
    elif kind in (u('integer'), u('float')):
        index = np.asarray(data)
    elif kind in (u('string')):
        index = _unconvert_string_array(data, nan_rep=None, encoding=encoding,
                                        errors=errors)
    elif kind == u('object'):
        index = np.asarray(data[0])
    else:  # pragma: no cover
        raise ValueError('unrecognized index type %s' % kind)
    return index 
Example #25
Source File: pytables.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def maybe_set_size(self, min_itemsize=None, **kwargs):
        """ maybe set a string col itemsize:
               min_itemsize can be an integer or a dict with this columns name
               with an integer size """
        if _ensure_decoded(self.kind) == u('string'):

            if isinstance(min_itemsize, dict):
                min_itemsize = min_itemsize.get(self.name)

            if min_itemsize is not None and self.typ.itemsize < min_itemsize:
                self.typ = _tables(
                ).StringCol(itemsize=min_itemsize, pos=self.pos) 
Example #26
Source File: pytables.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def _set_tz(values, tz, preserve_UTC=False, coerce=False):
    """
    coerce the values to a DatetimeIndex if tz is set
    preserve the input shape if possible

    Parameters
    ----------
    values : ndarray
    tz : string/pickled tz object
    preserve_UTC : boolean,
        preserve the UTC of the result
    coerce : if we do not have a passed timezone, coerce to M8[ns] ndarray
    """
    if tz is not None:
        name = getattr(values, 'name', None)
        values = values.ravel()
        tz = timezones.get_timezone(_ensure_decoded(tz))
        values = DatetimeIndex(values, name=name)
        if values.tz is None:
            values = values.tz_localize('UTC').tz_convert(tz)
        if preserve_UTC:
            if tz == 'UTC':
                values = list(values)
    elif coerce:
        values = np.asarray(values, dtype='M8[ns]')

    return values 
Example #27
Source File: dtypes.py    From tensorboard with Apache License 2.0 5 votes vote down vote up
def name(self):
        """Returns the string name for this `DType`."""
        return _TYPE_TO_STRING[self._type_enum] 
Example #28
Source File: tensor_util.py    From tensorboard with Apache License 2.0 5 votes vote down vote up
def GetNumpyAppendFn(dtype):
    # numpy dtype for strings are variable length. We can not compare
    # dtype with a single constant (np.string does not exist) to decide
    # dtype is a "string" type. We need to compare the dtype.type to be
    # sure it's a string type.
    if dtype.type == np.string_ or dtype.type == np.unicode_:
        return SlowAppendObjectArrayToTensorProto
    return GetFromNumpyDTypeDict(_NP_TO_APPEND_FN, dtype) 
Example #29
Source File: ops.py    From language with Apache License 2.0 5 votes vote down vote up
def _lowercase(x):
  # Specify `np.object` to avoid incorrectly returning a np.string type arr
  return np.array([w.lower() for w in x], np.object) 
Example #30
Source File: pytables.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def _need_convert(kind):
    kind = _ensure_decoded(kind)
    if kind in (u'datetime', u'datetime64', u'string'):
        return True
    return False