-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Comments
-1 on changing behavior to appease a type checker. The Series docstring is incorrect. |
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') |
That means if we change the docstring, you're changing the API as well. So either we change the |
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')" |
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. |
Your example doesn't apply. The elements of |
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). |
Is your feature request related to a problem?
Currently, the constructor for
Series
has thename
argument as astr
orNone
. ButSeries.name
can return aHashable
, e.g., as a result of usingDataFrame.xs()
or.iloc
on a row of aDataFrame
.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 orNone
API breaking implications
This is cleaning up the API, IMHO.
Describe alternatives you've considered
N/A
Additional context
ref: discussion in #42534
The text was updated successfully, but these errors were encountered: