Skip to content

Commit 588808a

Browse files
refactor
1 parent 7ef7fb2 commit 588808a

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

pandas/core/frame.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
from pandas.core.dtypes.dtypes import (
116116
ArrowDtype,
117117
BaseMaskedDtype,
118+
CategoricalDtype,
118119
ExtensionDtype,
119120
)
120121
from pandas.core.dtypes.generic import (
@@ -12015,15 +12016,29 @@ def _transform_ord_cat_cols_to_coded_cols(self) -> DataFrame:
1201512016
if len(categ.columns) == 0:
1201612017
return self
1201712018

12018-
cols_convert = categ.loc[:, categ.agg(lambda x: x.cat.ordered)].columns
12019+
cols_convert = categ.loc[:, categ.agg(lambda x: x.cat.ordered)].columns.unique()
12020+
single_cols = [col for col in cols_convert if isinstance(categ[col], Series)]
12021+
duplicated_cols = [
12022+
col for col in cols_convert if isinstance(categ[col], DataFrame)
12023+
]
1201912024

12020-
if len(cols_convert) > 0:
12021-
data = self.copy(deep=False)
12022-
data[cols_convert] = data[cols_convert].transform(
12025+
if not single_cols and not duplicated_cols:
12026+
return self
12027+
12028+
data = self.copy(deep=False)
12029+
if single_cols:
12030+
data[single_cols] = data[single_cols].transform(
1202312031
lambda x: x.cat.codes.replace(-1, np.nan)
1202412032
)
12025-
return data
12026-
return self
12033+
12034+
if duplicated_cols:
12035+
data[duplicated_cols] = data[duplicated_cols].apply(
12036+
lambda x: x.cat.codes.replace(-1, np.nan)
12037+
if isinstance(x, CategoricalDtype) and bool(x.ordered)
12038+
else x
12039+
)
12040+
12041+
return data
1202712042

1202812043
# ----------------------------------------------------------------------
1202912044
# ndarray-like stats methods

pandas/tests/frame/methods/test_cov_corr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,11 @@ def test_corr_numeric_only(self, meth, numeric_only):
255255
df.corr(meth, numeric_only=numeric_only)
256256

257257
@pytest.mark.parametrize("method", ["kendall", "spearman"])
258+
@td.skip_if_no("scipy")
258259
def test_corr_rank_ordered_categorical(
259260
self,
260261
method,
261262
):
262-
pytest.importorskip("scipy")
263263
df = DataFrame(
264264
{
265265
"ord_cat": Series(

0 commit comments

Comments
 (0)