@@ -188,6 +188,9 @@ def get_option(pat: str) -> Any:
188
188
return root [k ]
189
189
190
190
191
+ ### First Approach supports pd.set_option(options) where options=dict
192
+
193
+
191
194
def set_option (* args ) -> None :
192
195
"""
193
196
Set the value of the specified option or options.
@@ -199,18 +202,22 @@ def set_option(*args) -> None:
199
202
200
203
Parameters
201
204
----------
202
- *args : str | object
203
- Arguments provided in pairs, which will be interpreted as (pattern, value)
204
- pairs.
205
- pattern: str
206
- Regexp which should match a single option
207
- value: object
208
- New value of option
205
+ *args : str | object | dict
206
+ Options can be provided in one of two forms:
207
+
208
+ 1. As pairs of arguments, where each pair is interpreted as (pattern, value):
209
+ - pattern: str
210
+ Regexp which should match a single option.
211
+ - value: object
212
+ New value of option.
213
+
214
+ 2. As a single dictionary, where each key is a pattern and the corresponding
215
+ value is the new option value.
209
216
210
217
.. warning::
211
218
212
219
Partial pattern matches are supported for convenience, but unless you
213
- use the full option name (e.g. x.y.z.option_name), your code may break in
220
+ use the full option name (e.g. `` x.y.z.option_name`` ), your code may break in
214
221
future versions if new options with similar names are introduced.
215
222
216
223
Returns
@@ -220,17 +227,19 @@ def set_option(*args) -> None:
220
227
221
228
Raises
222
229
------
223
- ValueError if odd numbers of non-keyword arguments are provided
224
- TypeError if keyword arguments are provided
225
- OptionError if no such option exists
230
+ ValueError
231
+ If an odd number of non-keyword arguments is provided.
232
+ TypeError
233
+ If keyword arguments are provided.
234
+ OptionError
235
+ If no such option exists.
226
236
227
237
See Also
228
238
--------
229
239
get_option : Retrieve the value of the specified option.
230
240
reset_option : Reset one or more options to their default value.
231
241
describe_option : Print the description for one or more registered options.
232
- option_context : Context manager to temporarily set options in a ``with``
233
- statement.
242
+ option_context : Context manager to temporarily set options in a ``with`` statement.
234
243
235
244
Notes
236
245
-----
@@ -239,35 +248,169 @@ def set_option(*args) -> None:
239
248
240
249
Examples
241
250
--------
251
+ Setting options using pairs:
252
+
242
253
>>> pd.set_option("display.max_columns", 4)
243
254
>>> df = pd.DataFrame([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
244
255
>>> df
245
- 0 1 ... 3 4
246
- 0 1 2 ... 4 5
247
- 1 6 7 ... 9 10
256
+ 0 1 ... 3 4
257
+ 0 1 2 ... 4 5
258
+ 1 6 7 ... 9 10
248
259
[2 rows x 5 columns]
249
260
>>> pd.reset_option("display.max_columns")
261
+
262
+ Setting options using a dictionary:
263
+
264
+ >>> pd.set_option({"display.max_columns": 4, "display.width": 80})
250
265
"""
251
- # must at least 1 arg deal with constraints later
252
266
nargs = len (args )
253
- if not nargs or nargs % 2 != 0 :
254
- raise ValueError ("Must provide an even number of non-keyword arguments" )
267
+ pairs = []
255
268
256
- for k , v in zip (args [::2 ], args [1 ::2 ]):
257
- key = _get_single_key (k )
269
+ if nargs == 1 and isinstance (args [0 ], dict ):
270
+ pairs = args [0 ].items ()
271
+ else :
272
+ if not nargs or nargs % 2 != 0 :
273
+ raise ValueError (
274
+ "Must provide an even number of non-keyword arguments or a single dictionary"
275
+ )
276
+ pairs = zip (args [::2 ], args [1 ::2 ])
258
277
278
+ for k , v in pairs :
279
+ key = _get_single_key (k )
259
280
opt = _get_registered_option (key )
260
281
if opt and opt .validator :
261
282
opt .validator (v )
262
-
263
- # walk the nested dict
264
283
root , k_root = _get_root (key )
265
284
root [k_root ] = v
266
-
267
285
if opt .cb :
268
286
opt .cb (key )
269
287
270
288
289
+ ### Second Approach Supports both *args[pd.set_option(options)] and **kwargs[pd.set_option(**options)] where options=dict
290
+
291
+
292
+ # def set_option(*args, **kwargs) -> None:
293
+ # """
294
+ # Set the value of the specified option or options.
295
+
296
+ # This function allows fine-grained control over the behavior and display settings
297
+ # of pandas. Options affect various functionalities such as output formatting,
298
+ # display limits, and operational behavior. Settings can be modified at runtime
299
+ # without requiring changes to global configurations or environment variables.
300
+
301
+ # Options can be provided in any one of the following forms:
302
+
303
+ # 1. **Dictionary as a single positional argument:**
304
+ # Pass a dictionary where each key is an option pattern and its corresponding value
305
+ # is the new option value.
306
+
307
+ # Example:
308
+
309
+ # >>> pd.set_option({"display.max_columns": 4, "display.width": 80})
310
+
311
+ # 2. **Keyword arguments or dictionary unpacking:**
312
+ # Pass options as keyword arguments, where each keyword is the option name and its
313
+ # corresponding value is the new option value. This also supports dictionary unpacking
314
+ # using the double asterisk syntax.
315
+
316
+ # Example:
317
+
318
+ # >>> pd.set_option(display_max_columns=4, display_width=80)
319
+ # >>> options = {"display.max_columns": 4, "display.width": 80}
320
+ # >>> pd.set_option(**options)
321
+
322
+ # 3. **Traditional paired positional arguments:**
323
+ # Provide an even number of positional arguments that are interpreted as (pattern, value)
324
+ # pairs.
325
+
326
+ # Example:
327
+
328
+ # >>> pd.set_option("display.max_columns", 4, "display.width", 80)
329
+
330
+ # Parameters
331
+ # ----------
332
+ # *args : str | object | dict
333
+ # Depending on the form:
334
+ # - A single dictionary of options.
335
+ # - Or an even number of arguments representing (pattern, value) pairs.
336
+ # **kwargs : object
337
+ # When provided, keyword arguments are treated as options where the keyword is the option
338
+ # name and the value is the new option value. This includes dictionary unpacking using
339
+ # the ** syntax.
340
+
341
+ # Returns
342
+ # -------
343
+ # None
344
+ # This function does not return a value.
345
+
346
+ # Raises
347
+ # ------
348
+ # ValueError
349
+ # If an odd number of non-keyword arguments is provided.
350
+ # TypeError
351
+ # If the passed arguments do not match the expected types.
352
+ # OptionError
353
+ # If a specified option does not exist.
354
+
355
+ # See Also
356
+ # --------
357
+ # get_option : Retrieve the value of the specified option.
358
+ # reset_option : Reset one or more options to their default value.
359
+ # describe_option : Print the description for one or more registered options.
360
+ # option_context : Context manager to temporarily set options in a ``with`` statement.
361
+
362
+ # Notes
363
+ # -----
364
+ # For a complete list of available options, please refer to the :ref:`User Guide <options.available>`
365
+ # or use ``pandas.describe_option()``.
366
+
367
+ # Examples
368
+ # --------
369
+ # Using a dictionary:
370
+
371
+ # >>> pd.set_option({"display.max_columns": 4, "display.width": 80})
372
+
373
+ # Using keyword arguments or dictionary unpacking:
374
+
375
+ # >>> pd.set_option(display_max_columns=4, display_width=80)
376
+ # >>> options = {"display.max_columns": 4, "display.width": 80}
377
+ # >>> pd.set_option(**options)
378
+
379
+ # Using paired positional arguments:
380
+
381
+ # >>> pd.set_option("display.max_columns", 4, "display.width", 80)
382
+ # """
383
+ # # Handle dictionary passed directly
384
+ # if len(args) == 1 and isinstance(args[0], dict):
385
+ # options = args[0]
386
+ # for key, value in options.items():
387
+ # _set_single_option(key, value)
388
+ # # Handle keyword arguments (unpacked dictionary)
389
+ # elif kwargs:
390
+ # for key, value in kwargs.items():
391
+ # _set_single_option(key, value)
392
+ # # Handle traditional paired arguments
393
+ # else:
394
+ # if not args or len(args) % 2 != 0:
395
+ # raise ValueError(
396
+ # "Must provide an even number of non-keyword arguments or a single dictionary"
397
+ # )
398
+ # for key, value in zip(args[::2], args[1::2]):
399
+ # _set_single_option(key, value)
400
+
401
+
402
+ # def _set_single_option(key: Any, value: Any) -> None:
403
+ # """Helper function to set a single option."""
404
+ # key = _get_single_key(key)
405
+ # opt = _get_registered_option(key)
406
+ # if opt and opt.validator:
407
+ # opt.validator(value)
408
+ # root, k_root = _get_root(key)
409
+ # root[k_root] = value
410
+ # if opt.cb:
411
+ # opt.cb(key)
412
+
413
+
271
414
def describe_option (pat : str = "" , _print_desc : bool = True ) -> str | None :
272
415
"""
273
416
Print the description for one or more registered options.
0 commit comments