Skip to content

BUG: casting on concat with empties #38907

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

Merged
merged 2 commits into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ Groupby/resample/rolling
Reshaping
^^^^^^^^^
- Bug in :meth:`DataFrame.unstack` with missing levels led to incorrect index names (:issue:`37510`)
- Bug in :func:`concat` incorrectly casting to ``object`` dtype in some cases when one or more of the operands is empty (:issue:`38843`)
- 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`)
-


Expand Down
6 changes: 6 additions & 0 deletions pandas/core/internals/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,12 @@ def _concatenate_join_units(
# Concatenating join units along ax0 is handled in _merge_blocks.
raise AssertionError("Concatenating join units along axis0")

nonempties = [
x for x in join_units if x.block is None or x.block.shape[concat_axis] > 0
Copy link
Contributor

Choose a reason for hiding this comment

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

consider having a method on block for this

]
if nonempties:
join_units = nonempties

empty_dtype, upcasted_na = _get_empty_dtype_and_na(join_units)

to_concat = [
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexing/test_partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ def test_partial_setting_mixed_dtype(self):
# columns will align
df = DataFrame(columns=["A", "B"])
df.loc[0] = Series(1, index=range(4))
tm.assert_frame_equal(df, DataFrame(columns=["A", "B"], index=[0]))
expected = DataFrame(columns=["A", "B"], index=[0], dtype=int)
tm.assert_frame_equal(df, expected)

# columns will align
df = DataFrame(columns=["A", "B"])
Expand Down
6 changes: 1 addition & 5 deletions pandas/tests/reshape/concat/test_append.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def test_append_length0_frame(self, sort):
df5 = df.append(df3, sort=sort)

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

def test_append_records(self):
Expand Down Expand Up @@ -340,16 +341,11 @@ def test_append_empty_frame_to_series_with_dateutil_tz(self):
expected = DataFrame(
[[np.nan, np.nan, 1.0, 2.0, date]], columns=["c", "d", "a", "b", "date"]
)
# These columns get cast to object after append
expected["c"] = expected["c"].astype(object)
expected["d"] = expected["d"].astype(object)
tm.assert_frame_equal(result_a, expected)

expected = DataFrame(
[[np.nan, np.nan, 1.0, 2.0, date]] * 2, columns=["c", "d", "a", "b", "date"]
)
expected["c"] = expected["c"].astype(object)
expected["d"] = expected["d"].astype(object)

result_b = result_a.append(s, ignore_index=True)
tm.assert_frame_equal(result_b, expected)
Expand Down
1 change: 0 additions & 1 deletion pandas/tests/reshape/concat/test_empty.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ def test_concat_empty_df_object_dtype(self, dtype):
df_2 = DataFrame(columns=df_1.columns)
result = pd.concat([df_1, df_2], axis=0)
expected = df_1.copy()
expected["EmptyCol"] = expected["EmptyCol"].astype(object) # TODO: why?
tm.assert_frame_equal(result, expected)

def test_concat_empty_dataframe_dtypes(self):
Expand Down