Skip to content

ENH: Series.name should return a string or None #42550

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

Closed
Dr-Irv opened this issue Jul 15, 2021 · 7 comments · Fixed by #51310
Closed

ENH: Series.name should return a string or None #42550

Dr-Irv opened this issue Jul 15, 2021 · 7 comments · Fixed by #51310
Labels
Docs Series Series data structure

Comments

@Dr-Irv
Copy link
Contributor

Dr-Irv commented Jul 15, 2021

Is your feature request related to a problem?

Currently, the constructor for Series has the name argument as a str or None . But Series.name can return a Hashable, e.g., as a result of using DataFrame.xs() or .iloc on a row of a DataFrame.

So this is inconsistent. I think people expect the names of Series to be strings.

Describe the solution you'd like

Change the implementation of Series.name to return a string or None

API breaking implications

This is cleaning up the API, IMHO.

Describe alternatives you've considered

N/A

Additional context

ref: discussion in #42534

@Dr-Irv Dr-Irv added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 15, 2021
@jbrockmendel
Copy link
Member

-1 on changing behavior to appease a type checker.

The Series docstring is incorrect.

@vladu
Copy link
Contributor

vladu commented Jul 15, 2021

Current typing is correct. Restricting series name to string would break frames with multi-level columns. Example:

>>> import pandas as pd
>>> df = pd.DataFrame([[1, 2], [3, 4]], columns=pd.MultiIndex.from_tuples([('a', 'one'), ('a', 'two')]))
>>> df
    a
  one two
0   1   2
1   3   4

>>> df.iloc[:, 0].name
('a', 'one')

@Dr-Irv
Copy link
Contributor Author

Dr-Irv commented Jul 15, 2021

-1 on changing behavior to appease a type checker.

The Series docstring is incorrect.

That means if we change the docstring, you're changing the API as well.

So either we change the Series docstring (API change) or Series.name return type.

@Dr-Irv
Copy link
Contributor Author

Dr-Irv commented Jul 15, 2021

Current typing is correct. Restricting series name to string would break frames with multi-level columns. Example:

It's not clear that it would "break". I'm suggesting that you'd see this:

>>> import pandas as pd
>>> df = pd.DataFrame([[1, 2], [3, 4]], columns=pd.MultiIndex.from_tuples([('a', 'one'), ('a', 'two')]))
>>> df
    a
  one two
0   1   2
1   3   4

>>> df.iloc[:, 0].name
"('a', 'b')"

@vladu
Copy link
Contributor

vladu commented Jul 15, 2021

But imagine a use case where you perform an operation that results in multi-level column names, but then want to "flatten" them with some custom formatting, for reporting / display purposes.

>>> list(f'{t[0]}: {t[1]}' for t in df.columns)
 ['a: one', 'a: two']

Easy right now, essentially impossible, or at least massively more error-prone, if the series.name tuple is stringified.

@Dr-Irv
Copy link
Contributor Author

Dr-Irv commented Jul 15, 2021

But imagine a use case where you perform an operation that results in multi-level column names, but then want to "flatten" them with some custom formatting, for reporting / display purposes.

Your example doesn't apply. The elements of df.columns would remain tuples in this proposal. What would change is the name of the series.

@TomAugspurger
Copy link
Contributor

Agreed with changing the docs to match the behavior (which doesn't qualify as an API change; it's just an issue with the docs).

@lithomas1 lithomas1 added Docs Series Series data structure Needs Discussion Requires discussion from core team before further action and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 15, 2021
@jbrockmendel jbrockmendel added the Closing Candidate May be closeable, needs more eyeballs label Jan 10, 2022
@mroeschke mroeschke removed Enhancement Needs Discussion Requires discussion from core team before further action Closing Candidate May be closeable, needs more eyeballs labels Jul 5, 2022
@jbrockmendel jbrockmendel added Closing Candidate May be closeable, needs more eyeballs and removed Closing Candidate May be closeable, needs more eyeballs labels Feb 10, 2023
jbrockmendel added a commit to jbrockmendel/pandas that referenced this issue Feb 10, 2023
mroeschke pushed a commit that referenced this issue Feb 11, 2023
DOC: fix Series.name docstring, clsoes #42550
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Series Series data structure
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants