-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Refactor mapply() to split along opposite axis (#1)
* ♻️ Refactor mapply() to split along opposite axis * 🥅 Catch bad combination of passing Series and axis=1 * ✅ Add test for Series + axis=1 ValueError
- Loading branch information
Showing
5 changed files
with
106 additions
and
44 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
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 |
---|---|---|
@@ -1,33 +1,79 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
|
||
import mapply | ||
|
||
|
||
def test_mapply(): | ||
mapply.init(progressbar=False) | ||
def test_df_mapply(): | ||
mapply.init(progressbar=False, chunk_size=1) | ||
|
||
np.random.seed(1) | ||
df = pd.DataFrame( | ||
pd.np.random.randint(0, 300, size=(int(2000), 4)), columns=list("ABCD") | ||
np.random.randint(0, 300, size=(int(2000), 4)), columns=list("ABCD") | ||
) | ||
|
||
# axis as positional arg | ||
df["totals"] = df.mapply(lambda x: x.A + x.B, "columns") | ||
df.mapply(lambda x: x ** 2) | ||
|
||
# same output along both axes | ||
pd.testing.assert_frame_equal( | ||
df.apply(lambda x: x ** 2), | ||
df.mapply(lambda x: x ** 2), | ||
) | ||
pd.testing.assert_frame_equal( | ||
df.mapply(lambda x: x ** 2, axis=0), | ||
df.mapply(lambda x: x ** 2, axis=1), | ||
) | ||
|
||
# vectorized | ||
pd.testing.assert_series_equal( | ||
df.mapply(sum, max_chunks_per_worker=10), | ||
df.apply(sum), | ||
df.mapply(np.sum, raw=True), | ||
) | ||
pd.testing.assert_series_equal( | ||
df.apply(sum, axis=1), | ||
df.mapply(np.sum, raw=True, axis=1), | ||
) | ||
|
||
# result_type kwarg | ||
fn = lambda x: [x.A, x.B] # noqa:E731 | ||
pd.testing.assert_frame_equal( | ||
df.mapply(fn, axis=1, result_type="expand"), | ||
df.apply(fn, axis=1, result_type="expand"), | ||
df.mapply(fn, axis=1, result_type="expand"), | ||
) | ||
|
||
# max_chunks_per_worker=0 | ||
mapply.init(progressbar=False, chunk_size=1, max_chunks_per_worker=0) | ||
pd.testing.assert_frame_equal( | ||
df.apply(lambda x: x ** 2), | ||
df.mapply(lambda x: x ** 2), | ||
) | ||
|
||
# n_workers=1 | ||
mapply.init(progressbar=False, chunk_size=1, n_workers=1) | ||
pd.testing.assert_frame_equal( | ||
df.apply(lambda x: x ** 2), | ||
df.mapply(lambda x: x ** 2), | ||
) | ||
|
||
|
||
def test_series_mapply(): | ||
# chunk_size>1 | ||
mapply.init(progressbar=False, chunk_size=5) | ||
|
||
fn = lambda x: x ** 2 # noqa:E731 | ||
series = pd.Series(range(100)) | ||
|
||
with pytest.raises(ValueError, match="Passing axis=1 is not allowed for Series"): | ||
series.mapply(fn, axis=1) | ||
|
||
# convert_dtype=False | ||
pd.testing.assert_series_equal( | ||
series.apply(fn, convert_dtype=False), | ||
series.mapply(fn, convert_dtype=False), | ||
) | ||
|
||
mapply.init(progressbar=False, max_chunks_per_worker=0) | ||
df.mapply(lambda x: x ** 2) | ||
series = pd.Series({"a": list(range(100))}) | ||
|
||
mapply.init(progressbar=False, n_workers=1) | ||
df.mapply(lambda x: x ** 2) | ||
assert isinstance(series.mapply(lambda x: sum(x))[0], np.int64) |