Skip to content

Commit

Permalink
DEPR: DataFrame([categorical, ...]) special casing (#41557)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeff Reback <jeff@reback.net>
  • Loading branch information
jbrockmendel and jreback committed May 21, 2021
1 parent 1f63790 commit 5bb2fb2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ Deprecations
- Deprecated setting :attr:`Categorical._codes`, create a new :class:`Categorical` with the desired codes instead (:issue:`40606`)
- 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`)
- Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`)
- Deprecated special treatment of lists with first element a Categorical in the :class:`DataFrame` constructor; pass as ``pd.DataFrame({col: categorical, ...})`` instead (:issue:`38845`)
- Deprecated passing arguments as positional (except for ``"method"``) in :meth:`DataFrame.interpolate` and :meth:`Series.interpolate` (:issue:`41485`)
- Deprecated passing arguments (apart from ``value``) as positional in :meth:`DataFrame.fillna` and :meth:`Series.fillna` (:issue:`41485`)

Expand Down
11 changes: 11 additions & 0 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Hashable,
Sequence,
)
import warnings

import numpy as np
import numpy.ma as ma
Expand Down Expand Up @@ -772,6 +773,16 @@ def to_arrays(
return [], ensure_index([])

elif isinstance(data[0], Categorical):
# GH#38845 deprecate special case
warnings.warn(
"The behavior of DataFrame([categorical, ...]) is deprecated and "
"in a future version will be changed to match the behavior of "
"DataFrame([any_listlike, ...]). "
"To retain the old behavior, pass as a dictionary "
"DataFrame({col: categorical, ..})",
FutureWarning,
stacklevel=4,
)
if columns is None:
columns = ibase.default_index(len(data))
return data, columns
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -4568,7 +4568,7 @@ def read(
df = DataFrame(values, columns=cols_, index=index_)
else:
# Categorical
df = DataFrame([values], columns=cols_, index=index_)
df = DataFrame._from_arrays([values], columns=cols_, index=index_)
assert (df.dtypes == values.dtype).all(), (df.dtypes, values.dtype)
frames.append(df)

Expand Down
16 changes: 12 additions & 4 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2109,12 +2109,16 @@ def test_constructor_categorical(self):

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

def test_construct_from_list_of_categoricals(self):
df = DataFrame([Categorical(list("abc")), Categorical(list("abd"))])
msg = "will be changed to match the behavior"
with tm.assert_produces_warning(FutureWarning, match=msg):
df = DataFrame([Categorical(list("abc")), Categorical(list("abd"))])
expected = DataFrame(
{
0: Series(list("abc"), dtype="category"),
Expand All @@ -2126,7 +2130,9 @@ def test_construct_from_list_of_categoricals(self):

def test_from_nested_listlike_mixed_types(self):
# mixed
df = DataFrame([Categorical(list("abc")), list("def")])
msg = "will be changed to match the behavior"
with tm.assert_produces_warning(FutureWarning, match=msg):
df = DataFrame([Categorical(list("abc")), list("def")])
expected = DataFrame(
{0: Series(list("abc"), dtype="category"), 1: list("def")}, columns=[0, 1]
)
Expand All @@ -2140,8 +2146,10 @@ def test_construct_from_listlikes_mismatched_lengths(self):
"Passed arrays should have the same length as the rows Index",
]
)
msg2 = "will be changed to match the behavior"
with pytest.raises(ValueError, match=msg):
DataFrame([Categorical(list("abc")), Categorical(list("abdefg"))])
with tm.assert_produces_warning(FutureWarning, match=msg2):
DataFrame([Categorical(list("abc")), Categorical(list("abdefg"))])

def test_constructor_categorical_series(self):

Expand Down

0 comments on commit 5bb2fb2

Please sign in to comment.