Skip to content

Commit b938b9f

Browse files
loganthomasZeroto521ericmjl
authored
[DOC] MkDocs Math (#901)
* [INF] simplify a bit linting, use pre-commit as CI linting checker (#892) * [INF] simplify a bit linting There is two similar linting CIs (pre-commit and code-checks) but also have tiny differences. We should use one of them as the standard, not both. * Update CHANGELOG.md * should be `--config` not `-c` `-c` = `--code TEXT`, Format the code passed in as a string. * remove nbstripout keep the same with the old code-checks.yml * lint via pre-commit * Update CHANGELOG.md Co-authored-by: Eric Ma <[email protected]> * [DOC] mkdocs updates to log() in math * [DOC] mkdocs updates to ecdf() in math * [DOC] mkdocs updates to exp() in math * [DOC] mkdocs updates to logit() in math * [DOC] mkdocs updates to normal_cdf() in math * [DOC] mkdocs updates to probit() in math * [DOC] mkdocs updates to single line docstrings in math * [DOC] mkdocs updates to z_score() in math Co-authored-by: Zero <[email protected]> Co-authored-by: Eric Ma <[email protected]>
1 parent 09796e0 commit b938b9f

File tree

1 file changed

+70
-58
lines changed

1 file changed

+70
-58
lines changed

janitor/math.py

Lines changed: 70 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,16 @@
1414
@pf.register_series_method
1515
def log(s: pd.Series, error: str = "warn") -> pd.Series:
1616
"""
17-
Take natural logarithm of the Series
17+
Take natural logarithm of the Series.
1818
19-
:param s: Input Series
19+
:param s: Input Series.
2020
:param error: Determines behavior when taking the log of nonpositive
21-
entries. If "warn" then a RuntimeWarning is thrown. If "raise",
22-
then a RuntimeError is thrown. Otherwise, nothing is thrown and
23-
log of nonpositive values is np.nan; defaults to "warn"
21+
entries. If `'warn'` then a `RuntimeWarning` is thrown. If `'raise'`,
22+
then a `RuntimeError` is thrown. Otherwise, nothing is thrown and
23+
log of nonpositive values is `np.nan`; defaults to `'warn'`.
2424
:raises RuntimeError: Raised when there are nonpositive values in the
25-
Series and error="raise"
26-
:return: Transformed Series
27-
28-
.. # noqa: DAR103 error
25+
Series and `error='raise'`.
26+
:return: Transformed Series.
2927
"""
3028
s = s.copy()
3129
nonpositive = s <= 0
@@ -43,37 +41,46 @@ def log(s: pd.Series, error: str = "warn") -> pd.Series:
4341

4442
@pf.register_series_method
4543
def exp(s: pd.Series) -> pd.Series:
46-
"""Take the exponential transform of the series"""
44+
"""
45+
Take the exponential transform of the series.
46+
47+
:param s: Input Series.
48+
:return: Transformed Series.
49+
"""
4750
return np.exp(s)
4851

4952

5053
@pf.register_series_method
5154
def sigmoid(s: pd.Series) -> pd.Series:
5255
"""
53-
Take the sigmoid transform of the series where
56+
Take the sigmoid transform of the series where:
57+
58+
```python
5459
sigmoid(x) = 1 / (1 + exp(-x))
60+
```
5561
56-
.. # noqa: DAR101
57-
.. # noqa: DAR201
62+
:param s: Input Series.
63+
:return: Transformed Series.
5864
"""
5965
return expit(s)
6066

6167

6268
@pf.register_series_method
6369
def logit(s: pd.Series, error: str = "warn") -> pd.Series:
6470
"""
65-
Take logit transform of the Series
66-
where logit(p) = log(p/(1-p))
67-
68-
:param s: Input Series
69-
:param error: Determines behavior when s / (1-s) is outside of (0, 1). If
70-
"warn" then a RuntimeWarning is thrown. If "raise", then a RuntimeError
71-
is thrown. Otherwise, nothing is thrown and np.nan is returned
72-
for the problematic entries, defaults to "warn"
73-
:return: Transformed Series
74-
:raises RuntimeError: if `error` is set to `raise``.
75-
76-
.. # noqa: DAR103 error
71+
Take logit transform of the Series where:
72+
73+
```python
74+
logit(p) = log(p/(1-p))
75+
```
76+
77+
:param s: Input Series.
78+
:param error: Determines behavior when `s / (1-s)` is outside of `(0, 1)`.
79+
If `'warn'` then a `RuntimeWarning` is thrown. If `'raise'`, then a
80+
`RuntimeError` is thrown. Otherwise, nothing is thrown and `np.nan`
81+
is returned for the problematic entries; defaults to `'warn'`.
82+
:return: Transformed Series.
83+
:raises RuntimeError: if `error` is set to `'raise'`.
7784
"""
7885
s = s.copy()
7986
odds_ratio = s / (1 - s)
@@ -93,25 +100,28 @@ def logit(s: pd.Series, error: str = "warn") -> pd.Series:
93100

94101
@pf.register_series_method
95102
def normal_cdf(s: pd.Series) -> pd.Series:
96-
"""Transforms the Series via the CDF of the Normal distribution"""
103+
"""
104+
Transforms the Series via the CDF of the Normal distribution.
105+
106+
:param s: Input Series.
107+
:return: Transformed Series.
108+
"""
97109
return pd.Series(norm.cdf(s), index=s.index)
98110

99111

100112
@pf.register_series_method
101113
def probit(s: pd.Series, error: str = "warn") -> pd.Series:
102114
"""
103-
Transforms the Series via the inverse CDF of the Normal distribution
115+
Transforms the Series via the inverse CDF of the Normal distribution.
104116
105-
:param s: Input Series
106-
:param error: Determines behavior when s is outside of (0, 1). If
107-
"warn" then a RuntimeWarning is thrown. If "raise", then a RuntimeError
108-
is thrown. Otherwise, nothing is thrown and np.nan is returned
109-
for the problematic entries, defaults to "warn"
117+
:param s: Input Series.
118+
:param error: Determines behavior when `s` is outside of `(0, 1)`.
119+
If `'warn'` then a `RuntimeWarning` is thrown. If `'raise'`, then
120+
a `RuntimeError` is thrown. Otherwise, nothing is thrown and `np.nan`
121+
is returned for the problematic entries; defaults to `'warn'`.
110122
:raises RuntimeError: Raised when there are problematic values
111-
in the Series and error="raise"
123+
in the Series and `error='raise'`.
112124
:return: Transformed Series
113-
114-
.. # noqa: DAR103 error
115125
"""
116126
s = s.copy()
117127
outside_support = (s <= 0) | (s >= 1)
@@ -136,18 +146,20 @@ def z_score(
136146
keys: Tuple[str, str] = ("mean", "std"),
137147
) -> pd.Series:
138148
"""
139-
Transforms the Series into z-scores
149+
Transforms the Series into z-scores where:
140150
141-
:param s: Input Series
142-
:param moments_dict: If not None, then the mean and standard
143-
deviation used to compute the z-score transformation is
144-
saved as entries in moments_dict with keys determined by
145-
the keys argument, defaults to None
146-
:param keys: Determines the keys saved in moments_dict
147-
if moments are saved, defaults to ("mean", "std")
148-
:return: Transformed Series
151+
```python
152+
z = (s - s.mean()) / s.std()
153+
```
149154
150-
.. # noqa: DAR103 moments_dict
155+
:param s: Input Series.
156+
:param moments_dict: If not `None`, then the mean and standard
157+
deviation used to compute the z-score transformation is
158+
saved as entries in `moments_dict` with keys determined by
159+
the `keys` argument; defaults to `None`.
160+
:param keys: Determines the keys saved in `moments_dict`
161+
if moments are saved; defaults to (`'mean'`, `'std'`).
162+
:return: Transformed Series.
151163
"""
152164
mean = s.mean()
153165
std = s.std()
@@ -166,26 +178,26 @@ def ecdf(s: pd.Series) -> Tuple[np.ndarray, np.ndarray]:
166178
167179
Intended to be used with the following pattern:
168180
169-
.. code-block:: python
170-
171-
df = pd.DataFrame(...)
181+
```python
182+
df = pd.DataFrame(...)
172183
173-
# Obtain ECDF values to be plotted
174-
x, y = df["column_name"].ecdf()
184+
# Obtain ECDF values to be plotted
185+
x, y = df["column_name"].ecdf()
175186
176-
# Plot ECDF values
177-
plt.scatter(x, y)
187+
# Plot ECDF values
188+
plt.scatter(x, y)
189+
```
178190
179191
Null values must be dropped from the series,
180192
otherwise a `ValueError` is raised.
181193
182-
Also, if the dtype of the series is not numeric,
183-
a TypeError is raised.
194+
Also, if the `dtype` of the series is not numeric,
195+
a `TypeError` is raised.
184196
185-
:param s: A pandas series. dtype should be numeric.
186-
:returns: (x, y).
187-
x: sorted array of values.
188-
y: cumulative fraction of data points with value `x` or lower.
197+
:param s: A pandas series. `dtype` should be numeric.
198+
:returns: `(x, y)`.
199+
`x`: sorted array of values.
200+
`y`: cumulative fraction of data points with value `x` or lower.
189201
:raises TypeError: if series is not numeric.
190202
:raises ValueError: if series contains nulls.
191203
"""

0 commit comments

Comments
 (0)