Skip to content

Commit 09fb68b

Browse files
phoflyehoshuadimarsky
authored andcommitted
REGR: Regression in to_csv for ea dtype categorical (pandas-dev#47347)
1 parent 5b0be4d commit 09fb68b

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
@@ -2262,7 +2262,7 @@ def to_native_types(
22622262
**kwargs,
22632263
) -> np.ndarray:
22642264
"""convert to our native types format"""
2265-
if isinstance(values, Categorical):
2265+
if isinstance(values, Categorical) and values.categories.dtype.kind in "Mm":
22662266
# GH#40754 Convert categorical datetimes to datetime array
22672267
values = algos.take_nd(
22682268
values.categories._values,

pandas/tests/frame/methods/test_to_csv.py

+29
Original file line numberDiff line numberDiff line change
@@ -1285,3 +1285,32 @@ def test_to_csv_na_quoting(self):
12851285
)
12861286
expected = '""\n""\n'
12871287
assert result == expected
1288+
1289+
def test_to_csv_categorical_and_ea(self):
1290+
# GH#46812
1291+
df = DataFrame({"a": "x", "b": [1, pd.NA]})
1292+
df["b"] = df["b"].astype("Int16")
1293+
df["b"] = df["b"].astype("category")
1294+
result = df.to_csv()
1295+
expected_rows = [",a,b", "0,x,1", "1,x,"]
1296+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
1297+
assert result == expected
1298+
1299+
def test_to_csv_categorical_and_interval(self):
1300+
# GH#46297
1301+
df = DataFrame(
1302+
{
1303+
"a": [
1304+
pd.Interval(
1305+
Timestamp("2020-01-01"),
1306+
Timestamp("2020-01-02"),
1307+
inclusive="both",
1308+
)
1309+
]
1310+
}
1311+
)
1312+
df["a"] = df["a"].astype("category")
1313+
result = df.to_csv()
1314+
expected_rows = [",a", '0,"[2020-01-01, 2020-01-02]"']
1315+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
1316+
assert result == expected

0 commit comments

Comments
 (0)