Skip to content

Commit 7cd2e54

Browse files
authored
Fix support for pandas.CategoricalDtype (#205)
This regressed in `2.8.1`. Fix #197
1 parent 1a5a574 commit 7cd2e54

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
UNRELEASED
2+
----------
3+
4+
*UNRELEASED*
5+
6+
* `#197 <https://github.com/ESSS/pytest-regressions/issues/197>`__: Fixed support for ``pandas.CategoricalDtype``, regressed in ``2.8.1``.
7+
18
2.8.2
29
-----
310

src/pytest_regressions/dataframe_regression.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,12 @@ def check(
249249

250250
for column in data_frame.columns:
251251
array = data_frame[column]
252-
# Skip assertion if an array of strings
252+
253+
# Categorical columns (numbers mapped to strings) are supported.
254+
if isinstance(array.dtype, pd.CategoricalDtype):
255+
continue
256+
257+
# Arrays of strings are supported.
253258
if (array.dtype == "O") and (type(array.iloc[0]) is str):
254259
continue
255260
# Rejected: timedelta, objects, zero-terminated bytes, unicode strings and raw data

tests/test_dataframe_regression.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pandas as pd
77
import pytest
88

9+
from pytest_regressions.dataframe_regression import DataFrameRegressionFixture
910
from pytest_regressions.testing import check_regression_fixture_workflow
1011

1112

@@ -284,3 +285,15 @@ def test_dataframe_with_datetime(dataframe_regression):
284285
)
285286

286287
dataframe_regression.check(df)
288+
289+
290+
def test_categorical(dataframe_regression: DataFrameRegressionFixture) -> None:
291+
"""Support for categorical data columns (#197)."""
292+
values = [3, 3, 1, 1, 2, 0, 0, 2]
293+
types = pd.Categorical.from_codes(
294+
np.array(values),
295+
categories=["a", "b", "c", "d"],
296+
)
297+
298+
df = pd.DataFrame.from_dict({"types": types})
299+
dataframe_regression.check(df)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
,types
2+
0,d
3+
1,d
4+
2,b
5+
3,b
6+
4,c
7+
5,a
8+
6,a
9+
7,c

0 commit comments

Comments
 (0)