Skip to content

Commit 2107da1

Browse files
TomAugspurgerjreback
authored andcommitted
BUG: Fixed getattr for frame with column sparse (#30759)
Closes #30758
1 parent d5e445e commit 2107da1

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

doc/source/whatsnew/v1.0.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,7 @@ Reshaping
10041004
Sparse
10051005
^^^^^^
10061006
- Bug in :class:`SparseDataFrame` arithmetic operations incorrectly casting inputs to float (:issue:`28107`)
1007-
-
1007+
- Bug in ``DataFrame.sparse`` returning a ``Series`` when there was a column named ``sparse`` rather than the accessor (:issue:`30758`)
10081008
-
10091009

10101010
ExtensionArray

pandas/core/frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ def _constructor(self) -> Type["DataFrame"]:
402402

403403
_constructor_sliced: Type[Series] = Series
404404
_deprecations: FrozenSet[str] = NDFrame._deprecations | frozenset([])
405-
_accessors: Set[str] = set()
405+
_accessors: Set[str] = {"sparse"}
406406

407407
@property
408408
def _constructor_expanddim(self):

pandas/tests/arrays/sparse/test_accessor.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,8 @@ def test_series_from_coo_incorrect_format_raises(self):
116116
TypeError, match="Expected coo_matrix. Got csr_matrix instead."
117117
):
118118
pd.Series.sparse.from_coo(m)
119+
120+
def test_with_column_named_sparse(self):
121+
# https://github.com/pandas-dev/pandas/issues/30758
122+
df = pd.DataFrame({"sparse": pd.arrays.SparseArray([1, 2])})
123+
assert isinstance(df.sparse, pd.core.arrays.sparse.accessor.SparseFrameAccessor)

0 commit comments

Comments
 (0)