Skip to content

Commit 639cb52

Browse files
committed
fix: dataframe conditional where axis=1 failing on _NoDefault object is not suscriptable
1 parent 1863adb commit 639cb52

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,7 @@ Other
12051205
- Bug in :meth:`DataFrame.sort_index` when passing ``axis="columns"`` and ``ignore_index=True`` and ``ascending=False`` not returning a :class:`RangeIndex` columns (:issue:`57293`)
12061206
- Bug in :meth:`DataFrame.sort_values` where sorting by a column explicitly named ``None`` raised a ``KeyError`` instead of sorting by the column as expected. (:issue:`61512`)
12071207
- Bug in :meth:`DataFrame.transform` that was returning the wrong order unless the index was monotonically increasing. (:issue:`57069`)
1208+
- Bug in :meth:`DataFrame.where` where ``axis=1`` argument returns a 'float object is not suscriptable' ``TypeError`` (:issue:`58190`)
12081209
- Bug in :meth:`DataFrame.where` where using a non-bool type array in the function would return a ``ValueError`` instead of a ``TypeError`` (:issue:`56330`)
12091210
- Bug in :meth:`Index.sort_values` when passing a key function that turns values into tuples, e.g. ``key=natsort.natsort_key``, would raise ``TypeError`` (:issue:`56081`)
12101211
- Bug in :meth:`MultiIndex.fillna` error message was referring to ``isna`` instead of ``fillna`` (:issue:`60974`)

pandas/core/internals/managers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,8 @@ def apply(
431431
kwargs[k] = obj.iloc[b.mgr_locs.indexer]._values
432432
else:
433433
kwargs[k] = obj.iloc[:, b.mgr_locs.indexer]._values
434+
elif isinstance(obj, lib._NoDefault):
435+
pass
434436
else:
435437
# otherwise we have an ndarray
436438
kwargs[k] = obj[b.mgr_locs.indexer]

pandas/tests/series/indexing/test_where.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,26 @@ def test_where_datetimelike_categorical(tz_naive_fixture):
443443
res = pd.DataFrame(lvals).where(mask[:, None], pd.DataFrame(rvals))
444444

445445
tm.assert_frame_equal(res, pd.DataFrame(dr))
446+
447+
448+
@pytest.mark.parametrize(
449+
"original,conditional,expected",
450+
[
451+
(
452+
[[0.0, 0.5, 0.0], [0.1, 0.0, 0.2], [0.2, 0.0, 0.0]],
453+
[True, True, False],
454+
[[0.0, 0.5, 0.0], [0.1, 0.0, 0.2], [np.nan, np.nan, np.nan]],
455+
),
456+
(
457+
[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]],
458+
[True, False, True],
459+
[[1.0, 2.0, 3.0], [np.nan, np.nan, np.nan], [7.0, 8.0, 9.0]],
460+
),
461+
],
462+
)
463+
def test_where_axis1(original, conditional, expected):
464+
# GH 58190, 58144
465+
A = pd.DataFrame(original)
466+
res = A.where(Series(conditional), axis=1)
467+
expected = pd.DataFrame(expected)
468+
tm.assert_frame_equal(res, expected)

0 commit comments

Comments
 (0)