-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CHORE] connect, test:
df.withColumn
(#3359)
- Loading branch information
1 parent
e1c3faf
commit 33baba8
Showing
1 changed file
with
19 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from __future__ import annotations | ||
|
||
from pyspark.sql.functions import col | ||
|
||
|
||
def test_with_column(spark_session): | ||
# Create DataFrame from range(10) | ||
df = spark_session.range(10) | ||
|
||
# Add a new column that's a boolean indicating if id > 2 | ||
df_with_col = df.withColumn("double_id", col("id") > 2) | ||
|
||
# Verify the schema has both columns | ||
assert "id" in df_with_col.schema.names, "Original column should still exist" | ||
assert "double_id" in df_with_col.schema.names, "New column should be added" | ||
|
||
# Verify the data is correct | ||
df_pandas = df_with_col.toPandas() | ||
assert (df_pandas["double_id"] == (df_pandas["id"] > 2)).all(), "New column should be greater than 2 comparison" |