Skip to content

Commit

Permalink
BUG: Fix SparseFrameAccessor.to_dense return type (#59967)
Browse files Browse the repository at this point in the history
* BUG: Fix SparseFrameAccessor.to_dense return type

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
janezd and pre-commit-ci[bot] authored Oct 4, 2024
1 parent 58de332 commit 7f54bec
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ Sparse
^^^^^^
- Bug in :class:`SparseDtype` for equal comparison with na fill value. (:issue:`54770`)
- Bug in :meth:`DataFrame.sparse.from_spmatrix` which hard coded an invalid ``fill_value`` for certain subtypes. (:issue:`59063`)
- Bug in :meth:`DataFrame.sparse.to_dense` which ignored subclassing and always returned an instance of :class:`DataFrame` (:issue:`59913`)

ExtensionArray
^^^^^^^^^^^^^^
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/arrays/sparse/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,10 @@ def to_dense(self) -> DataFrame:
1 1
2 0
"""
from pandas import DataFrame

data = {k: v.array.to_dense() for k, v in self._parent.items()}
return DataFrame(data, index=self._parent.index, columns=self._parent.columns)
return self._parent._constructor(
data, index=self._parent.index, columns=self._parent.columns
)

def to_coo(self) -> spmatrix:
"""
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/arrays/sparse/test_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,7 @@ def test_with_column_named_sparse(self):
# https://github.com/pandas-dev/pandas/issues/30758
df = pd.DataFrame({"sparse": pd.arrays.SparseArray([1, 2])})
assert isinstance(df.sparse, pd.core.arrays.sparse.accessor.SparseFrameAccessor)

def test_subclassing(self):
df = tm.SubclassedDataFrame({"sparse": pd.arrays.SparseArray([1, 2])})
assert isinstance(df.sparse.to_dense(), tm.SubclassedDataFrame)

0 comments on commit 7f54bec

Please sign in to comment.