-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
ENH: Categorical.empty #40602
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
jreback
merged 13 commits into
pandas-dev:master
from
jbrockmendel:enh-categorical-empty
Apr 13, 2021
Merged
ENH: Categorical.empty #40602
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
967c8d0
ENH: Categorical.empty
jbrockmendel 7ccbddc
Merge branch 'master' into enh-categorical-empty
jbrockmendel 83dbbec
Merge branch 'master' into enh-categorical-empty
jbrockmendel e9e43b0
Merge branch 'master' into enh-categorical-empty
jbrockmendel 9565f8f
remove unused
jbrockmendel 0654fbf
Merge branch 'master' into enh-categorical-empty
jbrockmendel 1c20fcf
Merge branch 'master' into enh-categorical-empty
jbrockmendel 0fc8215
ENH: implement EA.empty
jbrockmendel af77718
Merge branch 'master' into enh-categorical-empty
jbrockmendel d674661
Merge branch 'master' into enh-categorical-empty
jbrockmendel ec77a14
post-merge fixup
jbrockmendel 6731ec1
Merge branch 'master' into enh-categorical-empty
jbrockmendel 7d8328b
empty -> _empty
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
""" | ||
Tests for subclasses of NDArrayBackedExtensionArray | ||
""" | ||
import numpy as np | ||
import pytest | ||
|
||
from pandas import ( | ||
CategoricalIndex, | ||
date_range, | ||
) | ||
from pandas.core.arrays import ( | ||
Categorical, | ||
DatetimeArray, | ||
PandasArray, | ||
PeriodArray, | ||
TimedeltaArray, | ||
) | ||
|
||
|
||
@pytest.fixture( | ||
params=[Categorical, DatetimeArray, TimedeltaArray, PeriodArray, PandasArray] | ||
) | ||
def ea_subclass(request): | ||
""" | ||
Fixture for subclasses of NDArrayBackedExtensionArray. | ||
""" | ||
return request.param | ||
|
||
|
||
class TestEmpty: | ||
# def test_empty(self, ea_subclass): | ||
|
||
def test_empty_categorical(self): | ||
ci = CategoricalIndex(["a", "b", "c"], ordered=True) | ||
dtype = ci.dtype | ||
|
||
# case with int8 codes | ||
shape = (4,) | ||
result = Categorical.empty(shape, dtype=dtype) | ||
assert isinstance(result, Categorical) | ||
assert result.shape == shape | ||
assert result._ndarray.dtype == np.int8 | ||
|
||
# case where repr would segfault if we didn't override base implementation | ||
result = Categorical.empty((4096,), dtype=dtype) | ||
assert isinstance(result, Categorical) | ||
assert result.shape == (4096,) | ||
assert result._ndarray.dtype == np.int8 | ||
repr(result) | ||
|
||
# case with int16 codes | ||
ci = CategoricalIndex(list(range(512)) * 4, ordered=False) | ||
dtype = ci.dtype | ||
result = Categorical.empty(shape, dtype=dtype) | ||
assert isinstance(result, Categorical) | ||
assert result.shape == shape | ||
assert result._ndarray.dtype == np.int16 | ||
|
||
def test_empty_dt64tz(self): | ||
dti = date_range("2016-01-01", periods=2, tz="Asia/Tokyo") | ||
dtype = dti.dtype | ||
|
||
shape = (0,) | ||
result = DatetimeArray.empty(shape, dtype=dtype) | ||
assert result.dtype == dtype | ||
assert isinstance(result, DatetimeArray) | ||
assert result.shape == shape | ||
|
||
def test_empty_dt64(self): | ||
shape = (3, 9) | ||
result = DatetimeArray.empty(shape, dtype="datetime64[ns]") | ||
assert isinstance(result, DatetimeArray) | ||
assert result.shape == shape | ||
|
||
def test_empty_td64(self): | ||
shape = (3, 9) | ||
result = TimedeltaArray.empty(shape, dtype="m8[ns]") | ||
assert isinstance(result, TimedeltaArray) | ||
assert result.shape == shape | ||
|
||
def test_empty_pandas_array(self): | ||
arr = PandasArray(np.array([1, 2])) | ||
dtype = arr.dtype | ||
|
||
shape = (3, 9) | ||
result = PandasArray.empty(shape, dtype=dtype) | ||
assert isinstance(result, PandasArray) | ||
assert result.dtype == dtype | ||
assert result.shape == shape |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.