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

Help Pint-Pandas pass tests when HAS_UNCERTAINTIES is enabled #49181

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@ def _setitem_with_indexer_split_path(self, indexer, value, name: str):

# we need an iterable, with a ndim of at least 1
# eg. don't pass through np.array(0)
if is_list_like_indexer(value) and getattr(value, "ndim", 1) > 0:
if is_list_like_indexer(value) and np.iterable(value) and getattr(value, "ndim", 1) > 0:

if isinstance(value, ABCDataFrame):
self._setitem_with_indexer_frame_value(indexer, value, name)
Expand Down Expand Up @@ -1977,6 +1977,7 @@ def _setitem_single_column(self, loc: int, value, plane_indexer):
pass
elif (
is_array_like(value)
and getattr(value, "shape", False)
and len(value.shape) > 0
and self.obj.shape[0] == value.shape[0]
and not is_empty_indexer(pi)
Expand Down
26 changes: 23 additions & 3 deletions pandas/tests/extension/base/setitem.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import numpy as np
import pytest

try:
import uncertainties.unumpy as unp
Copy link
Contributor

Choose a reason for hiding this comment

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

this is not appropriate for the pandas test suite as this is not in any way tested in ci

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 for the review! Do you have any suggestions as to what test cases I should look at to see how other extension packages deal with base tests that don't work as written? I've seen some examples where the hooks are there to create the correct sort of data (using fixtures) or hook the right functions (so that base test code calls extension functions). But am new to how I should deal with this case. If you guide, I can implement, test, and amend.

from uncertainties import ufloat, UFloat
HAS_UNCERTAINTIES = True
_ufloat_nan = ufloat(np.nan, 0)
except ImportError:
unp = np
HAS_UNCERTAINTIES = False

from pandas.core.dtypes.dtypes import (
DatetimeTZDtype,
IntervalDtype,
Expand Down Expand Up @@ -97,7 +106,11 @@ def test_setitem_scalar(self, data, setter):
assert arr[0] == data[1]

def test_setitem_loc_scalar_mixed(self, data):
df = pd.DataFrame({"A": np.arange(len(data)), "B": data})
if HAS_UNCERTAINTIES:
scalar_data = np.arange(len(data)) + ufloat(0, 0)
else:
scalar_data = np.arange(len(data))
df = pd.DataFrame({"A": scalar_data , "B": data})
df.loc[0, "B"] = data[1]
assert df.loc[0, "B"] == data[1]

Expand All @@ -112,7 +125,11 @@ def test_setitem_loc_scalar_multiple_homogoneous(self, data):
assert df.loc[10, "B"] == data[1]

def test_setitem_iloc_scalar_mixed(self, data):
df = pd.DataFrame({"A": np.arange(len(data)), "B": data})
if HAS_UNCERTAINTIES:
scalar_data = np.arange(len(data)) + ufloat(0, 0)
else:
scalar_data = np.arange(len(data))
df = pd.DataFrame({"A": scalar_data , "B": data})
df.iloc[0, 1] = data[1]
assert df.loc[0, "B"] == data[1]

Expand Down Expand Up @@ -427,7 +444,10 @@ def test_delitem_series(self, data):
# GH#40763
ser = pd.Series(data, name="data")

taker = np.arange(len(ser))
if HAS_UNCERTAINTIES:
taker = np.arange(len(ser)) + ufloat(0, 0)
else:
taker = np.arange(len(ser))
taker = np.delete(taker, 1)

expected = ser[taker]
Expand Down