Skip to content
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 doc/source/whatsnew/v0.21.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Numeric
Categorical
^^^^^^^^^^^

-
- Bug in :meth:`DataFrame.astype` where casting to 'category' on an empty ``DataFrame`` causes a segmentation fault (:issue:`18004`)
-
-

Expand Down
6 changes: 3 additions & 3 deletions pandas/_libs/src/inference.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,13 @@ def infer_dtype(object value, bint skipna=False):
if values.dtype != np.object_:
values = values.astype('O')

# make contiguous
values = values.ravel()

n = len(values)
if n == 0:
return 'empty'

# make contiguous
values = values.ravel()

# try to use a valid value
for i in range(n):
val = util.get_value_1d(values, i)
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,12 @@ def test_length_zero(self):
result = lib.infer_dtype([])
assert result == 'empty'

# GH 18004
arr = np.array([np.array([], dtype=object),
np.array([], dtype=object)])
result = lib.infer_dtype(arr)
assert result == 'empty'

def test_integers(self):
arr = np.array([1, 2, 3, np.int64(4), np.int32(5)], dtype='O')
result = lib.infer_dtype(arr)
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2124,6 +2124,13 @@ def test_creation_astype(self):
res = s.astype(CategoricalDtype(list('abcdef'), ordered=True))
tm.assert_series_equal(res, exp)

@pytest.mark.parametrize('columns', [['x'], ['x', 'y'], ['x', 'y', 'z']])
def test_empty_astype(self, columns):
# GH 18004
msg = '> 1 ndim Categorical are not supported at this time'
with tm.assert_raises_regex(NotImplementedError, msg):
DataFrame(columns=columns).astype('category')

def test_construction_series(self):

l = [1, 2, 3, 1]
Expand Down