Skip to content

Commit 4184167

Browse files
Merge branch 'pandas-dev:main' into ordered_cat_corr
2 parents e997747 + 2d73d62 commit 4184167

File tree

6 files changed

+65
-4
lines changed

6 files changed

+65
-4
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,7 @@ ExtensionArray
13001300
- Bug in :class:`Categorical` when constructing with an :class:`Index` with :class:`ArrowDtype` (:issue:`60563`)
13011301
- Bug in :meth:`.arrays.ArrowExtensionArray.__setitem__` which caused wrong behavior when using an integer array with repeated values as a key (:issue:`58530`)
13021302
- Bug in :meth:`ArrowExtensionArray.factorize` where NA values were dropped when input was dictionary-encoded even when dropna was set to False(:issue:`60567`)
1303+
- Bug in :meth:`NDArrayBackedExtensionArray.take` which produced arrays whose dtypes didn't match their underlying data, when called with integer arrays (:issue:`62448`)
13031304
- Bug in :meth:`api.types.is_datetime64_any_dtype` where a custom :class:`ExtensionDtype` would return ``False`` for array-likes (:issue:`57055`)
13041305
- Bug in comparison between object with :class:`ArrowDtype` and incompatible-dtyped (e.g. string vs bool) incorrectly raising instead of returning all-``False`` (for ``==``) or all-``True`` (for ``!=``) (:issue:`59505`)
13051306
- Bug in constructing pandas data structures when passing into ``dtype`` a string of the type followed by ``[pyarrow]`` while PyArrow is not installed would raise ``NameError`` rather than ``ImportError`` (:issue:`57928`)

pandas/core/arrays/numpy_.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
InterpolateOptions,
4949
NpDtype,
5050
Scalar,
51+
TakeIndexer,
5152
npt,
5253
)
5354

@@ -365,6 +366,27 @@ def interpolate(
365366
return self
366367
return type(self)._simple_new(out_data, dtype=self.dtype)
367368

369+
def take(
370+
self,
371+
indices: TakeIndexer,
372+
*,
373+
allow_fill: bool = False,
374+
fill_value: Any = None,
375+
axis: AxisInt = 0,
376+
) -> Self:
377+
"""
378+
Take entries from this array at each index in a list of indices,
379+
producing an array containing only those entries.
380+
"""
381+
result = super().take(
382+
indices, allow_fill=allow_fill, fill_value=fill_value, axis=axis
383+
)
384+
# See GH#62448.
385+
if self.dtype.kind in "iub":
386+
return type(self)(result._ndarray, copy=False)
387+
388+
return result
389+
368390
# ------------------------------------------------------------------------
369391
# Reductions
370392

pandas/tests/arrays/numpy_/test_numpy.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,42 @@ def test_factorize_unsigned():
348348
tm.assert_extension_array_equal(res_unique, NumpyExtensionArray(exp_unique))
349349

350350

351+
@pytest.mark.parametrize(
352+
"dtype",
353+
[
354+
np.bool_,
355+
np.uint8,
356+
np.uint16,
357+
np.uint32,
358+
np.uint64,
359+
np.int8,
360+
np.int16,
361+
np.int32,
362+
np.int64,
363+
],
364+
)
365+
def test_take_assigns_floating_point_dtype(dtype):
366+
# GH#62448.
367+
if dtype == np.bool_:
368+
array = NumpyExtensionArray(np.array([False, True, False], dtype=dtype))
369+
expected = np.dtype(object)
370+
else:
371+
array = NumpyExtensionArray(np.array([1, 2, 3], dtype=dtype))
372+
expected = np.float64
373+
374+
result = array.take([-1], allow_fill=True)
375+
assert result.dtype.numpy_dtype == expected
376+
377+
result = array.take([-1], allow_fill=True, fill_value=5.0)
378+
assert result.dtype.numpy_dtype == expected
379+
380+
381+
def test_take_preserves_boolean_arrays():
382+
array = NumpyExtensionArray(np.array([False, True, False], dtype=np.bool_))
383+
result = array.take([-1], allow_fill=False)
384+
assert result.dtype.numpy_dtype == np.bool_
385+
386+
351387
# ----------------------------------------------------------------------------
352388
# Output formatting
353389

pandas/tests/tseries/offsets/test_month.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test_offset_whole_year(self):
6161
datetime(2008, 12, 31),
6262
)
6363

64-
for base, exp_date in zip(dates[:-1], dates[1:]):
64+
for base, exp_date in zip(dates[:-1], dates[1:], strict=True):
6565
assert_offset_equal(SemiMonthEnd(), base, exp_date)
6666

6767
# ensure .apply_index works as expected
@@ -312,7 +312,7 @@ def test_offset_whole_year(self):
312312
datetime(2008, 12, 15),
313313
)
314314

315-
for base, exp_date in zip(dates[:-1], dates[1:]):
315+
for base, exp_date in zip(dates[:-1], dates[1:], strict=True):
316316
assert_offset_equal(SemiMonthBegin(), base, exp_date)
317317

318318
# ensure .apply_index works as expected

pandas/tests/tseries/offsets/test_offsets.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ def test_copy(self):
649649
"2008-01-02 00:00:00.001000000",
650650
"2008-01-02 00:00:00.000001000",
651651
],
652+
strict=True,
652653
),
653654
)
654655
def test_add(self, arithmatic_offset_type, expected, dt):
@@ -670,6 +671,7 @@ def test_add(self, arithmatic_offset_type, expected, dt):
670671
"2008-01-01 23:59:59.999000000",
671672
"2008-01-01 23:59:59.999999000",
672673
],
674+
strict=True,
673675
),
674676
)
675677
def test_sub(self, arithmatic_offset_type, expected, dt):
@@ -693,6 +695,7 @@ def test_sub(self, arithmatic_offset_type, expected, dt):
693695
"2008-01-02 00:00:00.008000000",
694696
"2008-01-02 00:00:00.000009000",
695697
],
698+
strict=True,
696699
),
697700
)
698701
def test_mul_add(self, arithmatic_offset_type, n, expected, dt):
@@ -717,6 +720,7 @@ def test_mul_add(self, arithmatic_offset_type, n, expected, dt):
717720
"2008-01-01 23:59:59.992000000",
718721
"2008-01-01 23:59:59.999991000",
719722
],
723+
strict=True,
720724
),
721725
)
722726
def test_mul_sub(self, arithmatic_offset_type, n, expected, dt):

pyproject.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,6 @@ exclude = [
588588
"pandas/tests/strings/test_strings.py" = ["B905"]
589589
"pandas/tests/test_algos.py" = ["B905"]
590590
"pandas/tests/test_sorting.py" = ["B905"]
591-
"pandas/tests/tseries/offsets/test_month.py" = ["B905"]
592-
"pandas/tests/tseries/offsets/test_offsets.py" = ["B905"]
593591
"pandas/tests/util/test_validate_kwargs.py" = ["B905"]
594592
"scripts/validate_unwanted_patterns.py" = ["B905"]
595593

0 commit comments

Comments
 (0)