-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
BUG: Increased support for subclassed types. #31331
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
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
bdafa80
Better subclassed type support in DataFrame.count()
43673b6
Subclassed type support in DataFrame.count()
7f594b5
Subclassed type support in DataFrame.duplicated()
a278d38
Subclassed type support in DataFrame.idxmin()
b9cf60a
Subclassed type support in DataFrame.idxmax()
99742a0
Subclassed type support in DataFrame.dot()
b117276
Subclassed type support in DataFrame.memory_usage()
20f9574
Subclassed type support in DataFrame.corrwith()
f0eaaa2
Better subclassed type support in DataFrame.asof()
0393c5d
Subclassed type support in Series.explode()
40f1ba7
black formatting
979f3cc
Added What's New entry
a02cbf3
Merge branch 'master' into subclass
c926c9f
Parameterized idx tests
f28852e
Merge remote-tracking branch 'upstream/master' into subclass
67ccc7f
Merge remote-tracking branch 'upstream/master' into subclass
a298deb
Merge branch 'master' into subclass
570dc6d
Merge master
e853d80
Added skip_if_no_scipy to failing test for three build systems failin…
4e60f49
Add docs
3f0d920
Merge remote-tracking branch 'upstream/master' into subclass
5794ffc
Removed an error in merging master.
5072f9e
Final commit for review
a25da2e
created all_reductions fixture
25b4dfb
Merge branch 'master' into subclass
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,6 +1,8 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
import pandas.util._test_decorators as td | ||
|
||
import pandas as pd | ||
from pandas import DataFrame, Index, MultiIndex, Series | ||
import pandas._testing as tm | ||
|
@@ -560,16 +562,123 @@ def strech(row): | |
assert not isinstance(result, tm.SubclassedDataFrame) | ||
tm.assert_series_equal(result, expected) | ||
|
||
def test_subclassed_numeric_reductions(self, all_numeric_reductions): | ||
def test_subclassed_reductions(self, all_reductions): | ||
# GH 25596 | ||
|
||
df = tm.SubclassedDataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]}) | ||
result = getattr(df, all_numeric_reductions)() | ||
result = getattr(df, all_reductions)() | ||
assert isinstance(result, tm.SubclassedSeries) | ||
|
||
def test_subclassed_boolean_reductions(self, all_boolean_reductions): | ||
# GH 25596 | ||
def test_subclassed_count(self): | ||
|
||
df = tm.SubclassedDataFrame( | ||
{ | ||
"Person": ["John", "Myla", "Lewis", "John", "Myla"], | ||
"Age": [24.0, np.nan, 21.0, 33, 26], | ||
"Single": [False, True, True, True, False], | ||
} | ||
) | ||
result = df.count() | ||
assert isinstance(result, tm.SubclassedSeries) | ||
|
||
df = tm.SubclassedDataFrame({"A": [1, 0, 3], "B": [0, 5, 6], "C": [7, 8, 0]}) | ||
result = df.count() | ||
assert isinstance(result, tm.SubclassedSeries) | ||
|
||
df = tm.SubclassedDataFrame( | ||
[[10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33], [40, 41, 42, 43]], | ||
index=MultiIndex.from_tuples( | ||
list(zip(list("AABB"), list("cdcd"))), names=["aaa", "ccc"] | ||
), | ||
columns=MultiIndex.from_tuples( | ||
list(zip(list("WWXX"), list("yzyz"))), names=["www", "yyy"] | ||
), | ||
) | ||
result = df.count(level=1) | ||
assert isinstance(result, tm.SubclassedDataFrame) | ||
|
||
df = tm.SubclassedDataFrame() | ||
result = df.count() | ||
assert isinstance(result, tm.SubclassedSeries) | ||
|
||
def test_isin(self): | ||
|
||
df = tm.SubclassedDataFrame( | ||
{"num_legs": [2, 4], "num_wings": [2, 0]}, index=["falcon", "dog"] | ||
) | ||
result = df.isin([0, 2]) | ||
assert isinstance(result, tm.SubclassedDataFrame) | ||
|
||
def test_duplicated(self): | ||
|
||
df = tm.SubclassedDataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]}) | ||
result = getattr(df, all_boolean_reductions)() | ||
result = df.duplicated() | ||
assert isinstance(result, tm.SubclassedSeries) | ||
|
||
df = tm.SubclassedDataFrame() | ||
result = df.duplicated() | ||
assert isinstance(result, tm.SubclassedSeries) | ||
|
||
@pytest.mark.parametrize("idx_method", ["idxmax", "idxmin"]) | ||
def test_idx(self, idx_method): | ||
|
||
df = tm.SubclassedDataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]}) | ||
result = getattr(df, idx_method)() | ||
assert isinstance(result, tm.SubclassedSeries) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks like it can be parametrized+shared with test_idxmin. if there are any others that can be parametrized, pls give it a try |
||
|
||
def test_dot(self): | ||
|
||
df = tm.SubclassedDataFrame([[0, 1, -2, -1], [1, 1, 1, 1]]) | ||
s = tm.SubclassedSeries([1, 1, 2, 1]) | ||
result = df.dot(s) | ||
assert isinstance(result, tm.SubclassedSeries) | ||
|
||
df = tm.SubclassedDataFrame([[0, 1, -2, -1], [1, 1, 1, 1]]) | ||
s = tm.SubclassedDataFrame([1, 1, 2, 1]) | ||
result = df.dot(s) | ||
assert isinstance(result, tm.SubclassedDataFrame) | ||
|
||
def test_memory_usage(self): | ||
|
||
df = tm.SubclassedDataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]}) | ||
result = df.memory_usage() | ||
assert isinstance(result, tm.SubclassedSeries) | ||
|
||
result = df.memory_usage(index=False) | ||
assert isinstance(result, tm.SubclassedSeries) | ||
|
||
@td.skip_if_no_scipy | ||
def test_corrwith(self): | ||
index = ["a", "b", "c", "d", "e"] | ||
columns = ["one", "two", "three", "four"] | ||
df1 = tm.SubclassedDataFrame( | ||
np.random.randn(5, 4), index=index, columns=columns | ||
) | ||
df2 = tm.SubclassedDataFrame( | ||
np.random.randn(4, 4), index=index[:4], columns=columns | ||
) | ||
correls = df1.corrwith(df2, axis=1, drop=True, method="kendall") | ||
|
||
assert isinstance(correls, (tm.SubclassedSeries)) | ||
|
||
def test_asof(self): | ||
|
||
N = 3 | ||
rng = pd.date_range("1/1/1990", periods=N, freq="53s") | ||
df = tm.SubclassedDataFrame( | ||
{ | ||
"A": [np.nan, np.nan, np.nan], | ||
"B": [np.nan, np.nan, np.nan], | ||
"C": [np.nan, np.nan, np.nan], | ||
}, | ||
index=rng, | ||
) | ||
|
||
result = df.asof(rng[-2:]) | ||
assert isinstance(result, tm.SubclassedDataFrame) | ||
|
||
result = df.asof(rng[-2]) | ||
assert isinstance(result, tm.SubclassedSeries) | ||
|
||
result = df.asof("1989-12-31") | ||
assert isinstance(result, tm.SubclassedSeries) |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.