Skip to content

Commit b0530e9

Browse files
mroeschkeim-vinicius
authored and
im-vinicius
committed
DEPS: Address numpy deprecation of len 1 arrays assignment (pandas-dev#52906)
* DEPS: Address numpy deprecation of len 1 arrays assignment * Address other failures, and investigate csv failure * Address csv error, undo one fix * Undo whitespace * Turn into 0D array
1 parent 36b3021 commit b0530e9

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
@@ -1753,6 +1753,13 @@ def _setitem_with_indexer(self, indexer, value, name: str = "iloc"):
17531753
if not isinstance(value, ABCSeries):
17541754
# if not Series (in which case we need to align),
17551755
# we can short-circuit
1756+
if (
1757+
isinstance(arr, np.ndarray)
1758+
and arr.ndim == 1
1759+
and len(arr) == 1
1760+
):
1761+
# NumPy 1.25 deprecation: https://github.com/numpy/numpy/pull/10615
1762+
arr = arr[0, ...]
17561763
empty_value[indexer[0]] = arr
17571764
self.obj[key] = empty_value
17581765
return

pandas/core/internals/base.py

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

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

192196
def grouped_reduce(self, func):

pandas/core/internals/blocks.py

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

pandas/io/parsers/base_parser.py

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

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

pandas/tests/groupby/test_groupby_dropna.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ def test_categorical_transformers(
627627
result = getattr(gb_keepna, transformation_func)(*args)
628628
expected = getattr(gb_dropna, transformation_func)(*args)
629629
for iloc, value in zip(
630-
df[df["x"].isnull()].index.tolist(), null_group_result.values
630+
df[df["x"].isnull()].index.tolist(), null_group_result.values.ravel()
631631
):
632632
if expected.ndim == 1:
633633
expected.iloc[iloc] = value

0 commit comments

Comments
 (0)