A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pandas-dev/pandas/issues/49309 below:

pd.Categorical(categorical, dtype="category") loses orderedness · Issue #49309 · pandas-dev/pandas · GitHub

cat = pd.Categorical([3, 1], categories=[3, 2, 1], ordered=True)

res = pd.Categorical(cat, dtype="category")

>>> res.dtype
CategoricalDtype(categories=[3, 2, 1], ordered=False)
>>> cat.dtype
CategoricalDtype(categories=[3, 2, 1], ordered=True)

By contrast, if we do cat.astype("category") we do retain orderedness.

Found when refactoring sanitize_array. We have two code paths effectively:

if isinstance(data, ExtensionArray):
    if dtype is not None:
        return data.astype(dtype, copy=copy)
    elif copy:
        return data.copy()
    return data

if isinstance(dtype, ExtensionDtype):
    cls = dtype.construct_array_type()
    return cls._from_sequence(data, dtype=dtype, copy=copy)

Switching the order of these checks causes a few Categorical tests to break bc of the Categorical constructor behavior.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4