TypeError: int() argument must be a string, a bytes-like object or a real number, not…
- User
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'Expr'
def add_features(df): df_clone = df.clone() df_clone = df_clone.with_columns([
# Разница в трофеях (pl.col("player_1_trophies") - pl.col("player_2_trophies")).alias("trophy_diff"), # Среднее арифметическое трофеев ((pl.col("player_1_trophies") + pl.col("player_2_trophies")) / 2).alias("avg_trophies"), # Первый игрок имеет больше корон хотя бы на 30 int((pl.col("player_1_trophies") - pl.col("player_2_trophies")) >= 30).alias("is_first_better"), # Второй игрок имеет больше корон хотя бы на 30 int((pl.col("player_2_trophies") - pl.col("player_1_trophies")) >= 30).alias("is_second_better") ]) return df_clone Assistant
The error occurs because you're trying to convert a Polars expression directly to an integer using
int(). In Polars, you need to use the.cast()method to convert expressions to different types.Here's the corrected version of your code:
def add_features(df): df_clone = df.clone() df_clone = df_clone.with_columns([ # Разница в трофеях (pl.col("player_1_trophies") - pl.col("player_2_trophies")).alias("trophy_diff"), # Среднее арифметическое трофеев ((pl.col("player_1_trophies") + pl.col("player_2_trophies")) / 2).alias("avg_trophies"), # Первый игрок имеет больше корон хотя бы на 30 ((pl.col("player_1_trophies") - pl.col("player_2_trophies")) >= 30).cast(pl.Int32).alias("is_first_better"), # Второй игрок имеет больше корон хотя бы на 30 ((pl.col("player_2_trophies") - pl.col("player_1_trophies")) >= 30).cast(pl.Int32).alias("is_second_better") ]) return df_cloneThe key changes are:
- Replaced
int()with.cast(pl.Int32) - The boolean comparison will now be properly converted to 0s and 1s
This should resolve the TypeError you're encountering. The
.cast(pl.Int32)method properly converts the boolean expression to integers within the Polars framework.- Replaced
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.