Skip to content

Allow dtype='category' or dtype=None for Series with Categorical #12636

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.18.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,5 @@ Bug Fixes
- Bug in ``value_counts`` when ``normalize=True`` and ``dropna=True`` where nulls still contributed to the normalized count (:issue:`12558`)

- Bug in ``CategoricalIndex.get_loc`` returns different result from regular ``Index`` (:issue:`12531`)

- Bug in ``Series`` when Series with Categorical is created with dtype specified (:issue:`12574`)
8 changes: 6 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from pandas.core import generic, base
from pandas.core.internals import SingleBlockManager
from pandas.core.categorical import Categorical, CategoricalAccessor
from pandas.core.dtypes import CategoricalDtype
import pandas.core.strings as strings
from pandas.tseries.common import (maybe_to_datetimelike,
CombinedDatetimelikeProperties)
Expand Down Expand Up @@ -195,9 +196,12 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
else:
data = data.reindex(index, copy=copy)
elif isinstance(data, Categorical):
if dtype is not None:
# Allow dtype=category only, otherwise error
Copy link
Contributor

Choose a reason for hiding this comment

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

also need to handle a passed category dtype with a non Categorical

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed others, this is the last one. Do you mean ensuring this is handled?

a = pd.Series([1,2,3], dtype='category')

Because that seems graceful:

>>> a
0    1
1    2
2    3
dtype: category
Categories (3, int64): [1, 2, 3]

Copy link
Contributor

Choose a reason for hiding this comment

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

yep - look over the test coverage for that and see if anything missing (out new tests near)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it. Thanks. Running nosetests now and then will push.

if ((dtype is not None) and
Copy link
Contributor

Choose a reason for hiding this comment

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

this is not right here; use is_categorical_dtype(dtype)

not isinstance(dtype, CategoricalDtype)):
raise ValueError("cannot specify a dtype with a "
"Categorical")
"Categorical unless "
"dtype='category'")
elif (isinstance(data, types.GeneratorType) or
(compat.PY3 and isinstance(data, map))):
data = list(data)
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ def test_basic(self):
self.assertFalse(is_categorical(np.dtype('float64')))
self.assertFalse(is_categorical(1.0))

def test_series_with_dtype(self):
self.assertRaises(
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a comment like #GH 12574, pointing back to the original issue?

ValueError, lambda: Series(Categorical([1, 2, 3]), dtype='int64'))
s = Series(Categorical([1, 2, 3]), dtype='category')
self.assertTrue(is_categorical_dtype(s))
Copy link
Contributor

Choose a reason for hiding this comment

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

these go in tests/series/ test_constructor

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean moving the contents of this test into test_constructor_categorical() in tests/series/test_constructor?

Copy link
Contributor

Choose a reason for hiding this comment

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

yep

self.assertTrue(is_categorical_dtype(s.dtype))


class TestDatetimeTZDtype(Base, tm.TestCase):

Expand Down