Skip to content

numpydev ragged array dtype warning #31203

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 5 commits into from
Jan 22, 2020
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
2 changes: 1 addition & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2058,7 +2058,7 @@ def drop(self, codes, level=None, errors="raise"):

if not isinstance(codes, (np.ndarray, Index)):
try:
codes = com.index_labels_to_array(codes)
codes = com.index_labels_to_array(codes, dtype=object)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI, at least in our tests, this isn't changing behavior. I added an assert com.index_labels_to_array(codes, dtype=object).dtype == com.index_labels_to_array(codes) temporarily, so we were always inferring object dtype here (in our tests).

except ValueError:
pass

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def cat_core(list_of_columns: List, sep: str):
return np.sum(arr_of_cols, axis=0)
list_with_sep = [sep] * (2 * len(list_of_columns) - 1)
list_with_sep[::2] = list_of_columns
arr_with_sep = np.asarray(list_with_sep)
arr_with_sep = np.asarray(list_with_sep, dtype=object)
Copy link
Member

Choose a reason for hiding this comment

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

darn, i thought i had already fixed this one

return np.sum(arr_with_sep, axis=0)


Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/categorical/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,6 @@ def test_constructor_imaginary(self):
@pytest.mark.skipif(_np_version_under1p16, reason="Skipping for NumPy <1.16")
def test_constructor_string_and_tuples(self):
# GH 21416
c = pd.Categorical(["c", ("a", "b"), ("b", "a"), "c"])
c = pd.Categorical(np.array(["c", ("a", "b"), ("b", "a"), "c"], dtype=object))
expected_index = pd.Index([("a", "b"), ("b", "a"), "c"])
assert c.categories.equals(expected_index)
2 changes: 1 addition & 1 deletion pandas/tests/arrays/categorical/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_fillna_iterable_category(self, named):
Point = collections.namedtuple("Point", "x y")
else:
Point = lambda *args: args # tuple
cat = Categorical([Point(0, 0), Point(0, 1), None])
cat = Categorical(np.array([Point(0, 0), Point(0, 1), None], dtype=object))
result = cat.fillna(Point(0, 0))
expected = Categorical([Point(0, 0), Point(0, 1), Point(0, 0)])

Expand Down
10 changes: 7 additions & 3 deletions pandas/tests/extension/base/getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ def test_take_non_na_fill_value(self, data_missing):
fill_value = data_missing[1] # valid
na = data_missing[0]

array = data_missing._from_sequence([na, fill_value, na])
array = data_missing._from_sequence(
[na, fill_value, na], dtype=data_missing.dtype
)
Copy link
Member

Choose a reason for hiding this comment

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

this sort of changes the interface. do we want authors to handle this on their own?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How does it change the interface?

We are reducing coverage of _from_sequence inferring the dtype from an untyped list. We could restore that if desired (and probably skip for problematic arrays).

Copy link
Member

Choose a reason for hiding this comment

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

We are reducing coverage of _from_sequence inferring the dtype from an untyped list.

Yah, i guess that is a better description than "changes the interface"

result = array.take([-1, 1], fill_value=fill_value, allow_fill=True)
expected = array.take([1, 1])
self.assert_extension_array_equal(result, expected)
Expand Down Expand Up @@ -293,10 +295,12 @@ def test_reindex_non_na_fill_value(self, data_missing):
valid = data_missing[1]
na = data_missing[0]

array = data_missing._from_sequence([na, valid])
array = data_missing._from_sequence([na, valid], dtype=data_missing.dtype)
ser = pd.Series(array)
result = ser.reindex([0, 1, 2], fill_value=valid)
expected = pd.Series(data_missing._from_sequence([na, valid, valid]))
expected = pd.Series(
data_missing._from_sequence([na, valid, valid], dtype=data_missing.dtype)
)

self.assert_series_equal(result, expected)

Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/extension/json/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ def __setitem__(self, key, value):
def __len__(self) -> int:
return len(self.data)

def __array__(self, dtype=None):
if dtype is None:
dtype = object
return np.asarray(self.data, dtype=dtype)

@property
def nbytes(self) -> int:
return sys.getsizeof(self.data)
Expand Down
4 changes: 0 additions & 4 deletions pandas/tests/extension/json/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,6 @@ def test_unstack(self, data, index):
# this matches otherwise
return super().test_unstack(data, index)

@pytest.mark.xfail(reason="Inconsistent sizes.")
def test_transpose(self, data):
super().test_transpose(data)


class TestGetitem(BaseJSON, base.BaseGetitemTests):
pass
Expand Down