File tree Expand file tree Collapse file tree 3 files changed +40
-2
lines changed Expand file tree Collapse file tree 3 files changed +40
-2
lines changed Original file line number Diff line number Diff line change 1
1
from __future__ import annotations
2
2
3
3
import contextlib
4
- from collections .abc import Iterable
4
+ from collections .abc import Iterable , Mapping
5
5
from typing import TYPE_CHECKING , Any
6
6
7
7
import polars ._reexport as pl
@@ -120,6 +120,18 @@ def _parse_inputs_as_iterable(
120
120
if not inputs :
121
121
return []
122
122
123
+ # Ensures that the outermost element cannot be a Dictionary (as an iterable)
124
+ if len (inputs ) == 1 and isinstance (inputs [0 ], Mapping ):
125
+ msg = (
126
+ "Cannot pass a dictionary as a single positional argument.\n "
127
+ "If you merely want the *keys*, use:\n "
128
+ " • df.method(*your_dict.keys())\n "
129
+ "If you need the key value pairs, use one of:\n "
130
+ " • unpack as keywords: df.method(**your_dict)\n "
131
+ " • build expressions: df.method(expr.alias(k) for k, expr in your_dict.items())"
132
+ )
133
+ raise TypeError (msg )
134
+
123
135
# Treat elements of a single iterable as separate inputs
124
136
if len (inputs ) == 1 and _is_iterable (inputs [0 ]):
125
137
return inputs [0 ]
Original file line number Diff line number Diff line change @@ -3249,3 +3249,29 @@ def test_nan_to_null() -> None:
3249
3249
)
3250
3250
3251
3251
assert_frame_equal (df1 , df2 )
3252
+
3253
+
3254
+ # Below 3 tests for https://github.com/pola-rs/polars/issues/17879
3255
+
3256
+
3257
+ def test_with_columns_dict_direct_typeerror () -> None :
3258
+ data = {"a" : pl .col ("a" ) * 2 }
3259
+ df = pl .select (a = 1 )
3260
+ with pytest .raises (
3261
+ TypeError , match = "Cannot pass a dictionary as a single positional argument"
3262
+ ):
3263
+ df .with_columns (data )
3264
+
3265
+
3266
+ def test_with_columns_dict_unpacking () -> None :
3267
+ data = {"a" : pl .col ("a" ) * 2 }
3268
+ df = pl .select (a = 1 ).with_columns (** data )
3269
+ expected = pl .DataFrame ({"a" : [2 ]})
3270
+ assert df .equals (expected )
3271
+
3272
+
3273
+ def test_with_columns_generator_alias () -> None :
3274
+ data = {"a" : pl .col ("a" ) * 2 }
3275
+ df = pl .select (a = 1 ).with_columns (expr .alias (name ) for name , expr in data .items ())
3276
+ expected = pl .DataFrame ({"a" : [2 ]})
3277
+ assert df .equals (expected )
Original file line number Diff line number Diff line change @@ -356,7 +356,7 @@ def test_projection_join_names_9955() -> None:
356
356
how = "inner" ,
357
357
)
358
358
359
- q = q .select (batting .collect_schema ())
359
+ q = q .select (* batting .collect_schema (). keys ())
360
360
361
361
assert q .collect ().schema == {
362
362
"playerID" : pl .String ,
You can’t perform that action at this time.
0 commit comments