Skip to content

Commit f0af15c

Browse files
meeseeksmachinephoflsimonjayhawkins
authored
Backport PR #47347 on branch 1.4.x (REGR: Regression in to_csv for ea dtype categorical) (#47388)
* Backport PR #47347: REGR: Regression in to_csv for ea dtype categorical * inclusive -> closed Co-authored-by: Patrick Hoefler <[email protected]> Co-authored-by: Simon Hawkins <[email protected]>
1 parent de071c6 commit f0af15c

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

doc/source/whatsnew/v1.4.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ including other versions of pandas.
1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
1717
- Fixed regression in :meth:`DataFrame.replace` when the replacement value was explicitly ``None`` when passed in a dictionary to ``to_replace`` also casting other columns to object dtype even when there were no values to replace (:issue:`46634`)
18+
- Fixed regression in :meth:`DataFrame.to_csv` raising error when :class:`DataFrame` contains extension dtype categorical column (:issue:`46297`, :issue:`46812`)
1819
- Fixed regression in representation of ``dtypes`` attribute of :class:`MultiIndex` (:issue:`46900`)
1920
- Fixed regression when setting values with :meth:`DataFrame.loc` updating :class:`RangeIndex` when index was set as new column and column was updated afterwards (:issue:`47128`)
2021
- Fixed regression in :meth:`DataFrame.nsmallest` led to wrong results when ``np.nan`` in the sorting column (:issue:`46589`)

pandas/core/internals/blocks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2160,7 +2160,7 @@ def to_native_types(
21602160
**kwargs,
21612161
) -> np.ndarray:
21622162
"""convert to our native types format"""
2163-
if isinstance(values, Categorical):
2163+
if isinstance(values, Categorical) and values.categories.dtype.kind in "Mm":
21642164
# GH#40754 Convert categorical datetimes to datetime array
21652165
values = take_nd(
21662166
values.categories._values,

pandas/tests/frame/methods/test_to_csv.py

+29
Original file line numberDiff line numberDiff line change
@@ -1333,3 +1333,32 @@ def test_to_csv_na_quoting(self):
13331333
)
13341334
expected = '""\n""\n'
13351335
assert result == expected
1336+
1337+
def test_to_csv_categorical_and_ea(self):
1338+
# GH#46812
1339+
df = DataFrame({"a": "x", "b": [1, pd.NA]})
1340+
df["b"] = df["b"].astype("Int16")
1341+
df["b"] = df["b"].astype("category")
1342+
result = df.to_csv()
1343+
expected_rows = [",a,b", "0,x,1", "1,x,"]
1344+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
1345+
assert result == expected
1346+
1347+
def test_to_csv_categorical_and_interval(self):
1348+
# GH#46297
1349+
df = DataFrame(
1350+
{
1351+
"a": [
1352+
pd.Interval(
1353+
Timestamp("2020-01-01"),
1354+
Timestamp("2020-01-02"),
1355+
closed="both",
1356+
)
1357+
]
1358+
}
1359+
)
1360+
df["a"] = df["a"].astype("category")
1361+
result = df.to_csv()
1362+
expected_rows = [",a", '0,"[2020-01-01, 2020-01-02]"']
1363+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
1364+
assert result == expected

0 commit comments

Comments
 (0)