Repository navigation
Support pandas 3+ in the dataframes API (#40709) - #40768
dhanusharer wants to merge 2 commits into
Conversation
* Enable pandas 3.x dependency on Python 3.11+ via PEP 508 environment markers * Update tox.ini and Gradle build configs to test pandas 3.0 in py311 * Add defensive attribute checking in frame_base decorators (args_to_kwargs, populate_defaults, with_docs_from) * Support pandas 3.0 Copy-on-Write defaults * Update fillna, groupby, first, last, and applymap to handle removed pandas 3.0 arguments and methods * Add pandas 3.0 string and datetime dtype recognition in pandas_type_compatibility * Update frames_test.py to support pandas 3.0
There was a problem hiding this comment.
📋 Review Summary
This PR updates the DataFrame API, schema conversion utilities, and type compatibility test suites to support Pandas 3.0 (where infer_string is enabled by default) and adds py311-pandas-30 to tox.ini. While the core Pandas 3.0 compatibility fixes are solid, the setup.py package bounds and Gradle test tasks are not updated yet, and one schema conversion path still relies on dtype.kind == 'O' instead of pd.api.types.is_string_dtype.
🔍 General Feedback
- Dependency & CI Wiring (
setup.py&build.gradle):sdks/python/setup.pystill capsdataframe_dependencyat'pandas>=2.2.2,<2.4'(with# TODO(https://github.com/apache/beam/issues/40709): support pandas 3+) anddocs/testextras at'pandas<2.6.0'. Additionally,sdks/python/test-suites/tox/py311/build.gradleneeds atestPy311pandas-30task registered so the newpy311-pandas-30tox environment is actually executed inpostCommitPyDep(and.github/trigger_files/beam_PostCommit_Python_Dependency.jsoncan be bumped to trigger it on the PR). - Remaining
dtype.kind == 'O'check inschemas.py:sdks/python/apache_beam/dataframe/schemas.py(line 206 inelement_type_from_dataframe) still checksif dtype.kind == 'O': return Anywhen inferring schema types from a proxy withoutelement_type. Under Pandas 3.0, default string columns havepd.StringDtype(na_value=np.nan)(dtype.kind == 'U'), which bypasses that branch and falls through todtype_to_fieldtype(raisingTypeError: Unable to encounter corresponding Beam type for dtype=str). Updating that check toif dtype.kind == 'O' or pd.api.types.is_string_dtype(dtype):(or handlingpd.api.types.is_string_dtypeinpandas_type_compatibility.py) will keep string column fallback consistent.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
💬 Inline Findings
sdks/python/tox.ini(line 376): 🟠 High:py311-pandas-30is added here intox.ini, but two other places need to be updated in tandem:
sdks/python/test-suites/tox/py311/build.gradleneeds a correspondingtoxTask "testPy311pandas-30", "py311-pandas-30", "${posargs}"(added totestandpostCommitPyDep) so that CI actually runs this tox environment.sdks/python/setup.pystill restrictsdataframe_dependencyto'pandas>=2.2.2,<2.4'(anddocs/testextras to'pandas<2.6.0'). Since tox installs the package withextras = test,dataframe,..., pip will either report a version conflict incommands_pre(pip check) or prevent users from installing Pandas 3.0 withapache-beam[dataframe].
sdks/python/apache_beam/dataframe/schemas_test.py(line 227): 🟡 Medium: Whenmajor >= 3,series_testsis reassigned instead of appended to (+=), which drops all the numeric and nullable integer Series test cases defined in lines 211–218 on Pandas 3+.
series_tests += [(pd.Series(['a', 'b', 'c'], dtype=dtype), str)]
…commit dependency CI
|
/gemini review |
|
Assigning reviewers: R: @claudevdm for label python. This pull request likely touches a core component ("core" label). Please review with scrutiny. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
Fixes #40709
Summary of Changes
pandas>=2.2.2,<3.2for Python>=3.11while preservingpandas<2.4for Python 3.10 using PEP 508 environment markers. Addedpy311-pandas-30tox environment and Gradle task.hasattr()checks inframe_base.pydecorators (@args_to_kwargs,@populate_defaults,@with_docs_from) to prevent import-time reflection crashes when pandas 3.0 removes deprecated methods or parameters. Handled permanent Copy-on-Write defaults.removed_args=['method']and avoided passingmethodtofillna()under pandas 3.0.removed_args=['axis']togroupby()and raised cleanNotImplementedErrorfor column-wise groupby.first,last, andapplymapwithhasattr()checks.fillna,first,last) withhasattr(DataFrameGroupBy, ...).method=Nonefrom_DeferredStringMethods.get_dummiesfillna call.str,pd.StringDtype, and datetime dtypes indtype_to_fieldtype().test_applymap_na_actionto skip on pandas 3.0+ and added pandas 3.0ffill/bfillbranch totest_fillna_columns.