Skip to content

Commit cf095e1

Browse files
Partial Revert "BUG/API: concat with empty DataFrames or all-NA columns (#43507)"
This reverts commit 084c543.
1 parent 978c1eb commit cf095e1

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

pandas/core/internals/concat.py

+33-2
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,26 @@
3131
is_1d_only_ea_dtype,
3232
is_datetime64tz_dtype,
3333
is_dtype_equal,
34+
is_scalar,
3435
needs_i8_conversion,
3536
)
3637
from pandas.core.dtypes.concat import (
3738
cast_to_common_type,
3839
concat_compat,
3940
)
4041
from pandas.core.dtypes.dtypes import ExtensionDtype
41-
from pandas.core.dtypes.missing import is_valid_na_for_dtype
42+
from pandas.core.dtypes.missing import (
43+
is_valid_na_for_dtype,
44+
isna,
45+
isna_all,
46+
)
4247

4348
import pandas.core.algorithms as algos
4449
from pandas.core.arrays import (
4550
DatetimeArray,
4651
ExtensionArray,
4752
)
53+
from pandas.core.arrays.sparse import SparseDtype
4854
from pandas.core.construction import ensure_wrapped_if_datetimelike
4955
from pandas.core.internals.array_manager import (
5056
ArrayManager,
@@ -419,7 +425,29 @@ def is_na(self) -> bool:
419425
blk = self.block
420426
if blk.dtype.kind == "V":
421427
return True
422-
return False
428+
429+
if not blk._can_hold_na:
430+
return False
431+
432+
values = blk.values
433+
if values.size == 0:
434+
return True
435+
if isinstance(values.dtype, SparseDtype):
436+
return False
437+
438+
if values.ndim == 1:
439+
# TODO(EA2D): no need for special case with 2D EAs
440+
val = values[0]
441+
if not is_scalar(val) or not isna(val):
442+
# ideally isna_all would do this short-circuiting
443+
return False
444+
return isna_all(values)
445+
else:
446+
val = values[0][0]
447+
if not is_scalar(val) or not isna(val):
448+
# ideally isna_all would do this short-circuiting
449+
return False
450+
return all(isna_all(row) for row in values)
423451

424452
def get_reindexed_values(self, empty_dtype: DtypeObj, upcasted_na) -> ArrayLike:
425453
values: ArrayLike
@@ -567,6 +595,9 @@ def _dtype_to_na_value(dtype: DtypeObj, has_none_blocks: bool):
567595
# different from missing.na_value_for_dtype
568596
return None
569597
elif dtype.kind in ["i", "u"]:
598+
if not has_none_blocks:
599+
# different from missing.na_value_for_dtype
600+
return None
570601
return np.nan
571602
elif dtype.kind == "O":
572603
return np.nan

pandas/tests/frame/methods/test_append.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def test_append_empty_dataframe(self):
159159
expected = df1.copy()
160160
tm.assert_frame_equal(result, expected)
161161

162-
def test_append_dtypes(self):
162+
def test_append_dtypes(self, using_array_manager):
163163

164164
# GH 5754
165165
# row appends of different dtypes (so need to do by-item)
@@ -183,7 +183,10 @@ def test_append_dtypes(self):
183183
expected = DataFrame(
184184
{"bar": Series([Timestamp("20130101"), np.nan], dtype="M8[ns]")}
185185
)
186-
expected = expected.astype(object)
186+
if using_array_manager:
187+
# TODO(ArrayManager) decide on exact casting rules in concat
188+
# With ArrayManager, all-NaN float is not ignored
189+
expected = expected.astype(object)
187190
tm.assert_frame_equal(result, expected)
188191

189192
df1 = DataFrame({"bar": Timestamp("20130101")}, index=range(1))
@@ -192,7 +195,9 @@ def test_append_dtypes(self):
192195
expected = DataFrame(
193196
{"bar": Series([Timestamp("20130101"), np.nan], dtype="M8[ns]")}
194197
)
195-
expected = expected.astype(object)
198+
if using_array_manager:
199+
# With ArrayManager, all-NaN float is not ignored
200+
expected = expected.astype(object)
196201
tm.assert_frame_equal(result, expected)
197202

198203
df1 = DataFrame({"bar": np.nan}, index=range(1))
@@ -201,7 +206,9 @@ def test_append_dtypes(self):
201206
expected = DataFrame(
202207
{"bar": Series([np.nan, Timestamp("20130101")], dtype="M8[ns]")}
203208
)
204-
expected = expected.astype(object)
209+
if using_array_manager:
210+
# With ArrayManager, all-NaN float is not ignored
211+
expected = expected.astype(object)
205212
tm.assert_frame_equal(result, expected)
206213

207214
df1 = DataFrame({"bar": Timestamp("20130101")}, index=range(1))

pandas/tests/reshape/merge/test_merge.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ def _constructor(self):
682682

683683
assert isinstance(result, NotADataFrame)
684684

685-
def test_join_append_timedeltas(self):
685+
def test_join_append_timedeltas(self, using_array_manager):
686686
# timedelta64 issues with join/merge
687687
# GH 5695
688688

@@ -696,9 +696,11 @@ def test_join_append_timedeltas(self):
696696
{
697697
"d": [datetime(2013, 11, 5, 5, 56), datetime(2013, 11, 5, 5, 56)],
698698
"t": [timedelta(0, 22500), timedelta(0, 22500)],
699-
},
700-
dtype=object,
699+
}
701700
)
701+
if using_array_manager:
702+
# TODO(ArrayManager) decide on exact casting rules in concat
703+
expected = expected.astype(object)
702704
tm.assert_frame_equal(result, expected)
703705

704706
def test_join_append_timedeltas2(self):

0 commit comments

Comments
 (0)