Skip to content

Commit a1c6fe0

Browse files
Backport PR pandas-dev#52906 on branch 2.0.x (DEPS: Address numpy deprecation of len 1 arrays assignment) (pandas-dev#52933)
Backport PR pandas-dev#52906: DEPS: Address numpy deprecation of len 1 arrays assignment Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 6bc51af commit a1c6fe0

File tree

5 files changed

+24
-3
lines changed

5 files changed

+24
-3
lines changed

pandas/core/indexing.py

+7
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,13 @@ def _setitem_with_indexer(self, indexer, value, name: str = "iloc"):
17681768
if not isinstance(value, ABCSeries):
17691769
# if not Series (in which case we need to align),
17701770
# we can short-circuit
1771+
if (
1772+
isinstance(arr, np.ndarray)
1773+
and arr.ndim == 1
1774+
and len(arr) == 1
1775+
):
1776+
# NumPy 1.25 deprecation: https://github.com/numpy/numpy/pull/10615
1777+
arr = arr[0, ...]
17711778
empty_value[indexer[0]] = arr
17721779
self.obj[key] = empty_value
17731780
return

pandas/core/internals/base.py

+4
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ def setitem_inplace(self, indexer, value) -> None:
186186
# dt64/td64, which do their own validation.
187187
value = np_can_hold_element(arr.dtype, value)
188188

189+
if isinstance(value, np.ndarray) and value.ndim == 1 and len(value) == 1:
190+
# NumPy 1.25 deprecation: https://github.com/numpy/numpy/pull/10615
191+
value = value[0, ...]
192+
189193
arr[indexer] = value
190194

191195
def grouped_reduce(self, func):

pandas/core/internals/blocks.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,9 @@ def setitem(self, indexer, value, using_cow: bool = False) -> Block:
10611061
self = self.make_block_same_class(
10621062
values.T if values.ndim == 2 else values
10631063
)
1064-
1064+
if isinstance(casted, np.ndarray) and casted.ndim == 1 and len(casted) == 1:
1065+
# NumPy 1.25 deprecation: https://github.com/numpy/numpy/pull/10615
1066+
casted = casted[0, ...]
10651067
values[indexer] = casted
10661068
return self
10671069

pandas/io/parsers/base_parser.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,12 @@ def _make_date_converter(
11131113
if date_parser is not lib.no_default and date_format is not None:
11141114
raise TypeError("Cannot use both 'date_parser' and 'date_format'")
11151115

1116+
def unpack_if_single_element(arg):
1117+
# NumPy 1.25 deprecation: https://github.com/numpy/numpy/pull/10615
1118+
if isinstance(arg, np.ndarray) and arg.ndim == 1 and len(arg) == 1:
1119+
return arg[0]
1120+
return arg
1121+
11161122
def converter(*date_cols, col: Hashable):
11171123
if date_parser is lib.no_default:
11181124
strs = parsing.concat_date_cols(date_cols)
@@ -1136,7 +1142,9 @@ def converter(*date_cols, col: Hashable):
11361142
else:
11371143
try:
11381144
result = tools.to_datetime(
1139-
date_parser(*date_cols), errors="ignore", cache=cache_dates
1145+
date_parser(*(unpack_if_single_element(arg) for arg in date_cols)),
1146+
errors="ignore",
1147+
cache=cache_dates,
11401148
)
11411149
if isinstance(result, datetime.datetime):
11421150
raise Exception("scalar parser")

pandas/tests/groupby/test_groupby_dropna.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ def test_categorical_transformers(
620620
result = getattr(gb_keepna, transformation_func)(*args)
621621
expected = getattr(gb_dropna, transformation_func)(*args)
622622
for iloc, value in zip(
623-
df[df["x"].isnull()].index.tolist(), null_group_result.values
623+
df[df["x"].isnull()].index.tolist(), null_group_result.values.ravel()
624624
):
625625
if expected.ndim == 1:
626626
expected.iloc[iloc] = value

0 commit comments

Comments
 (0)