8000 BUG: Fix getitem dtype preservation with multiindexes by m-richards · Pull Request #51895 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

BUG: Fix getitem dtype preservation with multiindexes #51895

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

Merged
merged 14 commits into from
Apr 25, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
suggestions from code review
  • Loading branch information
m-richards committed Mar 14, 2023
commit 1d221b1b8962e1c8851c66f0d96d2be9a5b9ece7
8 changes: 6 additions & 2 deletions pandas/tests/indexing/multiindex/test_multiindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import pandas._libs.index as _index
from pandas.errors import PerformanceWarning

from pandas.core.dtypes.common import is_categorical_dtype

import pandas as pd
from pandas import (
DataFrame,
Expand All @@ -12,6 +14,7 @@
Series,
)
import pandas._testing as tm
from pandas.core.arrays.boolean import BooleanDtype


class TestMultiIndexBasic:
Expand Down Expand Up @@ -211,7 +214,8 @@ def test_multiindex_dtype_preservation(self):
# GH51261
columns = MultiIndex.from_tuples([("A", "B")], names=["lvl1", "lvl2"])
df = DataFrame(["value"], columns=columns).astype("category")
assert (df["A"].dtypes == "category").all()
df_no_multiindex = df["A"]
assert is_categorical_dtype(df_no_multiindex["B"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert is_categorical_dtype(df_no_multiindex["B"])
assert isinstance(df_no_multiindex["B"].dtype, CategoricalDtype)

CategoricalDtype will need an import at the top


# geopandas 1763 analogue
df = DataFrame(
Expand All @@ -222,4 +226,4 @@ def test_multiindex_dtype_preservation(self):
["x", "y"],
],
).assign(bools=Series([True, False], dtype="boolean"))
assert df["bools"].dtype == "boolean"
assert isinstance(df["bools"].dtype, BooleanDtype)
0