Skip to content

Commit 5390003

Browse files
committed
Merge remote-tracking branch 'upstream/main' into add-typing-aliases
2 parents f4804d3 + 4d67bb2 commit 5390003

File tree

8 files changed

+66
-17
lines changed

8 files changed

+66
-17
lines changed

ci/deps/actions-311-downstream_compat.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ dependencies:
5050
- pytz>=2023.4
5151
- pyxlsb>=1.0.10
5252
- s3fs>=2023.12.2
53-
- scipy>=1.12.0
53+
# TEMP upper pin for scipy (https://github.com/statsmodels/statsmodels/issues/9584)
54+
- scipy>=1.12.0,<1.16
5455
- sqlalchemy>=2.0.0
5556
- tabulate>=0.9.0
5657
- xarray>=2024.1.1

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,7 @@ Indexing
753753
- Bug in :meth:`DataFrame.loc` with inconsistent behavior of loc-set with 2 given indexes to Series (:issue:`59933`)
754754
- Bug in :meth:`Index.get_indexer` and similar methods when ``NaN`` is located at or after position 128 (:issue:`58924`)
755755
- Bug in :meth:`MultiIndex.insert` when a new value inserted to a datetime-like level gets cast to ``NaT`` and fails indexing (:issue:`60388`)
756+
- Bug in :meth:`Series.__setitem__` when assigning boolean series with boolean indexer will raise ``LossySetitemError`` (:issue:`57338`)
756757
- Bug in printing :attr:`Index.names` and :attr:`MultiIndex.levels` would not escape single quotes (:issue:`60190`)
757758
- Bug in reindexing of :class:`DataFrame` with :class:`PeriodDtype` columns in case of consolidated block (:issue:`60980`, :issue:`60273`)
758759

pandas/core/dtypes/cast.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,10 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any:
19261926
# i.e. there are pd.NA elements
19271927
raise LossySetitemError
19281928
return element
1929+
# GH 57338 check boolean array set as object type
1930+
if tipo.kind == "O" and isinstance(element, np.ndarray):
1931+
if lib.is_bool_array(element):
1932+
return element.astype("bool")
19291933
raise LossySetitemError
19301934
if lib.is_bool(element):
19311935
return element

pandas/core/indexes/base.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7026,6 +7026,23 @@ def infer_objects(self, copy: bool = True) -> Index:
70267026
----------
70277027
copy : bool, default True
70287028
Whether to make a copy in cases where no inference occurs.
7029+
7030+
Returns
7031+
-------
7032+
Index
7033+
An Index with a new dtype if the dtype was inferred
7034+
or a shallow copy if the dtype could not be inferred.
7035+
7036+
See Also
7037+
--------
7038+
Index.inferred_type: Return a string of the type inferred from the values.
7039+
7040+
Examples
7041+
--------
7042+
>>> pd.Index(["a", 1]).infer_objects()
7043+
Index(['a', '1'], dtype='object')
7044+
>>> pd.Index([1, 2], dtype="object").infer_objects()
7045+
Index([1, 2], dtype='int64')
70297046
"""
70307047
if self._is_multi:
70317048
raise NotImplementedError(

pandas/tests/base/test_misc.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
import numpy as np
44
import pytest
55

6-
from pandas._config import using_string_dtype
7-
86
from pandas.compat import PYPY
97

108
from pandas.core.dtypes.common import (
11-
is_dtype_equal,
129
is_object_dtype,
1310
)
1411

@@ -81,10 +78,7 @@ def test_ndarray_compat_properties(index_or_series_obj):
8178
assert Series([1]).item() == 1
8279

8380

84-
@pytest.mark.skipif(
85-
PYPY or using_string_dtype(),
86-
reason="not relevant for PyPy doesn't work properly for arrow strings",
87-
)
81+
@pytest.mark.skipif(PYPY, reason="not relevant for PyPy")
8882
def test_memory_usage(index_or_series_memory_obj):
8983
obj = index_or_series_memory_obj
9084
# Clear index caches so that len(obj) == 0 report 0 memory usage
@@ -98,18 +92,21 @@ def test_memory_usage(index_or_series_memory_obj):
9892
res = obj.memory_usage()
9993
res_deep = obj.memory_usage(deep=True)
10094

101-
is_object = is_object_dtype(obj) or (is_ser and is_object_dtype(obj.index))
102-
is_categorical = isinstance(obj.dtype, pd.CategoricalDtype) or (
103-
is_ser and isinstance(obj.index.dtype, pd.CategoricalDtype)
104-
)
105-
is_object_string = is_dtype_equal(obj, "string[python]") or (
106-
is_ser and is_dtype_equal(obj.index.dtype, "string[python]")
107-
)
95+
def _is_object_dtype(obj):
96+
if isinstance(obj, pd.MultiIndex):
97+
return any(_is_object_dtype(level) for level in obj.levels)
98+
elif isinstance(obj.dtype, pd.CategoricalDtype):
99+
return _is_object_dtype(obj.dtype.categories)
100+
elif isinstance(obj.dtype, pd.StringDtype):
101+
return obj.dtype.storage == "python"
102+
return is_object_dtype(obj)
103+
104+
has_objects = _is_object_dtype(obj) or (is_ser and _is_object_dtype(obj.index))
108105

109106
if len(obj) == 0:
110107
expected = 0
111108
assert res_deep == res == expected
112-
elif is_object or is_categorical or is_object_string:
109+
elif has_objects:
113110
# only deep will pick them up
114111
assert res_deep > res
115112
else:

pandas/tests/dtypes/cast/test_can_hold_element.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,22 @@ def test_can_hold_element_int8_int():
7777
assert not can_hold_element(arr, np.uint32(element))
7878
assert not can_hold_element(arr, np.int64(element))
7979
assert not can_hold_element(arr, np.uint64(element))
80+
81+
82+
def test_can_hold_element_bool():
83+
arr = np.array([], dtype=bool)
84+
85+
element = True
86+
assert can_hold_element(arr, element)
87+
assert can_hold_element(arr, np.array([element]))
88+
assert can_hold_element(arr, np.array([element], dtype=object))
89+
90+
element = 1
91+
assert not can_hold_element(arr, element)
92+
assert not can_hold_element(arr, np.array([element]))
93+
assert not can_hold_element(arr, np.array([element], dtype=object))
94+
95+
element = np.nan
96+
assert not can_hold_element(arr, element)
97+
assert not can_hold_element(arr, np.array([element]))
98+
assert not can_hold_element(arr, np.array([element], dtype=object))

pandas/tests/extension/test_arrow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3295,7 +3295,7 @@ def test_decimal_parse_raises():
32953295
# GH 56984
32963296
ser = pd.Series(["1.2345"], dtype=ArrowDtype(pa.string()))
32973297
with pytest.raises(
3298-
pa.lib.ArrowInvalid, match="Rescaling Decimal128 value would cause data loss"
3298+
pa.lib.ArrowInvalid, match="Rescaling Decimal(128)? value would cause data loss"
32993299
):
33003300
ser.astype(ArrowDtype(pa.decimal128(1, 0)))
33013301

pandas/tests/series/indexing/test_setitem.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,3 +1838,13 @@ def test_setitem_empty_mask_dont_upcast_dt64():
18381838
ser.mask(mask, "foo", inplace=True)
18391839
assert ser.dtype == dti.dtype # no-op -> dont upcast
18401840
tm.assert_series_equal(ser, orig)
1841+
1842+
1843+
def test_setitem_bool_dtype_with_boolean_indexer():
1844+
# GH 57338
1845+
s1 = Series([True, True, True], dtype=bool)
1846+
s2 = Series([False, False, False], dtype=bool)
1847+
condition = [False, True, False]
1848+
s1[condition] = s2[condition]
1849+
expected = Series([True, False, True], dtype=bool)
1850+
tm.assert_series_equal(s1, expected)

0 commit comments

Comments
 (0)