Closed
Description
It seems that when using concat
coerces Series
of type int
to type float
if one of the DataFrame
s being concatted is empty:
>>> df1 = pd.DataFrame([1],columns=['a'])
>>> df2 = pd.DataFrame(columns=['a'])
>>> df = pd.concat([df1, df2])
>>> df['a'].dtype
dtype('float64')
>>> df1['a'].dtype
dtype('int64')
While if both columns have ints, no coercion happens:
>>> df1 = pd.DataFrame([1],columns=['a'])
>>> df2 = pd.DataFrame([1],columns=['a'])
>>> df = pd.concat([df1, df2])
>>> df['a'].dtype
dtype('int64')
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
jreback commentedon Nov 26, 2014
cc @immerrr
This is actually somewhat of a grey area. If you had passed a
None
, then it would be excluded and so no coercion happens. The fact that you passed a completely empty frame, which isobject
dtype is the issue. Technically this should coerce, though I can see the case for 'ignoring' it. Since it doesn't have any valid shape.IOW, I could see ignoring these in the pre-computation.
immerrr commentedon Nov 27, 2014
I agree, it should coerce technically, but not practically.
immerrr commentedon Nov 27, 2014
I have a lot on my plate right now, but if it waits till I'm done with that, I'll fix this.
a-y-khan commentedon Apr 5, 2020
In recent Pandas version (v1.0.3), concat coerces ints to
object
instead of floats:mroeschke commentedon Apr 11, 2021
I think the above behavior was an intentional behavior change a while back (that an empty DataFrame has an
object
dtype`); therefore, a common dtype between object and int is object.Closing as the new expected behavior, but happy to reopen if I am misunderstanding