-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ENH] add alias function for series (#1451)
* add alias function for series * add examples * add alias to mkdocs --------- Co-authored-by: samuel.oranyeli <[email protected]>
- Loading branch information
Showing
5 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""Implementation of the `toset` function.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
import pandas as pd | ||
import pandas_flavor as pf | ||
|
||
|
||
@pf.register_series_method | ||
def alias(series: pd.Series, alias: Any = None) -> pd.Series: | ||
"""Return a Series with a new name. Accepts either a scalar or a callable. | ||
Examples: | ||
>>> import pandas as pd | ||
>>> import janitor | ||
>>> s = pd.Series([1, 2, 3], name='series') | ||
>>> s | ||
0 1 | ||
1 2 | ||
2 3 | ||
Name: series, dtype: int64 | ||
>>> s.alias('series_new') | ||
0 1 | ||
1 2 | ||
2 3 | ||
Name: series_new, dtype: int64 | ||
>>> s.alias(str.upper) | ||
0 1 | ||
1 2 | ||
2 3 | ||
Name: SERIES, dtype: int64 | ||
Args: | ||
series: A pandas Series. | ||
alias: scalar or callable to create a new name for the pandas Series. | ||
Returns: | ||
A new pandas Series. | ||
""" | ||
series = series[:] | ||
if alias is None: | ||
return series | ||
if callable(alias): | ||
alias = alias(series.name) | ||
series.name = alias | ||
return series |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
- "!^_" | ||
members: | ||
- add_columns | ||
- alias | ||
- also | ||
- bin_numeric | ||
- case_when | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import pandas as pd | ||
from pandas.testing import assert_series_equal | ||
|
||
|
||
def test_alias_no_name(): | ||
"""Test output if Series does not have a name""" | ||
series = pd.Series([1, 2, 3]) | ||
assert_series_equal(series, series.alias()) | ||
|
||
|
||
def test_alias_callable(): | ||
"""Test output if alias is a callable""" | ||
series = pd.Series([1, 2, 3], name="UPPER") | ||
assert_series_equal(series.rename("upper"), series.alias(str.lower)) | ||
|
||
|
||
def test_alias_scalar(): | ||
"""Test output if alias is a scalar""" | ||
series = pd.Series([1, 2, 3], name="UPPER") | ||
assert_series_equal(series.rename("upper"), series.alias("upper")) |