Skip to content

Commit cff5b13

Browse files
jbrockmendeljreback
authored andcommitted
DEPR: DataFrame([categorical, ...]) special casing (pandas-dev#41557)
Co-authored-by: Jeff Reback <[email protected]>
1 parent a280542 commit cff5b13

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ Deprecations
674674
- Deprecated setting :attr:`Categorical._codes`, create a new :class:`Categorical` with the desired codes instead (:issue:`40606`)
675675
- Deprecated behavior of :meth:`DatetimeIndex.union` with mixed timezones; in a future version both will be cast to UTC instead of object dtype (:issue:`39328`)
676676
- Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`)
677+
- Deprecated special treatment of lists with first element a Categorical in the :class:`DataFrame` constructor; pass as ``pd.DataFrame({col: categorical, ...})`` instead (:issue:`38845`)
677678
- Deprecated passing arguments as positional (except for ``"method"``) in :meth:`DataFrame.interpolate` and :meth:`Series.interpolate` (:issue:`41485`)
678679
- Deprecated passing arguments (apart from ``value``) as positional in :meth:`DataFrame.fillna` and :meth:`Series.fillna` (:issue:`41485`)
679680

pandas/core/internals/construction.py

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Hashable,
1212
Sequence,
1313
)
14+
import warnings
1415

1516
import numpy as np
1617
import numpy.ma as ma
@@ -772,6 +773,16 @@ def to_arrays(
772773
return [], ensure_index([])
773774

774775
elif isinstance(data[0], Categorical):
776+
# GH#38845 deprecate special case
777+
warnings.warn(
778+
"The behavior of DataFrame([categorical, ...]) is deprecated and "
779+
"in a future version will be changed to match the behavior of "
780+
"DataFrame([any_listlike, ...]). "
781+
"To retain the old behavior, pass as a dictionary "
782+
"DataFrame({col: categorical, ..})",
783+
FutureWarning,
784+
stacklevel=4,
785+
)
775786
if columns is None:
776787
columns = ibase.default_index(len(data))
777788
return data, columns

pandas/io/pytables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4568,7 +4568,7 @@ def read(
45684568
df = DataFrame(values, columns=cols_, index=index_)
45694569
else:
45704570
# Categorical
4571-
df = DataFrame([values], columns=cols_, index=index_)
4571+
df = DataFrame._from_arrays([values], columns=cols_, index=index_)
45724572
assert (df.dtypes == values.dtype).all(), (df.dtypes, values.dtype)
45734573
frames.append(df)
45744574

pandas/tests/frame/test_constructors.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -2109,12 +2109,16 @@ def test_constructor_categorical(self):
21092109

21102110
def test_construct_from_1item_list_of_categorical(self):
21112111
# ndim != 1
2112-
df = DataFrame([Categorical(list("abc"))])
2112+
msg = "will be changed to match the behavior"
2113+
with tm.assert_produces_warning(FutureWarning, match=msg):
2114+
df = DataFrame([Categorical(list("abc"))])
21132115
expected = DataFrame({0: Series(list("abc"), dtype="category")})
21142116
tm.assert_frame_equal(df, expected)
21152117

21162118
def test_construct_from_list_of_categoricals(self):
2117-
df = DataFrame([Categorical(list("abc")), Categorical(list("abd"))])
2119+
msg = "will be changed to match the behavior"
2120+
with tm.assert_produces_warning(FutureWarning, match=msg):
2121+
df = DataFrame([Categorical(list("abc")), Categorical(list("abd"))])
21182122
expected = DataFrame(
21192123
{
21202124
0: Series(list("abc"), dtype="category"),
@@ -2126,7 +2130,9 @@ def test_construct_from_list_of_categoricals(self):
21262130

21272131
def test_from_nested_listlike_mixed_types(self):
21282132
# mixed
2129-
df = DataFrame([Categorical(list("abc")), list("def")])
2133+
msg = "will be changed to match the behavior"
2134+
with tm.assert_produces_warning(FutureWarning, match=msg):
2135+
df = DataFrame([Categorical(list("abc")), list("def")])
21302136
expected = DataFrame(
21312137
{0: Series(list("abc"), dtype="category"), 1: list("def")}, columns=[0, 1]
21322138
)
@@ -2140,8 +2146,10 @@ def test_construct_from_listlikes_mismatched_lengths(self):
21402146
"Passed arrays should have the same length as the rows Index",
21412147
]
21422148
)
2149+
msg2 = "will be changed to match the behavior"
21432150
with pytest.raises(ValueError, match=msg):
2144-
DataFrame([Categorical(list("abc")), Categorical(list("abdefg"))])
2151+
with tm.assert_produces_warning(FutureWarning, match=msg2):
2152+
DataFrame([Categorical(list("abc")), Categorical(list("abdefg"))])
21452153

21462154
def test_constructor_categorical_series(self):
21472155

0 commit comments

Comments
 (0)