paleolimbot commented on code in PR #835:
URL: https://github.com/apache/sedona-db/pull/835#discussion_r3227308210


##########
python/sedonadb/tests/expr/test_dataframe_filter.py:
##########
@@ -0,0 +1,138 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Tests for DataFrame.filter() / .where(). Each test builds its own input
+# DataFrame inline so the full context of a failure is visible in the
+# failing test function. Output is compared with
+# `pd.testing.assert_frame_equal`, which gives row/column diagnostics on
+# mismatch. Index is reset on the materialized output because pandas
+# preserves the original positions after a filter and we want to compare
+# logical contents.
+
+import pandas as pd
+import pandas.testing as pdt
+import pytest
+
+from sedonadb._lib import SedonaError
+from sedonadb.expr import col, lit
+
+
+def _xy_df(con):
+    return con.create_data_frame(
+        pd.DataFrame({"x": [1, 2, 3, 4], "y": [10, 20, 30, 40]})
+    )

Review Comment:
   Can you inline this into each test? Probably `pd.DataFrame({"x": [1, 2, 
3]})` would be sufficient.



##########
python/sedonadb/python/sedonadb/dataframe.py:
##########
@@ -141,6 +141,65 @@ def select(self, *exprs: "Expr | str | _SedonaLit") -> 
"DataFrame":
                 )
         return DataFrame(self._ctx, self._impl.select(coerced), self._options)
 
+    def filter(self, *exprs: "Expr") -> "DataFrame":
+        """Filter rows by one or more boolean expressions.
+
+        Multiple expressions are combined with logical AND, so
+        `df.filter(a, b)` is equivalent to `df.filter(a & b)` and to
+        `df.filter(a).filter(b)` (the planner sees one conjunction in
+        the first two forms and two filter nodes in the third).
+
+        Only `Expr` arguments are accepted. Strings are not interpreted
+        as SQL predicates (that is a separate feature). Bare `Literal`
+        values are also rejected — `filter(lit(True))` is almost
+        certainly a typo; if you really mean a constant predicate, wrap
+        a column expression like `col("flag") == lit(True)`.
+
+        Args:
+            *exprs: One or more boolean `sedonadb.expr.Expr` predicates.
+                At least one argument is required.
+
+        Examples:
+
+            >>> from sedonadb.expr import col
+            >>> sd = sedona.db.connect()
+            >>> df = sd.sql("SELECT * FROM (VALUES (1), (2), (3), (4)) AS 
t(x)")
+            >>> df.filter(col("x") > 2).show()
+            ┌───────┐
+            │   x   │
+            │ int64 │
+            ╞═══════╡
+            │     3 │
+            ├╌╌╌╌╌╌╌┤
+            │     4 │
+            └───────┘
+        """
+        from sedonadb.expr import Expr, Literal
+
+        if not exprs:
+            raise ValueError("filter() requires at least one predicate")
+
+        for e in exprs:
+            if isinstance(e, Literal):
+                raise TypeError(
+                    "filter() does not accept Literal arguments. "
+                    "filter(lit(value)) is almost always a typo; if you really 
"
+                    "want a constant predicate, wrap a column expression like "
+                    "col('flag') == lit(value)."
+                )
+            if not isinstance(e, Expr):
+                raise TypeError(
+                    f"filter() expects Expr arguments, got {type(e).__name__}"
+                )
+
+        return DataFrame(
+            self._ctx,
+            self._impl.filter([e._impl for e in exprs]),
+            self._options,
+        )
+
+    where = filter

Review Comment:
   Can we not add an alias for filter here? We can specifically add a pyspark 
compatibility later at a different time if there is interest.
   
   ```suggestion
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to