Skip to content

Commit 9fba33e

Browse files
authored
MAINT: add support for numpy2 (pydata#212)
1 parent 3214ebf commit 9fba33e

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

patsy/util.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,18 @@
5252
"is_categorical_dtype", None)
5353
have_pandas_categorical_dtype = _pandas_is_categorical_dtype is not None
5454

55+
# The handling of the `copy` keyword has been changed since numpy>=2.
56+
# https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword
57+
# If numpy<2 support is dropped, this try-clause can be removed.
58+
try:
59+
np.array([1]).__array__(copy=None)
60+
copy_if_needed = None
61+
except TypeError:
62+
copy_if_needed = False
63+
5564

5665
# Passes through Series and DataFrames, call np.asarray() on everything else
57-
def asarray_or_pandas(a, copy=False, dtype=None, subok=False):
66+
def asarray_or_pandas(a, copy=copy_if_needed, dtype=None, subok=False):
5867
if have_pandas:
5968
if isinstance(a, (pandas.Series, pandas.DataFrame)):
6069
# The .name attribute on Series is discarded when passing through

0 commit comments

Comments
 (0)