Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let _get_dtype accept Categoricals and CategoricalIndex #16887

Merged
merged 4 commits into from
Jul 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ Conversion
^^^^^^^^^^



Indexing
^^^^^^^^

Expand Down
4 changes: 3 additions & 1 deletion pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
ExtensionDtype)
from .generic import (ABCCategorical, ABCPeriodIndex,
ABCDatetimeIndex, ABCSeries,
ABCSparseArray, ABCSparseSeries)
ABCSparseArray, ABCSparseSeries, ABCCategoricalIndex)
from .inference import is_string_like
from .inference import * # noqa

Expand Down Expand Up @@ -1713,6 +1713,8 @@ def _get_dtype(arr_or_dtype):
return PeriodDtype.construct_from_string(arr_or_dtype)
elif is_interval_dtype(arr_or_dtype):
return IntervalDtype.construct_from_string(arr_or_dtype)
elif isinstance(arr_or_dtype, (ABCCategorical, ABCCategoricalIndex)):
return arr_or_dtype.dtype

if hasattr(arr_or_dtype, 'dtype'):
arr_or_dtype = arr_or_dtype.dtype
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,16 +532,16 @@ def test_is_complex_dtype():
(float, np.dtype(float)),
('float64', np.dtype('float64')),
(np.dtype('float64'), np.dtype('float64')),
pytest.mark.xfail((str, np.dtype('<U')), ),
(str, np.dtype(str)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what fixed this one?

Copy link
Contributor Author

@topper-123 topper-123 Jul 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On both my system (Windows) and at try.jupyter.org (sys.platform is 'linux'), _get_dtype(str) returns np.dtype('<U'). In the jenkins/appvoyeur, it returns np.dtype('S').

To me it looks like a difference of how numpy is handled on different system and not a pandas issue. I therefore relax that test, to make it only check that _get_dtype(str) == np.dtype(str), which passes both locally and in the CI system.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, that looks like a py2/py3 thing. ok then. (you can maybe add a comment for that particular case)

(pd.Series([1, 2], dtype=np.dtype('int16')), np.dtype('int16')),
(pd.Series(['a', 'b']), np.dtype(object)),
(pd.Index([1, 2]), np.dtype('int64')),
(pd.Index(['a', 'b']), np.dtype(object)),
('category', 'category'),
(pd.Categorical(['a', 'b']).dtype, CategoricalDtype()),
pytest.mark.xfail((pd.Categorical(['a', 'b']), CategoricalDtype()),),
(pd.Categorical(['a', 'b']), CategoricalDtype()),
(pd.CategoricalIndex(['a', 'b']).dtype, CategoricalDtype()),
pytest.mark.xfail((pd.CategoricalIndex(['a', 'b']), CategoricalDtype()),),
(pd.CategoricalIndex(['a', 'b']), CategoricalDtype()),
(pd.DatetimeIndex([1, 2]), np.dtype('<M8[ns]')),
(pd.DatetimeIndex([1, 2]).dtype, np.dtype('<M8[ns]')),
('<M8[ns]', np.dtype('<M8[ns]')),
Expand Down