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


Python dtypes.CategoricalDtype方法代码示例

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


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

示例1: test_astype_category

# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import CategoricalDtype [as 别名]
def test_astype_category(self, dtype_ordered, cat_ordered):
        # GH 10696/18593
        data = list('abcaacbab')
        cat = Categorical(data, categories=list('bac'), ordered=cat_ordered)

        # standard categories
        dtype = CategoricalDtype(ordered=dtype_ordered)
        result = cat.astype(dtype)
        expected = Categorical(
            data, categories=cat.categories, ordered=dtype_ordered)
        tm.assert_categorical_equal(result, expected)

        # non-standard categories
        dtype = CategoricalDtype(list('adc'), dtype_ordered)
        result = cat.astype(dtype)
        expected = Categorical(data, dtype=dtype)
        tm.assert_categorical_equal(result, expected)

        if dtype_ordered is False:
            # dtype='category' can't specify ordered, so only test once
            result = cat.astype('category')
            expected = cat
            tm.assert_categorical_equal(result, expected) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:25,代码来源:test_dtypes.py

示例2: test_astype_category

# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import CategoricalDtype [as 别名]
def test_astype_category(self, copy, name, ordered):
        # GH 18630
        index = self.create_index()
        if name:
            index = index.rename(name)

        # standard categories
        dtype = CategoricalDtype(ordered=ordered)
        result = index.astype(dtype, copy=copy)
        expected = CategoricalIndex(index.values, name=name, ordered=ordered)
        tm.assert_index_equal(result, expected)

        # non-standard categories
        dtype = CategoricalDtype(index.unique().tolist()[:-1], ordered)
        result = index.astype(dtype, copy=copy)
        expected = CategoricalIndex(index.values, name=name, dtype=dtype)
        tm.assert_index_equal(result, expected)

        if ordered is False:
            # dtype='category' defaults to ordered=False, so only test once
            result = index.astype('category', copy=copy)
            expected = CategoricalIndex(index.values, name=name)
            tm.assert_index_equal(result, expected) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:25,代码来源:common.py

示例3: test_categorical_index_preserver

# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import CategoricalDtype [as 别名]
def test_categorical_index_preserver(self):

        a = Series(np.arange(6, dtype='int64'))
        b = Series(list('aabbca'))

        df2 = DataFrame({'A': a,
                         'B': b.astype(CategoricalDtype(list('cab')))
                         }).set_index('B')
        result = pd.concat([df2, df2])
        expected = DataFrame(
            {'A': pd.concat([a, a]),
             'B': pd.concat([b, b]).astype(CategoricalDtype(list('cab')))
             }).set_index('B')
        tm.assert_frame_equal(result, expected)

        # wrong catgories
        df3 = DataFrame({'A': a, 'B': Categorical(b, categories=list('abe'))
                         }).set_index('B')
        msg = "categories must match existing categories when appending"
        with pytest.raises(TypeError, match=msg):
            pd.concat([df2, df3]) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:23,代码来源:test_concat.py

示例4: test_getitem_category_type

# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import CategoricalDtype [as 别名]
def test_getitem_category_type(self):
        # GH 14580
        # test iloc() on Series with Categorical data

        s = Series([1, 2, 3]).astype('category')

        # get slice
        result = s.iloc[0:2]
        expected = Series([1, 2]).astype(CategoricalDtype([1, 2, 3]))
        tm.assert_series_equal(result, expected)

        # get list of indexes
        result = s.iloc[[0, 1]]
        expected = Series([1, 2]).astype(CategoricalDtype([1, 2, 3]))
        tm.assert_series_equal(result, expected)

        # get boolean array
        result = s.iloc[[True, False, False]]
        expected = Series([1]).astype(CategoricalDtype([1, 2, 3]))
        tm.assert_series_equal(result, expected) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:22,代码来源:test_categorical.py

示例5: test_categorical_dtype_chunksize_explicit_categories

# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import CategoricalDtype [as 别名]
def test_categorical_dtype_chunksize_explicit_categories(all_parsers):
    # see gh-10153
    parser = all_parsers
    data = """a,b
1,a
1,b
1,b
2,c"""
    cats = ["a", "b", "c"]
    expecteds = [DataFrame({"a": [1, 1],
                            "b": Categorical(["a", "b"],
                                             categories=cats)}),
                 DataFrame({"a": [1, 2],
                            "b": Categorical(["b", "c"],
                                             categories=cats)},
                           index=[2, 3])]
    dtype = CategoricalDtype(cats)
    actuals = parser.read_csv(StringIO(data), dtype={"b": dtype}, chunksize=2)

    for actual, expected in zip(actuals, expecteds):
        tm.assert_frame_equal(actual, expected) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:23,代码来源:test_dtypes.py

示例6: test_categorical_category_dtype

# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import CategoricalDtype [as 别名]
def test_categorical_category_dtype(all_parsers, categories, ordered):
    parser = all_parsers
    data = """a,b
1,a
1,b
1,b
2,c"""
    expected = DataFrame({
        "a": [1, 1, 1, 2],
        "b": Categorical(["a", "b", "b", "c"],
                         categories=categories,
                         ordered=ordered)
    })

    dtype = {"b": CategoricalDtype(categories=categories,
                                   ordered=ordered)}
    result = parser.read_csv(StringIO(data), dtype=dtype)
    tm.assert_frame_equal(result, expected) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:20,代码来源:test_dtypes.py

示例7: test_categorical_index_preserver

# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import CategoricalDtype [as 别名]
def test_categorical_index_preserver(self):

        a = Series(np.arange(6, dtype='int64'))
        b = Series(list('aabbca'))

        df2 = DataFrame({'A': a,
                         'B': b.astype(CategoricalDtype(list('cab')))
                         }).set_index('B')
        result = pd.concat([df2, df2])
        expected = DataFrame(
            {'A': pd.concat([a, a]),
             'B': pd.concat([b, b]).astype(CategoricalDtype(list('cab')))
             }).set_index('B')
        tm.assert_frame_equal(result, expected)

        # wrong catgories
        df3 = DataFrame({'A': a, 'B': Categorical(b, categories=list('abe'))
                         }).set_index('B')
        pytest.raises(TypeError, lambda: pd.concat([df2, df3])) 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:21,代码来源:test_concat.py

示例8: test_categorical_categoricaldtype

# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import CategoricalDtype [as 别名]
def test_categorical_categoricaldtype(self, categories, ordered):
        data = """a,b
1,a
1,b
1,b
2,c"""
        expected = pd.DataFrame({
            "a": [1, 1, 1, 2],
            "b": Categorical(['a', 'b', 'b', 'c'],
                             categories=categories,
                             ordered=ordered)
        })
        dtype = {"b": CategoricalDtype(categories=categories,
                                       ordered=ordered)}
        result = self.read_csv(StringIO(data), dtype=dtype)
        tm.assert_frame_equal(result, expected) 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:18,代码来源:dtypes.py

示例9: test_categoricaldtype_coerces_datetime

# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import CategoricalDtype [as 别名]
def test_categoricaldtype_coerces_datetime(self):
        dtype = {
            'b': CategoricalDtype(pd.date_range('2017', '2019', freq='AS'))
        }
        data = "b\n2017-01-01\n2018-01-01\n2019-01-01"
        expected = pd.DataFrame({'b': Categorical(dtype['b'].categories)})
        result = self.read_csv(StringIO(data), dtype=dtype)
        tm.assert_frame_equal(result, expected)

        dtype = {
            'b': CategoricalDtype([pd.Timestamp("2014")])
        }
        data = "b\n2014-01-01\n2014-01-01T00:00:00"
        expected = pd.DataFrame({'b': Categorical([pd.Timestamp('2014')] * 2)})
        result = self.read_csv(StringIO(data), dtype=dtype)
        tm.assert_frame_equal(result, expected) 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:18,代码来源:dtypes.py

示例10: test_categorical_categoricaldtype_chunksize

# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import CategoricalDtype [as 别名]
def test_categorical_categoricaldtype_chunksize(self):
        # GH 10153
        data = """a,b
1,a
1,b
1,b
2,c"""
        cats = ['a', 'b', 'c']
        expecteds = [pd.DataFrame({'a': [1, 1],
                                   'b': Categorical(['a', 'b'],
                                                    categories=cats)}),
                     pd.DataFrame({'a': [1, 2],
                                   'b': Categorical(['b', 'c'],
                                                    categories=cats)},
                                  index=[2, 3])]
        dtype = CategoricalDtype(cats)
        actuals = self.read_csv(StringIO(data), dtype={'b': dtype},
                                chunksize=2)

        for actual, expected in zip(actuals, expecteds):
            tm.assert_frame_equal(actual, expected) 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:23,代码来源:dtypes.py

示例11: test_set_dtype_nans

# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import CategoricalDtype [as 别名]
def test_set_dtype_nans(self):
        c = Categorical(['a', 'b', np.nan])
        result = c._set_dtype(CategoricalDtype(['a', 'c']))
        tm.assert_numpy_array_equal(result.codes, np.array([0, -1, -1],
                                                           dtype='int8')) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:7,代码来源:test_missing.py

示例12: test_set_dtype_same

# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import CategoricalDtype [as 别名]
def test_set_dtype_same(self):
        c = Categorical(['a', 'b', 'c'])
        result = c._set_dtype(CategoricalDtype(['a', 'b', 'c']))
        tm.assert_categorical_equal(result, c) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:6,代码来源:test_dtypes.py

示例13: test_set_dtype_new_categories

# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import CategoricalDtype [as 别名]
def test_set_dtype_new_categories(self):
        c = Categorical(['a', 'b', 'c'])
        result = c._set_dtype(CategoricalDtype(list('abcd')))
        tm.assert_numpy_array_equal(result.codes, c.codes)
        tm.assert_index_equal(result.dtype.categories, Index(list('abcd'))) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:7,代码来源:test_dtypes.py

示例14: test_set_dtype_no_overlap

# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import CategoricalDtype [as 别名]
def test_set_dtype_no_overlap(self):
        c = Categorical(['a', 'b', 'c'], ['d', 'e'])
        result = c._set_dtype(CategoricalDtype(['a', 'b']))
        expected = Categorical([None, None, None], categories=['a', 'b'])
        tm.assert_categorical_equal(result, expected) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:7,代码来源:test_dtypes.py

示例15: test_constructor_dtype_and_others_raises

# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import CategoricalDtype [as 别名]
def test_constructor_dtype_and_others_raises(self):
        dtype = CategoricalDtype(['a', 'b'], ordered=True)
        msg = "Cannot specify `categories` or `ordered` together with `dtype`."
        with pytest.raises(ValueError, match=msg):
            Categorical(['a', 'b'], categories=['a', 'b'], dtype=dtype)

        with pytest.raises(ValueError, match=msg):
            Categorical(['a', 'b'], ordered=True, dtype=dtype)

        with pytest.raises(ValueError, match=msg):
            Categorical(['a', 'b'], ordered=False, dtype=dtype) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:13,代码来源:test_constructors.py


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