Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Support dict to pd.set_option for cleaner code #61093

Open
1 of 3 tasks
yasirroni opened this issue Mar 10, 2025 · 14 comments · May be fixed by #61151 or #61152
Open
1 of 3 tasks

ENH: Support dict to pd.set_option for cleaner code #61093

yasirroni opened this issue Mar 10, 2025 · 14 comments · May be fixed by #61151 or #61152
Assignees
Labels
API Design Deprecate Functionality to remove in pandas Enhancement

Comments

@yasirroni
Copy link

yasirroni commented Mar 10, 2025

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

I wish that I could use dict for pd.set_option, just like other packages typically accept dict when there are lots of option. Current pandas style follow what old programming language do, such as MATLAB.

Feature Description

import pandas as pd

options = {
    'display.precision': 2,
    'display.max_columns': 100,
    'styler.format.precision': 2,
}

# like this
pd.set_option(**options)

# or like this:
pd.set_option(options)

Alternative Solutions

Current implementations is:

pd.set_option('display.precision', 2)
pd.set_option('display.max_columns', 100)
pd.set_option('styler.format.precision', 2)

# or

options = {
    'display.precision': 2,
    'display.max_columns': 100,
    'styler.format.precision': 2,
}

for key, value in options.items():
    pd.set_option(key, value)

Additional Context

Because if I write it like this:

pd.set_option(
    'display.precision', 2,
    'display.max_columns', 100,
    'styler.format.precision', 2,
)

An auto formatter like ruff will make it into:

pd.set_option(
    'display.precision',
    2,
    'display.max_columns',
    100,
    'styler.format.precision',
    2,
)
@yasirroni yasirroni added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 10, 2025
@arthurlw
Copy link
Contributor

take

@himanshumahajan138
Copy link

himanshumahajan138 commented Mar 20, 2025

working on this

aah i missed it maybe

if i have solution can i propose it @arthurlw

@arthurlw arthurlw linked a pull request Mar 20, 2025 that will close this issue
5 tasks
@arthurlw
Copy link
Contributor

Oh sorry @himanshumahajan138 I just saw your message. If you have a solution too we can discuss and compare!

@yasirroni
Copy link
Author

yasirroni commented Mar 20, 2025

Hi @arthurlw and @himanshumahajan138. Thanks for making the PR.

I think #61152 is cleaner, but has a higher risk of bug (if exist). Also, I don't think we need to support the ** approach as it is not based on args(?).

@himanshumahajan138
Copy link

@yasirroni actually i am struggling with the Tight code formatting fixtures 😅

Fixing the failing doc tests and type tests after that we can have discussions on approach suitability

@rhshadrach
Copy link
Member

cc @pandas-dev/pandas-core for any thoughts.

@rhshadrach rhshadrach added the Needs Discussion Requires discussion from core team before further action label Mar 20, 2025
@Dr-Irv
Copy link
Contributor

Dr-Irv commented Mar 20, 2025

I think it's a good idea to support a dict as an argument to set_option() although maybe we should introduce set_options() to support a dict so there is a difference between setting a single option versus multiple options in a single call?

@datapythonista
Copy link
Member

I think it's a good addition, the same time makes things a bit complicated. @Dr-Irv makes the API clearer in my opinion, but I would prefer to not have to add and maintain one more method being almoat identical to the existing.

An idea could be since we are addimg a way to pass multiple options, to allow only one option and remove pd.set_option('s1': 1, 's2': 2) in favour of pasaing a dictionary instead. Not perfect, but feels like it'll male both the API and the code slightly aimpler.

In any case, I'm ok with any option.

@himanshumahajan138
Copy link

himanshumahajan138 commented Mar 21, 2025

Good wishes Members,

Actually my PR consists both approaches first one is only for pd.set_option(options) and second is for both of the cases pd.set_option(options) & pd.set_option(**options)

PR : #61152

@arthurlw
Copy link
Contributor

arthurlw commented Mar 21, 2025

For my PR (#61151), I implemented the first approach only, which is pd.set_options(options), where options is a dictionary.

I think this would be simpler, but I am okay with either option.

@rhshadrach
Copy link
Member

rhshadrach commented Mar 22, 2025

although maybe we should introduce set_options() to support a dict so there is a difference between setting a single option versus multiple options in a single call?

I think the issue with this is that pd.set_option can already set multiple options. So the API seems slightly confusing to me. In addition, I don't particularly like different functions that are very similarly named and do essentially the same thing with different argument types. To me, it'd be similar to proposing pandas have DataFrame.set_index and DataFrame.set_indexes.

@rhshadrach
Copy link
Member

An idea could be since we are addimg a way to pass multiple options, to allow only one option and remove pd.set_option('s1': 1, 's2': 2) in favour of pasaing a dictionary instead. Not perfect, but feels like it'll male both the API and the code slightly aimpler.

Just to be sure, I think you mean to deprecate pd.set_option('s1', 1, 's2', 2). I'm in favor of this.

@arthurlw
Copy link
Contributor

arthurlw commented Mar 22, 2025

If we support dictionaries, I think it would make sense to deprecate pd.set_option('s1', 1, 's2', 2) as well. Added a deprecation warning to my PR for now.

@rhshadrach rhshadrach added API Design Deprecate Functionality to remove in pandas and removed Needs Discussion Requires discussion from core team before further action Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 22, 2025
@simonjayhawkins
Copy link
Member

If we support dictionaries, I think it would make sense to deprecate pd.set_option('s1', 1, 's2', 2) as well. Added a deprecation warning to my PR for now.

This is not really a benefit for existing users that already the functionality of the current API to set multiple options at once?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Deprecate Functionality to remove in pandas Enhancement
Projects
None yet
7 participants