Skip to content

Commit

Permalink
DEPR: Categorical.to_dense (pandas-dev#32639)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and SeeminSyed committed Mar 22, 2020
1 parent 01c5918 commit 64f96e2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
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

0 comments on commit 64f96e2

Please sign in to comment.