Skip to content

BUG: concat warning when joining with empty and columns have different dtype #62949

@rhshadrach

Description

@rhshadrach

The warning is due to #39122

pd.set_option("infer_string", True)
df = pd.DataFrame({"a": "x", "b": ["y", "z"]}).set_index(["a", "b"])
df.columns = pd.Index([], dtype="int")
ser = pd.DataFrame({"b": ["y", "z"], "value": [1, 2]}).set_index("b")["value"]
df.join(ser, on="b")
# FutureWarning: The behavior of array concatenation with empty entries is deprecated...

In the above code, we hit

result = concat([left, right], axis=1)

with left having no columns but of int64 dtype and right having columns with str dtype. Both before and after the deprecation we get str dtype for the columns. The call to pandas.core.dtypes.concat.concat_compact does result in object dtype, but then we wrap this with Index._with_infer(...) in Index._concat giving str again.

Should we be inferring dtype after the concat index is determined?

In merge, it seems to me we should skip calling concat with axis=1 if left or right has empty columns but perhaps still call concat if both are empty.

cc @jbrockmendel @jorisvandenbossche

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds DiscussionRequires discussion from core team before further actionReshapingConcat, Merge/Join, Stack/Unstack, ExplodeWarningsWarnings that appear or should be added to pandas

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions