Skip to content

Backport PR #52906 on branch 2.0.x (DEPS: Address numpy deprecation of len 1 arrays assignment) #52933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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