Skip to content

Commit 04282c7

Browse files
authored
BUG: casting on concat with empties (#38907)
1 parent 39e1261 commit 04282c7

File tree

5 files changed

+10
-8
lines changed

5 files changed

+10
-8
lines changed

doc/source/whatsnew/v1.3.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ Groupby/resample/rolling
296296
Reshaping
297297
^^^^^^^^^
298298
- Bug in :meth:`DataFrame.unstack` with missing levels led to incorrect index names (:issue:`37510`)
299-
- Bug in :func:`concat` incorrectly casting to ``object`` dtype in some cases when one or more of the operands is empty (:issue:`38843`)
299+
- Bug in :func:`concat` incorrectly casting to ``object`` dtype in some cases when one or more of the operands is empty (:issue:`38843`, :issue:`38907`)
300300
-
301301

302302

pandas/core/internals/concat.py

+6
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,12 @@ def _concatenate_join_units(
318318
# Concatenating join units along ax0 is handled in _merge_blocks.
319319
raise AssertionError("Concatenating join units along axis0")
320320

321+
nonempties = [
322+
x for x in join_units if x.block is None or x.block.shape[concat_axis] > 0
323+
]
324+
if nonempties:
325+
join_units = nonempties
326+
321327
empty_dtype, upcasted_na = _get_empty_dtype_and_na(join_units)
322328

323329
to_concat = [

pandas/tests/indexing/test_partial.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ def test_partial_setting_mixed_dtype(self):
154154
# columns will align
155155
df = DataFrame(columns=["A", "B"])
156156
df.loc[0] = Series(1, index=range(4))
157-
tm.assert_frame_equal(df, DataFrame(columns=["A", "B"], index=[0]))
157+
expected = DataFrame(columns=["A", "B"], index=[0], dtype=int)
158+
tm.assert_frame_equal(df, expected)
158159

159160
# columns will align
160161
df = DataFrame(columns=["A", "B"])

pandas/tests/reshape/concat/test_append.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def test_append_length0_frame(self, sort):
8282
df5 = df.append(df3, sort=sort)
8383

8484
expected = DataFrame(index=[0, 1], columns=["A", "B", "C"])
85+
expected["C"] = expected["C"].astype(np.float64)
8586
tm.assert_frame_equal(df5, expected)
8687

8788
def test_append_records(self):
@@ -340,16 +341,11 @@ def test_append_empty_frame_to_series_with_dateutil_tz(self):
340341
expected = DataFrame(
341342
[[np.nan, np.nan, 1.0, 2.0, date]], columns=["c", "d", "a", "b", "date"]
342343
)
343-
# These columns get cast to object after append
344-
expected["c"] = expected["c"].astype(object)
345-
expected["d"] = expected["d"].astype(object)
346344
tm.assert_frame_equal(result_a, expected)
347345

348346
expected = DataFrame(
349347
[[np.nan, np.nan, 1.0, 2.0, date]] * 2, columns=["c", "d", "a", "b", "date"]
350348
)
351-
expected["c"] = expected["c"].astype(object)
352-
expected["d"] = expected["d"].astype(object)
353349

354350
result_b = result_a.append(s, ignore_index=True)
355351
tm.assert_frame_equal(result_b, expected)

pandas/tests/reshape/concat/test_empty.py

-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ def test_concat_empty_df_object_dtype(self, dtype):
210210
df_2 = DataFrame(columns=df_1.columns)
211211
result = pd.concat([df_1, df_2], axis=0)
212212
expected = df_1.copy()
213-
expected["EmptyCol"] = expected["EmptyCol"].astype(object) # TODO: why?
214213
tm.assert_frame_equal(result, expected)
215214

216215
def test_concat_empty_dataframe_dtypes(self):

0 commit comments

Comments
 (0)