Repository navigation
Conversation
Co-Authored-By: Claude Code <noreply@anthropic.com>
cmp0xff
requested changes
Oct 12, 2026
Comment on lines
+4639
to
+4672
| def test_flexible_comparisons_scalar() -> None: | ||
| """Test DataFrame flexible comparison methods accept scalar values.""" | ||
| df_str = pd.DataFrame({"A": ["foo", "bar"]}) | ||
| check(assert_type(df_str.eq("foo"), pd.DataFrame), pd.DataFrame) | ||
| check(assert_type(df_str.ne("foo"), pd.DataFrame), pd.DataFrame) | ||
| check(assert_type(df_str.lt("m"), pd.DataFrame), pd.DataFrame) | ||
| check(assert_type(df_str.le("m"), pd.DataFrame), pd.DataFrame) | ||
| check(assert_type(df_str.gt("m"), pd.DataFrame), pd.DataFrame) | ||
| check(assert_type(df_str.ge("m"), pd.DataFrame), pd.DataFrame) | ||
|
|
||
| df_num = pd.DataFrame({"A": [1, 2], "B": [3, 4]}) | ||
| check(assert_type(df_num.eq(1), pd.DataFrame), pd.DataFrame) | ||
| check(assert_type(df_num.gt(1.5), pd.DataFrame), pd.DataFrame) | ||
| check(assert_type(df_num.le(True), pd.DataFrame), pd.DataFrame) | ||
|
|
||
| ts = pd.Timestamp("2026-01-01") | ||
| df_ts = pd.DataFrame({"t": [ts]}) | ||
| check(assert_type(df_ts.lt(ts), pd.DataFrame), pd.DataFrame) | ||
|
|
||
| td = pd.Timedelta(days=1) | ||
| df_td = pd.DataFrame({"d": [td]}) | ||
| check(assert_type(df_td.ge(td), pd.DataFrame), pd.DataFrame) | ||
|
|
||
| dt = datetime.datetime(2026, 1, 1) | ||
| check(assert_type(df_ts.ne(dt), pd.DataFrame), pd.DataFrame) | ||
|
|
||
| # list-like and DataFrame operands remain supported | ||
| df = pd.DataFrame({"A": ["foo", "bar"], "B": [1, 2]}) | ||
| check(assert_type(df.eq(["foo", 1]), pd.DataFrame), pd.DataFrame) | ||
| check(assert_type(df.eq(df), pd.DataFrame), pd.DataFrame) | ||
| check(assert_type(df.ne(pd.Series(["x", "y"])), pd.DataFrame), pd.DataFrame) | ||
|
|
||
| if TYPE_CHECKING_INVALID_USAGE: | ||
| _0 = df_num.eq(object()) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] # pyrefly: ignore[bad-argument-type] # ty: ignore[invalid-argument-type] |
Contributor
There was a problem hiding this comment.
Please move them next to test_types_df_to_df_comparison near line 212, where comparison tests already live
Contributor
|
Hi, would you continue to work on this? |
Contributor
|
Please also merge |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
assert_type()to assert the type of any return value)AGENTS.md.Annotates
otherasScalar | ListLike | DataFramein the sixDataFrameflexible comparison methods, matching the documented behavior ("scalar, sequence, Series, or DataFrame") and the existingSeries.eqannotation. Arithmetic methods are unchanged, since e.g.df + "foo"is not valid at runtime.Tests cover
str,int,float,bool,Timestamp,Timedeltaanddatetimescalar operands plus list-like,SeriesandDataFrameoperands, and guard against a non-scalar object.Validated with
poe test: mypy, pyright, pyrefly, pytest (3409 passed) and style all clean;tyreports only the six pre-existing diagnostics onmainfrom ty 0.0.86 (none in the files touched here).AI Implementation Plan
Per AGENTS.md, a comprehensive implementation record for agents:
Motivation: #1978 —
DataFrame.eq/ne/le/lt/ge/gttypedother: complex | ListLike | DataFrame, rejecting valid runtime scalars (str,bool,Timestamp, ...). The docs describeotheras "scalar, sequence, Series, or DataFrame" andSeries.eqalready usesScalar | Series[S1].Approach: replace
complexwithScalarin exactly the six comparison signatures inpandas-stubs/core/frame.pyi.Scalaris a strict superset ofcomplex(adds the_IndexIterScalarmembers:str,bytes, date/time types,bool,int,float,Timestamp,Timedelta, plusnpscalars), so the change is backward compatible.Experiments and attempts:
main; only the flexible comparisons rejected valid runtime input;df.eq(["foo", 1]),df.eq(df)etc. were already accepted and are kept as regression tests.lt("m"); pandas raisesTypeError: Invalid comparison between dtype=int64 and strat runtime (ordering comparisons do not fall back to all-False like equality). Fixed by testing ordering comparisons against homogeneous string frames and numeric comparisons against numeric frames.object(), which is outsideScalar | ListLike | DataFramefor all four checkers.Caveats:
Scalaris wider than the set of dtypes a given frame can meaningfully compare with; e.g.df_num.lt("m")type-checks but raises at runtime. This matches theSeriesside and the docs, which defer elementwise comparison semantics to runtime.Validation:
poe mypy/pyright/pyrefly/pytest/styleall pass (pytest: 3409 passed, 5 skipped).poe tyfails with the same sixinvalid-property-type-overridediagnostics as pristinemain(ty 0.0.86, arrays/*.pyi only; unrelated to this change).Out of scope: widening arithmetic operators (invalid at runtime);
Seriesflexible comparisons (already accept scalars);fill_valuetyping for string dtypes (tracked in #1797).