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

DEPR: Categorical.to_dense #32639

Merged
merged 5 commits into from
Mar 15, 2020
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: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ Deprecations
- :meth:`DataFrame.mean` and :meth:`DataFrame.median` with ``numeric_only=None`` will include datetime64 and datetime64tz columns in a future version (:issue:`29941`)
- Setting values with ``.loc`` using a positional slice is deprecated and will raise in a future version. Use ``.loc`` with labels or ``.iloc`` with positions instead (:issue:`31840`)
- :meth:`DataFrame.to_dict` has deprecated accepting short names for ``orient`` in future versions (:issue:`32515`)
- :meth:`Categorical.to_dense` is deprecated and will be removed in a future version, use ``np.asarray(cat)`` instead (:issue:`32639`)

.. ---------------------------------------------------------------------------

Expand Down
6 changes: 6 additions & 0 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1675,6 +1675,12 @@ def to_dense(self):
-------
dense : array
"""
warn(
"Categorical.to_dense is deprecated and will be removed in "
"a future version. Use np.asarray(cat) instead.",
FutureWarning,
stacklevel=2,
)
return np.asarray(self)

def fillna(self, value=None, method=None, limit=None):
Expand Down
14 changes: 10 additions & 4 deletions pandas/tests/arrays/categorical/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def test_set_categories(self):
tm.assert_index_equal(c.categories, Index([1, 2, 3, 4]))

exp = np.array([1, 2, 3, 4, 1], dtype=np.int64)
tm.assert_numpy_array_equal(c.to_dense(), exp)
tm.assert_numpy_array_equal(np.asarray(c), exp)

# all "pointers" to '4' must be changed from 3 to 0,...
c = c.set_categories([4, 3, 2, 1])
Expand All @@ -260,21 +260,27 @@ def test_set_categories(self):

# output is the same
exp = np.array([1, 2, 3, 4, 1], dtype=np.int64)
tm.assert_numpy_array_equal(c.to_dense(), exp)
tm.assert_numpy_array_equal(np.asarray(c), exp)
assert c.min() == 4
assert c.max() == 1

# set_categories should set the ordering if specified
c2 = c.set_categories([4, 3, 2, 1], ordered=False)
assert not c2.ordered

tm.assert_numpy_array_equal(c.to_dense(), c2.to_dense())
tm.assert_numpy_array_equal(np.asarray(c), np.asarray(c2))

# set_categories should pass thru the ordering
c2 = c.set_ordered(False).set_categories([4, 3, 2, 1])
assert not c2.ordered

tm.assert_numpy_array_equal(c.to_dense(), c2.to_dense())
tm.assert_numpy_array_equal(np.asarray(c), np.asarray(c2))

def test_to_dense_deprecated(self):
cat = Categorical(["a", "b", "c", "a"], ordered=True)

with tm.assert_produces_warning(FutureWarning):
cat.to_dense()

@pytest.mark.parametrize(
"values, categories, new_categories",
Expand Down