8000 COMPAT: np 1.18 wants explicit dtype=object) by jbrockmendel · Pull Request #30035 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

COMPAT: np 1.18 wants explicit dtype=object) #30035

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

Closed
wants to merge 11 commits into from
Closed
Prev Previous commit
Next Next commit
suppress more
  • Loading branch information
jbrockmendel committed Dec 4, 2019
commit af573eb44fde1b682d49a9da51a5592652c16f9c
7 changes: 6 additions & 1 deletion pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numbers
from typing import Union
import warnings

import numpy as np
from numpy.lib.mixins import NDArrayOperatorsMixin
Expand Down Expand Up @@ -147,7 +148,11 @@ def _from_sequence(cls, scalars, dtype=None, copy=False):
if isinstance(dtype, PandasDtype):
dtype = dtype._dtype

result = np.asarray(scalars, dtype=dtype)
with warnings.catch_warnings():
# See https://github.com/numpy/numpy/issues/15041
warnings.filterwarnings("ignore", ".*with automatic object dtype.*")
result = np.asarray(scalars, dtype=dtype)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be fixed at the caller, not here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yah, pretty much every usage where we're just suppressing the warnings was a kludge

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thought for a way forward with the new sentinel in numpy/numpy#15119 is that you would leave this unchanged. When needed you would add the sentinel as the dtype in the call to _from_sequence (or even to whoever is calling _from_sequence. During the deprecation period, try to rework the use case that justifies the inefficient ragged array, and get back to NumPy with the use case for such a construct so we can stop or rethink the deprecation.


if copy and result is scalars:
result = result.copy()
return cls(result)
Expand Down
9 changes: 8 additions & 1 deletion pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from functools import partial
import inspect
from typing import Any, Iterable, Union
import warnings

import numpy as np

Expand Down Expand Up @@ -224,7 +225,13 @@ def asarray_tuplesafe(values, dtype=None):
if isinstance(values, list) and dtype in [np.object_, object]:
return construct_1d_object_array_from_listlike(values)

result = np.asarray(values, dtype=dtype)
if dtype is None:
with warnings.catch_warnings():
# See https://github.com/numpy/numpy/issues/15041
warnings.filterwarnings("ignore", ".*with automatic object dtype.*")
result = np.asarray(values)
else:
result = np.asarray(values, dtype=dtype)

if issubclass(result.dtype.type, str):
result = np.asarray(values, dtype=object)
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" routings for casting """

from datetime import datetime, timedelta
import warnings

import numpy as np

Expand Down Expand Up @@ -1029,7 +1030,11 @@ def maybe_infer_to_datetimelike(value, convert_dates: bool = False):

if not is_list_like(v):
v = [v]
v = np.array(v, copy=False)

with warnings.catch_warnings():
# See https://github.com/numpy/numpy/issues/15041
warnings.filterwarnings("ignore", ".*with automatic object dtype.*")
v = np.array(v, copy=False)

# we only care about object dtypes
if not is_object_dtype(v):
Expand Down
13 changes: 12 additions & 1 deletion pandas/core/dtypes/concat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Utility functions related to concat
"""
import warnings

import numpy as np

Expand Down Expand Up @@ -134,6 +135,7 @@ def is_nonempty(x) -> bool:
# coerce to object
to_concat = [x.astype("object") for x in to_concat]

to_concat = [_safe_array(x) for x in to_concat]
return np.concatenate(to_concat, axis=axis)


Expand Down Expand Up @@ -172,7 +174,7 @@ def concat_categorical(to_concat, axis: int = 0):
to_concat = [
x._internal_get_values()
if is_categorical_dtype(x.dtype)
else np.asarray(x).ravel()
else _safe_array(x).ravel()
if not is_datetime64tz_dtype(x)
else np.asarray(x.astype(object))
for x in to_concat
Expand All @@ -183,6 +185,15 @@ def concat_categorical(to_concat, axis: int 6D40 = 0):
return result


def _safe_array(x):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so i actually like this as a real function; can you move to pandas.compat.numpy and use everywhere?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you do this one

# FIXME: kludge
with warnings.catch_warnings():
# See https://github.com/numpy/numpy/issues/15041
warnings.filterwarnings("ignore", ".*with automatic object dtype.*")
arr = np.asarray(x)
return arr


def union_categoricals(
to_union, sort_categories: bool = False, ignore_order: bool = False
):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def test_scientific_no_exponent(self):
def test_convert_non_hashable(self):
# GH13324
# make sure that we are handing non-hashables
arr = np.array([[10.0, 2], 1.0, "apple"])
arr = np.array([[10.0, 2], 1.0, "apple"], dtype=object)
result = lib.maybe_convert_numeric(arr, set(), False, True)
tm.assert_numpy_array_equal(result, np.array([np.nan, 1.0, np.nan]))

Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/extension/base/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ def test_memory_usage(self, data):
assert result == s.nbytes

def test_array_interface(self, data):
result = np.array(data)
if hasattr(data, "dtype") and data.dtype.kind == "O":
# e.g. JSONArray
result = np.array(data, dtype=object)
else:
result = np.array(data)
assert result[0] == data[0]

result = np.array(data, dtype=object)
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/io/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def test_same_ordering(datapath):
assert_framelist_equal(dfs_lxml, dfs_bs4)


@td.skip_if_no("bs4")
@pytest.mark.parametrize(
"flavor",
[
Expand Down
0