Programmer-RD-AI commented on code in PR #54588:
URL: https://github.com/apache/airflow/pull/54588#discussion_r2282730657
##########
providers/amazon/src/airflow/providers/amazon/aws/transfers/sql_to_s3.py:
##########
@@ -261,38 +261,57 @@ def execute(self, context: Context) -> None:
file_obj=buf, key=object_key, bucket_name=self.s3_bucket,
replace=self.replace
)
- def _partition_dataframe(self, df: pd.DataFrame | pl.DataFrame) ->
Iterable[tuple[str, pd.DataFrame]]:
- """Partition dataframe using pandas groupby() method."""
+ def _partition_dataframe(
+ self, df: pd.DataFrame | pl.DataFrame
+ ) -> Iterable[tuple[str, pd.DataFrame | pl.DataFrame]]:
+ """Partition dataframe using pandas or polars groupby() method."""
try:
import secrets
import string
import numpy as np
+ import pandas as pd
import polars as pl
except ImportError:
pass
- if isinstance(df, pl.DataFrame):
- df = df.to_pandas()
-
# if max_rows_per_file argument is specified, a temporary column with
a random unusual name will be
# added to the dataframe. This column is used to dispatch the
dataframe into smaller ones using groupby()
-
- random_column_name = ""
+ random_column_name = None
if self.max_rows_per_file and not self.groupby_kwargs:
random_column_name = "".join(secrets.choice(string.ascii_letters)
for _ in range(20))
- df[random_column_name] = np.arange(len(df)) //
self.max_rows_per_file
self.groupby_kwargs = {"by": random_column_name}
+
+ if isinstance(df, pd.DataFrame):
+ if random_column_name:
+ df[random_column_name] = np.arange(len(df)) //
self.max_rows_per_file
+ elif isinstance(df, pl.DataFrame):
+ if random_column_name:
+ df = df.with_columns(
+ (pl.int_range(pl.len()) //
self.max_rows_per_file).alias(random_column_name)
+ )
Review Comment:
Instead of re-checking random_column_name inside each branch, we could check
it once up front and then just handle the pandas/polars logic. Keeps the flow
cleaner and avoids duplicate conditionals.
```python
if random_column_name:
if isinstance(df, pd.DataFrame):
df[random_column_name] = np.arange(len(df)) // self.max_rows_per_file
elif isinstance(df, pl.DataFrame):
df = df.with_columns(
(pl.int_range(pl.len()) //
self.max_rows_per_file).alias(random_column_name)
)
```
--
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]