-
-
Notifications
You must be signed in to change notification settings - Fork 18.8k
BUG: creating Categorical from pandas Index/Series with "object" dtype infers string #62080
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
base: main
Are you sure you want to change the base?
Changes from all commits
c4e1c18
e1a893d
cfa767f
c0ae870
5188b81
b63a723
0fb42cc
9216954
8f460ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,6 +180,31 @@ def test_array_repr(self, data, size): | |
def test_groupby_extension_agg(self, as_index, data_for_grouping): | ||
super().test_groupby_extension_agg(as_index, data_for_grouping) | ||
|
||
def test_categorical_preserve_object_dtype_from_pandas(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will probably go in tests.arrays.categorical.test_constructors or something similar |
||
import numpy as np | ||
|
||
import pandas as pd | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these imports go at the top of the file |
||
|
||
pd.options.future.infer_string = True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use tm.option_context for this |
||
|
||
ser = pd.Series(["foo", "bar", "baz"], dtype="object") | ||
idx = pd.Index(["foo", "bar", "baz"], dtype="object") | ||
arr = np.array(["foo", "bar", "baz"], dtype="object") | ||
pylist = ["foo", "bar", "baz"] | ||
|
||
cat_from_ser = Categorical(ser) | ||
cat_from_idx = Categorical(idx) | ||
cat_from_arr = Categorical(arr) | ||
cat_from_list = Categorical(pylist) | ||
|
||
# Series/Index with object dtype: preserve object dtype | ||
assert cat_from_ser.categories.dtype == "object" | ||
assert cat_from_idx.categories.dtype == "object" | ||
|
||
# Numpy array or list: infer string dtype | ||
assert cat_from_arr.categories.dtype == "str" | ||
assert cat_from_list.categories.dtype == "str" | ||
|
||
|
||
class Test2DCompat(base.NDArrayBacked2DTests): | ||
def test_repr_2d(self, data): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this removed?