Skip to content

[DOC] Add docstring MWEs for sort_naturally and take_first #983

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

Merged
merged 2 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions janitor/functions/sort_naturally.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Implementation of the `sort_naturally` function."""
import pandas_flavor as pf
import pandas as pd
from natsort import index_natsorted
Expand Down Expand Up @@ -39,32 +40,33 @@ def sort_naturally(
after the column name to sort by is provided.
They are passed through to the `natsorted` function.

Functional usage syntax:
Example:

>>> import pandas as pd
>>> import janitor
>>> df = pd.DataFrame(
... {
... "Well": ["A21", "A3", "A21", "B2", "B51", "B12"],
... "Value": [1, 2, 13, 3, 4, 7],
... }
... )
>>> df
Well Value
0 A21 1
1 A3 2
2 A21 13
3 B2 3
4 B51 4
5 B12 7
>>> df.sort_naturally("Well")
Well Value
1 A3 2
0 A21 1
2 A21 13
3 B2 3
5 B12 7
4 B51 4

```python
import pandas as pd
import janitor as jn

df = pd.DataFrame(...)

df = jn.sort_naturally(
df=df,
column_name='alphanumeric_column',
)
```

Method chaining usage syntax:

```python
import pandas as pd
import janitor

df = pd.DataFrame(...)

df = df.sort_naturally(
column_name='alphanumeric_column',
)
```
:param df: A pandas DataFrame.
:param column_name: The column on which natural sorting should take place.
:param natsorted_kwargs: Keyword arguments to be passed
Expand Down
30 changes: 16 additions & 14 deletions janitor/functions/take_first.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Implementation of take_first function."""
from typing import Hashable, Iterable, Union
import pandas_flavor as pf
import pandas as pd
Expand All @@ -13,20 +14,21 @@ def take_first(
"""
Take the first row within each group specified by `subset`.

This method does not mutate the original DataFrame.

```python
import pandas as pd
import janitor

data = {
"a": ["x", "x", "y", "y"],
"b": [0, 1, 2, 3]
}
df = pd.DataFrame(data)

df.take_first(subset="a", by="b")
```
Example:

>>> import pandas as pd
>>> import janitor
>>> df = pd.DataFrame({"a": ["x", "x", "y", "y"], "b": [0, 1, 2, 3]})
>>> df
a b
0 x 0
1 x 1
2 y 2
3 y 3
>>> df.take_first(subset="a", by="b")
a b
0 x 0
2 y 2

:param df: A pandas DataFrame.
:param subset: Column(s) defining the group.
Expand Down