Skip to content

Commit 87a54fe

Browse files
committed
some comments
1 parent 8f460ac commit 87a54fe

File tree

3 files changed

+22
-25
lines changed

3 files changed

+22
-25
lines changed

pandas/core/arrays/categorical.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,8 @@ def __init__(
468468
try:
469469
codes, categories = factorize(values, sort=True)
470470
except TypeError as err:
471+
# raise, as we don't have a sortable data structure and so
472+
# the user should give us one by specifying categories
471473
codes, categories = factorize(values, sort=False)
472474
if dtype.ordered:
473475
raise TypeError(

pandas/tests/arrays/categorical/test_constructors.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,3 +786,23 @@ def test_range_values_preserves_rangeindex_categories(self, values, categories):
786786
result = Categorical(values=values, categories=categories).categories
787787
expected = RangeIndex(range(5))
788788
tm.assert_index_equal(result, expected, exact=True)
789+
790+
def test_categorical_preserve_object_dtype_from_pandas(self):
791+
with pd.option_context("future.infer_string", True):
792+
ser = Series(["foo", "bar", "baz"], dtype="object")
793+
idx = Index(["foo", "bar", "baz"], dtype="object")
794+
arr = np.array(["foo", "bar", "baz"], dtype="object")
795+
pylist = ["foo", "bar", "baz"]
796+
797+
cat_from_ser = Categorical(ser)
798+
cat_from_idx = Categorical(idx)
799+
cat_from_arr = Categorical(arr)
800+
cat_from_list = Categorical(pylist)
801+
802+
# Series/Index with object dtype: preserve object dtype
803+
assert cat_from_ser.categories.dtype == "object"
804+
assert cat_from_idx.categories.dtype == "object"
805+
806+
# Numpy array or list: infer string dtype
807+
assert cat_from_arr.categories.dtype == "str"
808+
assert cat_from_list.categories.dtype == "str"

pandas/tests/extension/test_categorical.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -180,31 +180,6 @@ def test_array_repr(self, data, size):
180180
def test_groupby_extension_agg(self, as_index, data_for_grouping):
181181
super().test_groupby_extension_agg(as_index, data_for_grouping)
182182

183-
def test_categorical_preserve_object_dtype_from_pandas(self):
184-
import numpy as np
185-
186-
import pandas as pd
187-
188-
pd.options.future.infer_string = True
189-
190-
ser = pd.Series(["foo", "bar", "baz"], dtype="object")
191-
idx = pd.Index(["foo", "bar", "baz"], dtype="object")
192-
arr = np.array(["foo", "bar", "baz"], dtype="object")
193-
pylist = ["foo", "bar", "baz"]
194-
195-
cat_from_ser = Categorical(ser)
196-
cat_from_idx = Categorical(idx)
197-
cat_from_arr = Categorical(arr)
198-
cat_from_list = Categorical(pylist)
199-
200-
# Series/Index with object dtype: preserve object dtype
201-
assert cat_from_ser.categories.dtype == "object"
202-
assert cat_from_idx.categories.dtype == "object"
203-
204-
# Numpy array or list: infer string dtype
205-
assert cat_from_arr.categories.dtype == "str"
206-
assert cat_from_list.categories.dtype == "str"
207-
208183

209184
class Test2DCompat(base.NDArrayBacked2DTests):
210185
def test_repr_2d(self, data):

0 commit comments

Comments
 (0)