File tree Expand file tree Collapse file tree 3 files changed +12
-3
lines changed Expand file tree Collapse file tree 3 files changed +12
-3
lines changed Original file line number Diff line number Diff line change @@ -682,6 +682,7 @@ Sparse
682
682
^^^^^^
683
683
- Bug in :class: `SparseDtype ` for equal comparison with na fill value. (:issue: `54770 `)
684
684
- Bug in :meth: `DataFrame.sparse.from_spmatrix ` which hard coded an invalid ``fill_value `` for certain subtypes. (:issue: `59063 `)
685
+ - Bug in :meth: `DataFrame.sparse.to_dense ` which ignored subclassing and always returned an instance of :class: `DataFrame ` (:issue: `59913 `)
685
686
686
687
ExtensionArray
687
688
^^^^^^^^^^^^^^
Original file line number Diff line number Diff line change @@ -369,10 +369,9 @@ def to_dense(self) -> DataFrame:
369
369
1 1
370
370
2 0
371
371
"""
372
- from pandas import DataFrame
373
-
372
+ constructor = self ._parent ._constructor
374
373
data = {k : v .array .to_dense () for k , v in self ._parent .items ()}
375
- return DataFrame (data , index = self ._parent .index , columns = self ._parent .columns )
374
+ return constructor (data , index = self ._parent .index , columns = self ._parent .columns )
376
375
377
376
def to_coo (self ) -> spmatrix :
378
377
"""
Original file line number Diff line number Diff line change @@ -252,3 +252,12 @@ def test_with_column_named_sparse(self):
252
252
# https://github.com/pandas-dev/pandas/issues/30758
253
253
df = pd .DataFrame ({"sparse" : pd .arrays .SparseArray ([1 , 2 ])})
254
254
assert isinstance (df .sparse , pd .core .arrays .sparse .accessor .SparseFrameAccessor )
255
+
256
+ def test_subclassing (self ):
257
+ class SubclassedDataFrame (pd .DataFrame ):
258
+ @property
259
+ def _constructor (self ):
260
+ return SubclassedDataFrame
261
+
262
+ df = SubclassedDataFrame ({"sparse" : pd .arrays .SparseArray ([1 , 2 ])})
263
+ assert isinstance (df .sparse .to_dense (), SubclassedDataFrame )
You can’t perform that action at this time.
0 commit comments