Skip to content
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

BUG: combine_first raise FutureWarning surprisingly #58304

Closed
3 tasks done
xyb opened this issue Apr 18, 2024 · 4 comments
Closed
3 tasks done

BUG: combine_first raise FutureWarning surprisingly #58304

xyb opened this issue Apr 18, 2024 · 4 comments
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@xyb
Copy link

xyb commented Apr 18, 2024

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

import warnings
import pandas as pd

with warnings.catch_warnings(record=True) as w:
    df = pd.DataFrame({'A': [1]})
    df2 = pd.DataFrame({'A': [None]})
    df['A'].combine_first(df2['A'])
    assert len(w) == 1
    assert w[0].category.__name__ == 'FutureWarning'
    assert x.message.args == ('The behavior of array concatenation with empty entries is deprecated. In a future version, this will no longer exclude empty items when determining the result dtype. To retain the old behavior, exclude the empty entries before the concat operation.',)

Issue Description

The concat function in pandas is designed to emit a FutureWarning when attempting to concatenate an empty DataFrame, which is a logical consequence of its intended behavior. However, there is an inconsistency with the combine_first method. It occasionally generates the same FutureWarning associated with concatenation, in a manner that may seem perplexing, even when both DataFrames involved in the operation are clearly non-empty. This anomaly can present challenges in preemptively suppressing the warning within one's code without affecting the overall functionality.

Expected Behavior

Avoid confusing warnings

Installed Versions

INSTALLED VERSIONS

commit : fd3f571
python : 3.11.6.final.0
python-bits : 64
OS : Darwin
OS-release : 21.6.0
Version : Darwin Kernel Version 21.6.0: Wed Aug 10 14:25:27 PDT 2022; root:xnu-8020.141.5~2/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US
LOCALE : en_US.UTF-8

pandas : 2.2.0
numpy : 1.25.2
pytz : 2022.7.1
dateutil : 2.8.2
setuptools : 67.4.0
pip : 23.0.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.12.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.2
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2023.9.2
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 15.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@xyb xyb added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 18, 2024
@Aloqeely
Copy link
Member

Thanks for the report! It is intended to emit a FutureWarning here.
On pandas 2.2.2 the resulting dtype of df['A'].combine_first(df2['A']) is int64, so it didn't take into account the dtype of the empty value None, whereas in main this produces object dtype.

You can see more here: #39122

@xyb
Copy link
Author

xyb commented Apr 23, 2024

@Aloqeely I understand that the FutureWarning for concat makes sense.
However, for combine_first, since the merging algorithm is internal to combine_first, it is difficult for the caller to ensure that a FutureWarning will not be triggered before invoking it; if the caller can manage this, then it would be equivalent to implementing combine_first themselves.
In the online environment logs and unit test reports of my system, there are numerous FutureWarnings generated due to calls to combine_first, which greatly interferes with system maintenance and code debugging.

@Aloqeely
Copy link
Member

Sorry I am not sure I understand you here.
But the good news is that the warning does not occur on the main branch so this should be solved in v3.0.0

@xyb
Copy link
Author

xyb commented Apr 25, 2024

@Aloqeely That's indeed great news! Thank you for your patience.

@xyb xyb closed this as completed Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

2 participants