-
-
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?
Conversation
slack link update issue pandas-dev#61690
@jbrockmendel Regarding this bug, the change to always preserve object dtype for categories when constructing a Categorical from a pandas Series or Index with dtype="object" is a behavioral change that affects a wide range of pandas internals and user-facing APIs. Hence I am seeing a lot of failures. I see two ways to resolve without changing overall behavior.
Let me know your thoughts. |
@@ -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 comment
The 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
def test_categorical_preserve_object_dtype_from_pandas(self): | ||
import numpy as np | ||
|
||
import pandas as pd |
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.
these imports go at the top of the file
|
||
import pandas as pd | ||
|
||
pd.options.future.infer_string = True |
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.
use tm.option_context for this
@@ -465,15 +470,17 @@ def __init__( | |||
except TypeError as err: | |||
codes, categories = factorize(values, sort=False) | |||
if dtype.ordered: | |||
# raise, as we don't have a sortable data structure and so | |||
# the user should give us one by specifying categories |
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?
doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.Preserve object dtype for categories when constructing Categorical from pandas objects
This PR fixes an inconsistency in how pandas infers the dtype of categories when constructing a Categorical from different input types:
When constructing a Categorical from a pandas Series or Index with dtype="object", the categories' dtype is now preserved as object.
When constructing from a NumPy array with dtype="object" or a raw Python sequence, pandas continues to infer the most specific dtype for the categories (e.g., str if all elements are strings).
This change brings the behavior of Categorical in line with how Series and Index handle dtype preservation, making the API more consistent and predictable.
Example
Documentation and release notes have been updated.
Closes: #61778