Skip to content

BUG: Keep original values when taking with a new fill value #55929

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 3 commits into from
Nov 20, 2023

Conversation

rob-sil
Copy link
Contributor

@rob-sil rob-sil commented Nov 12, 2023

SparseArray's take method could mix up the two possible fill values: the array's fill value and the fill_value argument provided to take. In the case of a non-empty sparse array whose values were all its fill value, take would ignore the array's fill value and only use its fill_value argument, even for valid indices. The return value would be all fill_value, rather than a mix of the two fill values.

@@ -170,6 +170,13 @@ def test_take_all_empty(self):
result = a.take([0, 1], allow_fill=True, fill_value=np.nan)
tm.assert_sp_array_equal(a, result)

def test_take_different_fill_value(self):
# Take with a different fill value shouldn't overwrite the original
a = pd.array([0.0], dtype=SparseDtype("float64", fill_value=0.0))
Copy link
Member

Choose a reason for hiding this comment

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

Could you avoid the 1 letter variable name e.g. use arr

@mroeschke mroeschke added the Sparse Sparse Data Type label Nov 13, 2023
@@ -349,6 +349,7 @@ Datetimelike
- Bug in addition or subtraction of very large :class:`Tick` objects with :class:`Timestamp` or :class:`Timedelta` objects raising ``OverflowError`` instead of ``OutOfBoundsTimedelta`` (:issue:`55503`)
- Bug in creating a :class:`Index`, :class:`Series`, or :class:`DataFrame` with a non-nanosecond :class:`DatetimeTZDtype` and inputs that would be out of bounds with nanosecond resolution incorrectly raising ``OutOfBoundsDatetime`` (:issue:`54620`)
- Bug in creating a :class:`Index`, :class:`Series`, or :class:`DataFrame` with a non-nanosecond ``datetime64`` dtype and inputs that would be out of bounds for a ``datetime64[ns]`` incorrectly raising ``OutOfBoundsDatetime`` (:issue:`55756`)
- Bug in taking from a :class:`SparseArray` when using a different fill value than the array's fill value. (:issue:`55181`)
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
- Bug in taking from a :class:`SparseArray` when using a different fill value than the array's fill value. (:issue:`55181`)
- Bug in :meth:`SparseArray.take` when using a different fill value than the array's fill value. (:issue:`55181`)

@rob-sil rob-sil requested a review from mroeschke November 20, 2023 14:57
@mroeschke mroeschke added this to the 2.2 milestone Nov 20, 2023
@mroeschke mroeschke merged commit 484ec01 into pandas-dev:main Nov 20, 2023
@mroeschke
Copy link
Member

Thanks @rob-sil

phofl pushed a commit to phofl/pandas that referenced this pull request Nov 21, 2023
…ev#55929)

* Keep original values when taking with a new fill value

* Switch one-letter name

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

Successfully merging this pull request may close these issues.

BUG: DataFrame.mode() fails on sparse integer DataFrames
2 participants