当前位置: 首页>>代码示例>>Python>>正文


Python cast.astype_nansafe方法代码示例

本文整理汇总了Python中pandas.core.dtypes.cast.astype_nansafe方法的典型用法代码示例。如果您正苦于以下问题:Python cast.astype_nansafe方法的具体用法?Python cast.astype_nansafe怎么用?Python cast.astype_nansafe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pandas.core.dtypes.cast的用法示例。


在下文中一共展示了cast.astype_nansafe方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: astype

# 需要导入模块: from pandas.core.dtypes import cast [as 别名]
# 或者: from pandas.core.dtypes.cast import astype_nansafe [as 别名]
def astype(self, dtype, copy=True):
        """
        Cast to a NumPy array or IntegerArray with 'dtype'.

        Parameters
        ----------
        dtype : str or dtype
            Typecode or data-type to which the array is cast.
        copy : bool, default True
            Whether to copy the data, even if not necessary. If False,
            a copy is made only if the old dtype does not match the
            new dtype.

        Returns
        -------
        array : ndarray or IntegerArray
            NumPy ndarray or IntergerArray with 'dtype' for its dtype.

        Raises
        ------
        TypeError
            if incompatible type with an IntegerDtype, equivalent of same_kind
            casting
        """

        # if we are astyping to an existing IntegerDtype we can fastpath
        if isinstance(dtype, _IntegerDtype):
            result = self._data.astype(dtype.numpy_dtype, copy=False)
            return type(self)(result, mask=self._mask, copy=False)

        # coerce
        data = self._coerce_to_ndarray()
        return astype_nansafe(data, dtype, copy=None) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:35,代码来源:integer.py

示例2: _cast_types

# 需要导入模块: from pandas.core.dtypes import cast [as 别名]
# 或者: from pandas.core.dtypes.cast import astype_nansafe [as 别名]
def _cast_types(self, values, cast_type, column):
        """
        Cast values to specified type

        Parameters
        ----------
        values : ndarray
        cast_type : string or np.dtype
           dtype to cast values to
        column : string
            column name - used only for error reporting

        Returns
        -------
        converted : ndarray
        """

        if is_categorical_dtype(cast_type):
            known_cats = (isinstance(cast_type, CategoricalDtype) and
                          cast_type.categories is not None)

            if not is_object_dtype(values) and not known_cats:
                # XXX this is for consistency with
                # c-parser which parses all categories
                # as strings
                values = astype_nansafe(values, str)

            cats = Index(values).unique().dropna()
            values = Categorical._from_inferred_categories(
                cats, cats.get_indexer(values), cast_type
            )

        else:
            try:
                values = astype_nansafe(values, cast_type, copy=True)
            except ValueError:
                raise ValueError("Unable to convert column %s to "
                                 "type %s" % (column, cast_type))
        return values 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:41,代码来源:parsers.py

示例3: _cast_types

# 需要导入模块: from pandas.core.dtypes import cast [as 别名]
# 或者: from pandas.core.dtypes.cast import astype_nansafe [as 别名]
def _cast_types(self, values, cast_type, column):
        """
        Cast values to specified type

        Parameters
        ----------
        values : ndarray
        cast_type : string or np.dtype
           dtype to cast values to
        column : string
            column name - used only for error reporting

        Returns
        -------
        converted : ndarray
        """

        if is_categorical_dtype(cast_type):
            known_cats = (isinstance(cast_type, CategoricalDtype) and
                          cast_type.categories is not None)

            if not is_object_dtype(values) and not known_cats:
                # XXX this is for consistency with
                # c-parser which parses all categories
                # as strings
                values = astype_nansafe(values, str)

            cats = Index(values).unique().dropna()
            values = Categorical._from_inferred_categories(
                cats, cats.get_indexer(values), cast_type,
                true_values=self.true_values)

        # use the EA's implementation of casting
        elif is_extension_array_dtype(cast_type):
            # ensure cast_type is an actual dtype and not a string
            cast_type = pandas_dtype(cast_type)
            array_type = cast_type.construct_array_type()
            try:
                return array_type._from_sequence_of_strings(values,
                                                            dtype=cast_type)
            except NotImplementedError:
                raise NotImplementedError(
                    "Extension Array: {ea} must implement "
                    "_from_sequence_of_strings in order "
                    "to be used in parser methods".format(ea=array_type))

        else:
            try:
                values = astype_nansafe(values, cast_type,
                                        copy=True, skipna=True)
            except ValueError:
                raise ValueError("Unable to convert column %s to "
                                 "type %s" % (column, cast_type))
        return values 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:56,代码来源:parsers.py


注:本文中的pandas.core.dtypes.cast.astype_nansafe方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。