BUG: Appending or concatenating to empty ExtensionArray removes type information #48510
Closed
2 of 3 tasks
Labels
Dtype Conversions
Unexpected or buggy dtype conversions
ExtensionArray
Extending pandas with custom dtypes or arrays.
good first issue
Needs Tests
Unit test(s) needed to prevent regressions
Reshaping
Concat, Merge/Join, Stack/Unstack, Explode
Milestone
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
When appending a dataframe (
df_other
) to another dataframe (df
) which has an empty column of typeExtensionDtype
(in this caseInt64Dtype
, but the specific EA type doesn't matter), then the resulting dataframe's column (df2['a']
) loses the dtype information and turns into an object dtype.You can run the example with
arr = [1]
instead of the empty list (arr = []
) and observe that - as expected - the type is not changed and remainsInt64Dtype
.I traced the issue to
_concatenate_join_units
and_get_empty_dtype
which ignores type information when the column is empty (if not unit.is_na
). This in turn then fails to enter theelif any(is_1d_only_ea_obj(t) for t in to_concat)
EA handling branch in_concatenate_join_units
.Expected Behavior
Type information remains as both types are compatible (the fact that one Series is empty shouldn't matter).
Installed Versions
INSTALLED VERSIONS
commit : ca60aab
python : 3.8.13.final.0
python-bits : 64
OS : Linux
OS-release : 5.19.8-200.fc36.x86_64
Version : #1 SMP PREEMPT_DYNAMIC Thu Sep 8 19:02:21 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_AU.UTF-8
LOCALE : en_AU.UTF-8
pandas : 1.4.4
numpy : 1.23.2
pytz : 2020.4
dateutil : 2.8.1
setuptools : 59.6.0
pip : 22.2.2
Cython : 0.29.32
pytest : 7.1.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 0.9.6
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.8.6
jinja2 : 2.11.2
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : 1.3.5
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : 1.1.1
matplotlib : None
numba : None
numexpr : 2.8.1
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 1.0.1
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
snappy : None
sqlalchemy : 1.3.23
tables : 3.7.0
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : None
zstandard : None
The text was updated successfully, but these errors were encountered: