Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEPR: concat ignoring empty objects #52532

Merged
merged 28 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
63292d4
DEPR: concat with empty objects
jbrockmendel Apr 7, 2023
2ace79c
xfail on 32bit
jbrockmendel Apr 8, 2023
6258adf
missing reason
jbrockmendel Apr 8, 2023
bfd969f
Merge branch 'main' into depr-concat-empty
jbrockmendel Apr 10, 2023
51e6d36
Fix AM build
jbrockmendel Apr 10, 2023
52ce0d7
post-merge fixup
jbrockmendel Apr 10, 2023
f8dc81e
Merge branch 'main' into depr-concat-empty
jbrockmendel Apr 11, 2023
163bf8a
catch more specifically
jbrockmendel Apr 11, 2023
03a0641
un-xfail
jbrockmendel Apr 12, 2023
49a7146
Merge branch 'main' into depr-concat-empty
jbrockmendel Apr 12, 2023
7e2e995
mypy fixup
jbrockmendel Apr 12, 2023
7c0c715
Merge branch 'main' into depr-concat-empty
jbrockmendel Apr 13, 2023
7f2977a
Merge branch 'main' into depr-concat-empty
jbrockmendel Apr 13, 2023
0eaf359
Merge branch 'main' into depr-concat-empty
jbrockmendel Apr 17, 2023
a878fea
Merge branch 'main' into depr-concat-empty
jbrockmendel Apr 18, 2023
75d5041
update test
jbrockmendel Apr 18, 2023
9e2de8f
Merge branch 'main' into depr-concat-empty
jbrockmendel May 4, 2023
392b40a
Fix broken test
jbrockmendel May 4, 2023
465c141
Merge branch 'main' into depr-concat-empty
jbrockmendel May 16, 2023
3666bca
remove duplicate whatsnew entries
jbrockmendel May 16, 2023
390d4ef
Merge branch 'main' into depr-concat-empty
jbrockmendel May 22, 2023
aa5794f
Merge branch 'main' into depr-concat-empty
jbrockmendel May 23, 2023
1277b26
Merge branch 'main' into depr-concat-empty
jbrockmendel May 24, 2023
5cddae9
Merge branch 'main' into depr-concat-empty
jbrockmendel May 24, 2023
8e58bff
Merge branch 'main' into depr-concat-empty
jbrockmendel May 25, 2023
47a17b3
Merge branch 'main' into depr-concat-empty
jbrockmendel May 25, 2023
e696c53
remove unused
jbrockmendel May 25, 2023
7f07121
Merge branch 'main' into depr-concat-empty
jbrockmendel May 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ Deprecations
- Deprecated 'broadcast_axis' keyword in :meth:`Series.align` and :meth:`DataFrame.align`, upcast before calling ``align`` with ``left = DataFrame({col: left for col in right.columns}, index=right.index)`` (:issue:`51856`)
- Deprecated 'method', 'limit', and 'fill_axis' keywords in :meth:`DataFrame.align` and :meth:`Series.align`, explicitly call ``fillna`` on the alignment results instead (:issue:`51856`)
- Deprecated 'quantile' keyword in :meth:`Rolling.quantile` and :meth:`Expanding.quantile`, renamed as 'q' instead (:issue:`52550`)
- Deprecated :func:`concat` behavior when any of the objects being concatenated have length 0; in the past the dtypes of empty objects were ignored when determining the resulting dtype, in a future version they will not (:issue:`39122`)
- Deprecated :meth:`.DataFrameGroupBy.apply` and methods on the objects returned by :meth:`.DataFrameGroupBy.resample` operating on the grouping column(s); select the columns to operate on after groupby to either explicitly include or exclude the groupings and avoid the ``FutureWarning`` (:issue:`7155`)
- Deprecated :meth:`.Groupby.all` and :meth:`.GroupBy.any` with datetime64 or :class:`PeriodDtype` values, matching the :class:`Series` and :class:`DataFrame` deprecations (:issue:`34479`)
- Deprecated :meth:`Categorical.to_list`, use ``obj.tolist()`` instead (:issue:`51254`)
Expand Down
13 changes: 13 additions & 0 deletions pandas/core/dtypes/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
Sequence,
cast,
)
import warnings

import numpy as np

from pandas._libs import lib
from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.astype import astype_array
from pandas.core.dtypes.cast import (
Expand Down Expand Up @@ -107,6 +109,17 @@ def concat_compat(

if len(to_concat) < len(orig):
_, _, alt_dtype = _get_result_dtype(orig, non_empties)
if alt_dtype != target_dtype:
# GH#39122
warnings.warn(
"The behavior of array concatenation with empty entries is "
"deprecated. In a future version, this will no longer exclude "
"empty items when determining the result dtype. "
"To retain the old behavior, exclude the empty entries before "
"the concat operation.",
FutureWarning,
stacklevel=find_stack_level(),
)

if target_dtype is not None:
to_concat = [astype_array(arr, target_dtype, copy=False) for arr in to_concat]
Expand Down
31 changes: 16 additions & 15 deletions pandas/core/internals/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,9 @@ def is_na(self) -> bool:

values = blk.values
if values.size == 0:
# GH#39122 this case will return False once deprecation is enforced
return True

if isinstance(values.dtype, SparseDtype):
return False

Expand All @@ -406,16 +408,14 @@ def is_na(self) -> bool:
return all(isna_all(row) for row in values)

@cache_readonly
def is_na_without_isna_all(self) -> bool:
def is_na_after_size_and_isna_all_deprecation(self) -> bool:
"""
Will self.is_na be True after values.size == 0 deprecation and isna_all
deprecation are enforced?
"""
blk = self.block
if blk.dtype.kind == "V":
return True
if not blk._can_hold_na:
return False

values = blk.values
if values.size == 0:
return True
Comment on lines -409 to -418
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the future behavior won't depend on values.size == 0 (note the changed method name/docstring)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure but the deprecation hasn't been enforced yet, why is this changing already?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method is for checking on the future behavior to see if we need to issue a warning.

return False

def get_reindexed_values(self, empty_dtype: DtypeObj, upcasted_na) -> ArrayLike:
Expand Down Expand Up @@ -477,17 +477,16 @@ def _concatenate_join_units(join_units: list[JoinUnit], copy: bool) -> ArrayLike

if empty_dtype != empty_dtype_future:
if empty_dtype == concat_values.dtype:
# GH#40893
# GH#39122, GH#40893
warnings.warn(
"The behavior of DataFrame concatenation with all-NA entries is "
"deprecated. In a future version, this will no longer exclude "
"all-NA columns when determining the result dtypes. "
"To retain the old behavior, cast the all-NA columns to the "
"desired dtype before the concat operation.",
"The behavior of DataFrame concatenation with empty or all-NA "
"entries is deprecated. In a future version, this will no longer "
"exclude empty or all-NA columns when determining the result dtypes. "
"To retain the old behavior, exclude the relevant entries before "
"the concat operation.",
FutureWarning,
stacklevel=find_stack_level(),
)

return concat_values


Expand Down Expand Up @@ -543,7 +542,9 @@ def _get_empty_dtype(join_units: Sequence[JoinUnit]) -> tuple[DtypeObj, DtypeObj
dtype_future = dtype
if len(dtypes) != len(join_units):
dtypes_future = [
unit.block.dtype for unit in join_units if not unit.is_na_without_isna_all
unit.block.dtype
for unit in join_units
if not unit.is_na_after_size_and_isna_all_deprecation
]
if not len(dtypes_future):
dtypes_future = [
Expand Down
7 changes: 5 additions & 2 deletions pandas/tests/dtypes/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ def test_concat_mismatched_categoricals_with_empty():
ser1 = Series(["a", "b", "c"], dtype="category")
ser2 = Series([], dtype="category")

result = _concat.concat_compat([ser1._values, ser2._values])
expected = pd.concat([ser1, ser2])._values
msg = "The behavior of array concatenation with empty entries is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = _concat.concat_compat([ser1._values, ser2._values])
with tm.assert_produces_warning(FutureWarning, match=msg):
expected = pd.concat([ser1, ser2])._values
tm.assert_categorical_equal(result, expected)


Expand Down
11 changes: 8 additions & 3 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,13 @@ def f3(x):

df2 = DataFrame({"a": [3, 2, 2, 2], "b": range(4), "c": range(5, 9)})

depr_msg = "The behavior of array concatenation with empty entries is deprecated"

# correct result
result1 = df.groupby("a").apply(f1)
result2 = df2.groupby("a").apply(f1)
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
result1 = df.groupby("a").apply(f1)
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
result2 = df2.groupby("a").apply(f1)
tm.assert_frame_equal(result1, result2)

# should fail (not the same number of levels)
Expand All @@ -390,7 +394,8 @@ def f3(x):
with pytest.raises(AssertionError, match=msg):
df.groupby("a").apply(f3)
with pytest.raises(AssertionError, match=msg):
df2.groupby("a").apply(f3)
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
df2.groupby("a").apply(f3)


def test_attr_wrapper(ts):
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,9 @@ def test_append_empty_preserve_name(self, name, expected):
left = Index([], name="foo")
right = Index([1, 2, 3], name=name)

result = left.append(right)
msg = "The behavior of array concatenation with empty entries is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = left.append(right)
assert result.name == expected

@pytest.mark.parametrize(
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/reshape/concat/test_append.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ def test_append_preserve_index_name(self):
df2 = DataFrame(data=[[1, 4, 7], [2, 5, 8], [3, 6, 9]], columns=["A", "B", "C"])
df2 = df2.set_index(["A"])

result = df1._append(df2)
msg = "The behavior of array concatenation with empty entries is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = df1._append(df2)
assert result.index.name == "A"

indexes_can_append = [
Expand Down
21 changes: 13 additions & 8 deletions pandas/tests/reshape/concat/test_append_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,11 +693,14 @@ def test_concat_categorical_empty(self):
s1 = Series([], dtype="category")
s2 = Series([1, 2], dtype="category")

tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), s2)
tm.assert_series_equal(s1._append(s2, ignore_index=True), s2)
msg = "The behavior of array concatenation with empty entries is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), s2)
tm.assert_series_equal(s1._append(s2, ignore_index=True), s2)

tm.assert_series_equal(pd.concat([s2, s1], ignore_index=True), s2)
tm.assert_series_equal(s2._append(s1, ignore_index=True), s2)
with tm.assert_produces_warning(FutureWarning, match=msg):
tm.assert_series_equal(pd.concat([s2, s1], ignore_index=True), s2)
tm.assert_series_equal(s2._append(s1, ignore_index=True), s2)

s1 = Series([], dtype="category")
s2 = Series([], dtype="category")
Expand All @@ -719,11 +722,13 @@ def test_concat_categorical_empty(self):

# empty Series is ignored
exp = Series([np.nan, np.nan])
tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), exp)
tm.assert_series_equal(s1._append(s2, ignore_index=True), exp)
with tm.assert_produces_warning(FutureWarning, match=msg):
tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), exp)
tm.assert_series_equal(s1._append(s2, ignore_index=True), exp)

tm.assert_series_equal(pd.concat([s2, s1], ignore_index=True), exp)
tm.assert_series_equal(s2._append(s1, ignore_index=True), exp)
with tm.assert_produces_warning(FutureWarning, match=msg):
tm.assert_series_equal(pd.concat([s2, s1], ignore_index=True), exp)
tm.assert_series_equal(s2._append(s1, ignore_index=True), exp)

def test_categorical_concat_append(self):
cat = Categorical(["a", "b"], categories=["a", "b"])
Expand Down
14 changes: 10 additions & 4 deletions pandas/tests/reshape/concat/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,8 +756,14 @@ def test_concat_ignore_empty_object_float(empty_dtype, df_dtype):
df = DataFrame({"foo": [1, 2], "bar": [1, 2]}, dtype=df_dtype)
empty = DataFrame(columns=["foo", "bar"], dtype=empty_dtype)

result = concat([empty, df])

msg = "The behavior of DataFrame concatenation with empty or all-NA entries"
warn = None
if df_dtype == "datetime64[ns]" or (
df_dtype == "float64" and empty_dtype != "float64"
):
warn = FutureWarning
with tm.assert_produces_warning(warn, match=msg):
result = concat([empty, df])
expected = df
if df_dtype == "int64":
# TODO what exact behaviour do we want for integer eventually?
Expand All @@ -782,7 +788,7 @@ def test_concat_ignore_all_na_object_float(empty_dtype, df_dtype):
else:
df_dtype = "float64"

msg = "The behavior of DataFrame concatenation with all-NA entries"
msg = "The behavior of DataFrame concatenation with empty or all-NA entries"
warn = None
if empty_dtype != df_dtype and empty_dtype is not None:
warn = FutureWarning
Expand All @@ -804,7 +810,7 @@ def test_concat_ignore_empty_from_reindex():

aligned = df2.reindex(columns=df1.columns)

msg = "The behavior of DataFrame concatenation with all-NA entries"
msg = "The behavior of DataFrame concatenation with empty or all-NA entries"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = concat([df1, aligned], ignore_index=True)
expected = df1 = DataFrame({"a": [1, 2], "b": [pd.Timestamp("2012-01-01"), pd.NaT]})
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/reshape/concat/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,9 @@ def test_concat_float_datetime64(using_array_manager):

if not using_array_manager:
expected = DataFrame({"A": pd.array(["2000"], dtype="datetime64[ns]")})
result = concat([df_time, df_float.iloc[:0]])
msg = "The behavior of DataFrame concatenation with empty or all-NA entries"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = concat([df_time, df_float.iloc[:0]])
tm.assert_frame_equal(result, expected)
else:
expected = DataFrame({"A": pd.array(["2000"], dtype="datetime64[ns]")}).astype(
Expand Down
12 changes: 8 additions & 4 deletions pandas/tests/reshape/concat/test_empty.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ def test_concat_empty_series(self):

s1 = Series([1, 2, 3], name="x")
s2 = Series(name="y", dtype="float64")
res = concat([s1, s2], axis=0)
msg = "The behavior of array concatenation with empty entries is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
res = concat([s1, s2], axis=0)
# name will be reset
exp = Series([1, 2, 3])
tm.assert_series_equal(res, exp)
Expand Down Expand Up @@ -238,9 +240,11 @@ def test_concat_inner_join_empty(self):
df_a = DataFrame({"a": [1, 2]}, index=[0, 1], dtype="int64")
df_expected = DataFrame({"a": []}, index=RangeIndex(0), dtype="int64")

for how, expected in [("inner", df_expected), ("outer", df_a)]:
result = concat([df_a, df_empty], axis=1, join=how)
tm.assert_frame_equal(result, expected)
result = concat([df_a, df_empty], axis=1, join="inner")
tm.assert_frame_equal(result, df_expected)

result = concat([df_a, df_empty], axis=1, join="outer")
tm.assert_frame_equal(result, df_a)

def test_empty_dtype_coerce(self):
# xref to #12411
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/reshape/concat/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def test_concat_empty_and_non_empty_series_regression(self):
s2 = Series([], dtype=object)

expected = s1
result = concat([s1, s2])
msg = "The behavior of array concatenation with empty entries is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = concat([s1, s2])
tm.assert_series_equal(result, expected)

def test_concat_series_axis1(self):
Expand Down
9 changes: 7 additions & 2 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,13 @@ def test_join_append_timedeltas(self, using_array_manager):
{"d": [datetime(2013, 11, 5, 5, 56)], "t": [timedelta(0, 22500)]}
)
df = DataFrame(columns=list("dt"))
df = concat([df, d], ignore_index=True)
result = concat([df, d], ignore_index=True)
msg = "The behavior of DataFrame concatenation with empty or all-NA entries"
warn = FutureWarning
if using_array_manager:
warn = None
with tm.assert_produces_warning(warn, match=msg):
df = concat([df, d], ignore_index=True)
result = concat([df, d], ignore_index=True)
expected = DataFrame(
{
"d": [datetime(2013, 11, 5, 5, 56), datetime(2013, 11, 5, 5, 56)],
Expand Down
8 changes: 6 additions & 2 deletions pandas/tests/series/methods/test_combine_first.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ def test_combine_first(self):
# corner case
ser = Series([1.0, 2, 3], index=[0, 1, 2])
empty = Series([], index=[], dtype=object)
result = ser.combine_first(empty)
msg = "The behavior of array concatenation with empty entries is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = ser.combine_first(empty)
ser.index = ser.index.astype("O")
tm.assert_series_equal(ser, result)

Expand Down Expand Up @@ -110,7 +112,9 @@ def test_combine_first_timezone_series_with_empty_series(self):
)
s1 = Series(range(10), index=time_index)
s2 = Series(index=time_index)
result = s1.combine_first(s2)
msg = "The behavior of array concatenation with empty entries is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = s1.combine_first(s2)
tm.assert_series_equal(result, s1)

def test_combine_first_preserves_dtype(self):
Expand Down