Skip to content
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

BUG: Fix SparseFrameAccessor.to_dense return type #59967

Merged
merged 2 commits into from
Oct 4, 2024

Conversation

janezd
Copy link
Contributor

@janezd janezd commented Oct 4, 2024

Comment on lines 257 to 263
class SubclassedDataFrame(pd.DataFrame):
@property
def _constructor(self):
return SubclassedDataFrame

df = SubclassedDataFrame({"sparse": pd.arrays.SparseArray([1, 2])})
assert isinstance(df.sparse.to_dense(), SubclassedDataFrame)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class SubclassedDataFrame(pd.DataFrame):
@property
def _constructor(self):
return SubclassedDataFrame
df = SubclassedDataFrame({"sparse": pd.arrays.SparseArray([1, 2])})
assert isinstance(df.sparse.to_dense(), SubclassedDataFrame)
df = tm.SubclassedDataFrame({"sparse": pd.arrays.SparseArray([1, 2])})
assert isinstance(df.sparse.to_dense(), tm.SubclassedDataFrame)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I didn't know it's already there.

@@ -369,10 +369,9 @@ def to_dense(self) -> DataFrame:
1 1
2 0
"""
from pandas import DataFrame

constructor = self._parent._constructor
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Could you inline this on the return line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, done.

@mroeschke mroeschke added Sparse Sparse Data Type Subclassing Subclassing pandas objects labels Oct 4, 2024
@janezd janezd force-pushed the fix-sparse-accessor-to-dense branch from fea6dd8 to 10b2363 Compare October 4, 2024 20:24
Copy link
Contributor Author

@janezd janezd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. I amended the existing commit.

Comment on lines 257 to 263
class SubclassedDataFrame(pd.DataFrame):
@property
def _constructor(self):
return SubclassedDataFrame

df = SubclassedDataFrame({"sparse": pd.arrays.SparseArray([1, 2])})
assert isinstance(df.sparse.to_dense(), SubclassedDataFrame)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I didn't know it's already there.

@@ -369,10 +369,9 @@ def to_dense(self) -> DataFrame:
1 1
2 0
"""
from pandas import DataFrame

constructor = self._parent._constructor
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, done.

@mroeschke
Copy link
Member

pre-commit.ci autofix

@mroeschke mroeschke added this to the 3.0 milestone Oct 4, 2024
@mroeschke mroeschke merged commit 7f54bec into pandas-dev:main Oct 4, 2024
51 checks passed
@mroeschke
Copy link
Member

Thanks @janezd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Sparse Sparse Data Type Subclassing Subclassing pandas objects
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: SparseFrameAccessor.to_dense ignores _constructor
2 participants