Skip to content

Commit

Permalink
fix: normalize column names checking for matches (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
eakmanrq authored May 24, 2024
1 parent 1441d0a commit 7b220a8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 4 additions & 3 deletions sqlframe/base/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1093,15 +1093,16 @@ def replace(
@operation(Operation.SELECT)
def withColumn(self, colName: str, col: Column) -> Self:
col = self._ensure_and_normalize_col(col)
col_name = self._ensure_and_normalize_col(colName).alias_or_name
existing_col_names = self.expression.named_selects
existing_col_index = (
existing_col_names.index(colName) if colName in existing_col_names else None
existing_col_names.index(col_name) if col_name in existing_col_names else None
)
if existing_col_index:
expression = self.expression.copy()
expression.expressions[existing_col_index] = col.alias(colName).expression
expression.expressions[existing_col_index] = col.alias(col_name).expression
return self.copy(expression=expression)
return self.copy().select(col.alias(colName), append=True)
return self.copy().select(col.alias(col_name), append=True)

@operation(Operation.SELECT)
def withColumnRenamed(self, existing: str, new: str) -> Self:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/standalone/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_persist_storagelevel(standalone_employee: StandaloneDataFrame, compare_


def test_with_column_duplicate_alias(standalone_employee: StandaloneDataFrame):
df = standalone_employee.withColumn("fname", F.col("age").cast("string"))
df = standalone_employee.withColumn("fName", F.col("age").cast("string"))
assert df.columns == ["employee_id", "fname", "lname", "age", "store_id"]
# Make sure that the new columns is added with an alias to `fname`
assert (
Expand Down

0 comments on commit 7b220a8

Please sign in to comment.