-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TST: more method-specific test files (#30453)
- Loading branch information
1 parent
0d76ecc
commit d8d12d6
Showing
14 changed files
with
498 additions
and
458 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import re | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
from pandas import DataFrame, Series | ||
import pandas.util.testing as tm | ||
|
||
|
||
@pytest.mark.parametrize("subset", ["a", ["a"], ["a", "B"]]) | ||
def test_duplicated_with_misspelled_column_name(subset): | ||
# GH 19730 | ||
df = DataFrame({"A": [0, 0, 1], "B": [0, 0, 1], "C": [0, 0, 1]}) | ||
msg = re.escape("Index(['a'], dtype='object')") | ||
|
||
with pytest.raises(KeyError, match=msg): | ||
df.duplicated(subset) | ||
|
||
|
||
@pytest.mark.slow | ||
def test_duplicated_do_not_fail_on_wide_dataframes(): | ||
# gh-21524 | ||
# Given the wide dataframe with a lot of columns | ||
# with different (important!) values | ||
data = { | ||
"col_{0:02d}".format(i): np.random.randint(0, 1000, 30000) for i in range(100) | ||
} | ||
df = DataFrame(data).T | ||
result = df.duplicated() | ||
|
||
# Then duplicates produce the bool Series as a result and don't fail during | ||
# calculation. Actual values doesn't matter here, though usually it's all | ||
# False in this case | ||
assert isinstance(result, Series) | ||
assert result.dtype == np.bool | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"keep, expected", | ||
[ | ||
("first", Series([False, False, True, False, True])), | ||
("last", Series([True, True, False, False, False])), | ||
(False, Series([True, True, True, False, True])), | ||
], | ||
) | ||
def test_duplicated_keep(keep, expected): | ||
df = DataFrame({"A": [0, 1, 1, 2, 0], "B": ["a", "b", "b", "c", "a"]}) | ||
|
||
result = df.duplicated(keep=keep) | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
@pytest.mark.xfail(reason="GH#21720; nan/None falsely considered equal") | ||
@pytest.mark.parametrize( | ||
"keep, expected", | ||
[ | ||
("first", Series([False, False, True, False, True])), | ||
("last", Series([True, True, False, False, False])), | ||
(False, Series([True, True, True, False, True])), | ||
], | ||
) | ||
def test_duplicated_nan_none(keep, expected): | ||
df = DataFrame({"C": [np.nan, 3, 3, None, np.nan]}, dtype=object) | ||
|
||
result = df.duplicated(keep=keep) | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize("keep", ["first", "last", False]) | ||
@pytest.mark.parametrize("subset", [None, ["A", "B"], "A"]) | ||
def test_duplicated_subset(subset, keep): | ||
df = DataFrame( | ||
{ | ||
"A": [0, 1, 1, 2, 0], | ||
"B": ["a", "b", "b", "c", "a"], | ||
"C": [np.nan, 3, 3, None, np.nan], | ||
} | ||
) | ||
|
||
if subset is None: | ||
subset = list(df.columns) | ||
elif isinstance(subset, str): | ||
# need to have a DataFrame, not a Series | ||
# -> select columns with singleton list, not string | ||
subset = [subset] | ||
|
||
expected = df[subset].duplicated(keep=keep) | ||
result = df.duplicated(keep=keep, subset=subset) | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
def test_duplicated_on_empty_frame(): | ||
# GH 25184 | ||
|
||
df = DataFrame(columns=["a", "b"]) | ||
dupes = df.duplicated("a") | ||
|
||
result = df[dupes] | ||
expected = df.copy() | ||
tm.assert_frame_equal(result, expected) |
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,78 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from pandas import DataFrame, Series | ||
import pandas.util.testing as tm | ||
|
||
|
||
class TestDataFramePctChange: | ||
def test_pct_change_numeric(self): | ||
# GH#11150 | ||
pnl = DataFrame( | ||
[np.arange(0, 40, 10), np.arange(0, 40, 10), np.arange(0, 40, 10)] | ||
).astype(np.float64) | ||
pnl.iat[1, 0] = np.nan | ||
pnl.iat[1, 1] = np.nan | ||
pnl.iat[2, 3] = 60 | ||
|
||
for axis in range(2): | ||
expected = pnl.ffill(axis=axis) / pnl.ffill(axis=axis).shift(axis=axis) - 1 | ||
result = pnl.pct_change(axis=axis, fill_method="pad") | ||
|
||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_pct_change(self, datetime_frame): | ||
rs = datetime_frame.pct_change(fill_method=None) | ||
tm.assert_frame_equal(rs, datetime_frame / datetime_frame.shift(1) - 1) | ||
|
||
rs = datetime_frame.pct_change(2) | ||
filled = datetime_frame.fillna(method="pad") | ||
tm.assert_frame_equal(rs, filled / filled.shift(2) - 1) | ||
|
||
rs = datetime_frame.pct_change(fill_method="bfill", limit=1) | ||
filled = datetime_frame.fillna(method="bfill", limit=1) | ||
tm.assert_frame_equal(rs, filled / filled.shift(1) - 1) | ||
|
||
rs = datetime_frame.pct_change(freq="5D") | ||
filled = datetime_frame.fillna(method="pad") | ||
tm.assert_frame_equal( | ||
rs, (filled / filled.shift(freq="5D") - 1).reindex_like(filled) | ||
) | ||
|
||
def test_pct_change_shift_over_nas(self): | ||
s = Series([1.0, 1.5, np.nan, 2.5, 3.0]) | ||
|
||
df = DataFrame({"a": s, "b": s}) | ||
|
||
chg = df.pct_change() | ||
expected = Series([np.nan, 0.5, 0.0, 2.5 / 1.5 - 1, 0.2]) | ||
edf = DataFrame({"a": expected, "b": expected}) | ||
tm.assert_frame_equal(chg, edf) | ||
|
||
@pytest.mark.parametrize( | ||
"freq, periods, fill_method, limit", | ||
[ | ||
("5B", 5, None, None), | ||
("3B", 3, None, None), | ||
("3B", 3, "bfill", None), | ||
("7B", 7, "pad", 1), | ||
("7B", 7, "bfill", 3), | ||
("14B", 14, None, None), | ||
], | ||
) | ||
def test_pct_change_periods_freq( | ||
self, datetime_frame, freq, periods, fill_method, limit | ||
): | ||
# GH#7292 | ||
rs_freq = datetime_frame.pct_change( | ||
freq=freq, fill_method=fill_method, limit=limit | ||
) | ||
rs_periods = datetime_frame.pct_change( | ||
periods, fill_method=fill_method, limit=limit | ||
) | ||
tm.assert_frame_equal(rs_freq, rs_periods) | ||
|
||
empty_ts = DataFrame(index=datetime_frame.index, columns=datetime_frame.columns) | ||
rs_freq = empty_ts.pct_change(freq=freq, fill_method=fill_method, limit=limit) | ||
rs_periods = empty_ts.pct_change(periods, fill_method=fill_method, limit=limit) | ||
tm.assert_frame_equal(rs_freq, rs_periods) |
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
Oops, something went wrong.