Skip to content

Commit

Permalink
Backport PR #52906 on branch 2.0.x (DEPS: Address numpy deprecation o…
Browse files Browse the repository at this point in the history
…f len 1 arrays assignment) (#52933)

Backport PR #52906: DEPS: Address numpy deprecation of len 1 arrays assignment

Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and mroeschke committed Apr 26, 2023
1 parent 6bc51af commit a1c6fe0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
7 changes: 7 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,13 @@ def _setitem_with_indexer(self, indexer, value, name: str = "iloc"):
if not isinstance(value, ABCSeries):
# if not Series (in which case we need to align),
# we can short-circuit
if (
isinstance(arr, np.ndarray)
and arr.ndim == 1
and len(arr) == 1
):
# NumPy 1.25 deprecation: https://github.com/numpy/numpy/pull/10615
arr = arr[0, ...]
empty_value[indexer[0]] = arr
self.obj[key] = empty_value
return
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/internals/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ def setitem_inplace(self, indexer, value) -> None:
# dt64/td64, which do their own validation.
value = np_can_hold_element(arr.dtype, value)

if isinstance(value, np.ndarray) and value.ndim == 1 and len(value) == 1:
# NumPy 1.25 deprecation: https://github.com/numpy/numpy/pull/10615
value = value[0, ...]

arr[indexer] = value

def grouped_reduce(self, func):
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,9 @@ def setitem(self, indexer, value, using_cow: bool = False) -> Block:
self = self.make_block_same_class(
values.T if values.ndim == 2 else values
)

if isinstance(casted, np.ndarray) and casted.ndim == 1 and len(casted) == 1:
# NumPy 1.25 deprecation: https://github.com/numpy/numpy/pull/10615
casted = casted[0, ...]
values[indexer] = casted
return self

Expand Down
10 changes: 9 additions & 1 deletion pandas/io/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,12 @@ def _make_date_converter(
if date_parser is not lib.no_default and date_format is not None:
raise TypeError("Cannot use both 'date_parser' and 'date_format'")

def unpack_if_single_element(arg):
# NumPy 1.25 deprecation: https://github.com/numpy/numpy/pull/10615
if isinstance(arg, np.ndarray) and arg.ndim == 1 and len(arg) == 1:
return arg[0]
return arg

def converter(*date_cols, col: Hashable):
if date_parser is lib.no_default:
strs = parsing.concat_date_cols(date_cols)
Expand All @@ -1136,7 +1142,9 @@ def converter(*date_cols, col: Hashable):
else:
try:
result = tools.to_datetime(
date_parser(*date_cols), errors="ignore", cache=cache_dates
date_parser(*(unpack_if_single_element(arg) for arg in date_cols)),
errors="ignore",
cache=cache_dates,
)
if isinstance(result, datetime.datetime):
raise Exception("scalar parser")
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/groupby/test_groupby_dropna.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ def test_categorical_transformers(
result = getattr(gb_keepna, transformation_func)(*args)
expected = getattr(gb_dropna, transformation_func)(*args)
for iloc, value in zip(
df[df["x"].isnull()].index.tolist(), null_group_result.values
df[df["x"].isnull()].index.tolist(), null_group_result.values.ravel()
):
if expected.ndim == 1:
expected.iloc[iloc] = value
Expand Down

0 comments on commit a1c6fe0

Please sign in to comment.