Skip to content

Commit 64f96e2

Browse files
jbrockmendelSeeminSyed
authored andcommitted
DEPR: Categorical.to_dense (pandas-dev#32639)
1 parent 01c5918 commit 64f96e2

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ Deprecations
207207
- :meth:`DataFrame.mean` and :meth:`DataFrame.median` with ``numeric_only=None`` will include datetime64 and datetime64tz columns in a future version (:issue:`29941`)
208208
- 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`)
209209
- :meth:`DataFrame.to_dict` has deprecated accepting short names for ``orient`` in future versions (:issue:`32515`)
210+
- :meth:`Categorical.to_dense` is deprecated and will be removed in a future version, use ``np.asarray(cat)`` instead (:issue:`32639`)
210211

211212
.. ---------------------------------------------------------------------------
212213

pandas/core/arrays/categorical.py

+6
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,12 @@ def to_dense(self):
16751675
-------
16761676
dense : array
16771677
"""
1678+
warn(
1679+
"Categorical.to_dense is deprecated and will be removed in "
1680+
"a future version. Use np.asarray(cat) instead.",
1681+
FutureWarning,
1682+
stacklevel=2,
1683+
)
16781684
return np.asarray(self)
16791685

16801686
def fillna(self, value=None, method=None, limit=None):

pandas/tests/arrays/categorical/test_api.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def test_set_categories(self):
247247
tm.assert_index_equal(c.categories, Index([1, 2, 3, 4]))
248248

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

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

261261
# output is the same
262262
exp = np.array([1, 2, 3, 4, 1], dtype=np.int64)
263-
tm.assert_numpy_array_equal(c.to_dense(), exp)
263+
tm.assert_numpy_array_equal(np.asarray(c), exp)
264264
assert c.min() == 4
265265
assert c.max() == 1
266266

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

271-
tm.assert_numpy_array_equal(c.to_dense(), c2.to_dense())
271+
tm.assert_numpy_array_equal(np.asarray(c), np.asarray(c2))
272272

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

277-
tm.assert_numpy_array_equal(c.to_dense(), c2.to_dense())
277+
tm.assert_numpy_array_equal(np.asarray(c), np.asarray(c2))
278+
279+
def test_to_dense_deprecated(self):
280+
cat = Categorical(["a", "b", "c", "a"], ordered=True)
281+
282+
with tm.assert_produces_warning(FutureWarning):
283+
cat.to_dense()
278284

279285
@pytest.mark.parametrize(
280286
"values, categories, new_categories",

0 commit comments

Comments
 (0)