From 0a7c546fd676a0f94c0dc4dde6b73f2f09e542a5 Mon Sep 17 00:00:00 2001 From: Rushabh Vasani Date: Wed, 19 Feb 2020 22:44:10 +0530 Subject: [PATCH 1/4] add test --- pandas/tests/dtypes/test_dtypes.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index dd99b81fb6764..4dadfdc32d423 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -26,7 +26,7 @@ ) import pandas as pd -from pandas import Categorical, CategoricalIndex, IntervalIndex, Series, date_range +from pandas import Categorical, CategoricalIndex, DatetimeIndex, IntervalIndex, Series, date_range import pandas._testing as tm from pandas.core.arrays.sparse import SparseArray, SparseDtype @@ -172,6 +172,11 @@ def test_is_boolean(self, categories, expected): assert is_bool_dtype(cat) is expected assert is_bool_dtype(cat.dtype) is expected + def test_dtype_specific_categorical_dtype(self): + expected = 'datetime64[ns]' + result = str(Categorical(DatetimeIndex([])).categories.dtype) + assert result == expected + class TestDatetimeTZDtype(Base): @pytest.fixture From d6752231c13b133921848405eb19e7600136212e Mon Sep 17 00:00:00 2001 From: Rushabh Vasani Date: Wed, 19 Feb 2020 22:51:49 +0530 Subject: [PATCH 2/4] fix linting --- pandas/tests/dtypes/test_dtypes.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index 4dadfdc32d423..b5902fee17754 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -26,7 +26,15 @@ ) import pandas as pd -from pandas import Categorical, CategoricalIndex, DatetimeIndex, IntervalIndex, Series, date_range +from pandas import ( + Categorical, + CategoricalIndex, + DatetimeIndex, + IntervalIndex, + Series, + date_range, +) + import pandas._testing as tm from pandas.core.arrays.sparse import SparseArray, SparseDtype From 305415ec70496cf93086887b6149a90b112d1dd4 Mon Sep 17 00:00:00 2001 From: Rushabh Vasani Date: Thu, 20 Feb 2020 11:19:15 +0530 Subject: [PATCH 3/4] add docstring --- pandas/core/dtypes/dtypes.py | 8 ++++++++ pandas/tests/dtypes/test_dtypes.py | 1 - 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 8aaebe89871b6..70e677411b4a9 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -175,6 +175,8 @@ class CategoricalDtype(PandasExtensionDtype, ExtensionDtype): ---------- categories : sequence, optional Must be unique, and must not contain any nulls. + The categories are stored in an Index, + and if an index is provided the dtype of that index will be used. ordered : bool or None, default False Whether or not this categorical is treated as a ordered categorical. None can be used to maintain the ordered value of existing categoricals when @@ -210,6 +212,12 @@ class CategoricalDtype(PandasExtensionDtype, ExtensionDtype): 3 NaN dtype: category Categories (2, object): [b < a] + + An empty CategoricalDtype with a specific dtype can be created + by providing an empty index. As follows, + + >>> pd.CategoricalDtype(pd.DatetimeIndex([])).categories.dtype + dtype(' Date: Thu, 20 Feb 2020 14:34:12 +0530 Subject: [PATCH 4/4] run black --- pandas/tests/dtypes/test_dtypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index 106b2b4434fb6..645912b2a2c21 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -180,7 +180,7 @@ def test_is_boolean(self, categories, expected): assert is_bool_dtype(cat.dtype) is expected def test_dtype_specific_categorical_dtype(self): - expected = 'datetime64[ns]' + expected = "datetime64[ns]" result = str(Categorical(DatetimeIndex([])).categories.dtype) assert result == expected