Skip to content

REGR: CategoricalIndex(None) #41612

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 3 commits into from
May 25, 2021
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ Build
Deprecations
~~~~~~~~~~~~
- Deprecated allowing scalars to be passed to the :class:`Categorical` constructor (:issue:`38433`)
- Deprecated constructing :class:`CategoricalIndex` without passing list-like data (:issue:`38944`)
- Deprecated allowing subclass-specific keyword arguments in the :class:`Index` constructor, use the specific subclass directly instead (:issue:`14093`, :issue:`21311`, :issue:`22315`, :issue:`26974`)
- Deprecated ``astype`` of datetimelike (``timedelta64[ns]``, ``datetime64[ns]``, ``Datetime64TZDtype``, ``PeriodDtype``) to integer dtypes, use ``values.view(...)`` instead (:issue:`38544`)
- Deprecated :meth:`MultiIndex.is_lexsorted` and :meth:`MultiIndex.lexsort_depth`, use :meth:`MultiIndex.is_monotonic_increasing` instead (:issue:`32259`)
Expand Down
11 changes: 11 additions & 0 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@ def __new__(

name = maybe_extract_name(name, data, cls)

if data is None:
# GH#38944
warnings.warn(
"Constructing a CategoricalIndex without passing data is "
"deprecated and will raise in a future version. "
"Use CategoricalIndex([], ...) instead",
FutureWarning,
stacklevel=2,
)
data = []

if is_scalar(data):
raise cls._scalar_data_error(data)

Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/indexes/categorical/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ def test_can_hold_identifiers(self):
key = idx[0]
assert idx._can_hold_identifiers_and_holds_name(key) is True

def test_pickle_compat_construction(self):
# Once the deprecation is enforced, we can use the parent class's test
with tm.assert_produces_warning(FutureWarning, match="without passing data"):
self._index_cls()

def test_insert(self, simple_index):

ci = simple_index
Expand Down
9 changes: 8 additions & 1 deletion pandas/tests/indexes/categorical/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@


class TestCategoricalIndexConstructors:
def test_construction_without_data_deprecated(self):
# Once the deprecation is enforced, we can add this case to
# test_construction_disallows_scalar
msg = "without passing data"
with tm.assert_produces_warning(FutureWarning, match=msg):
CategoricalIndex(categories=list("abcd"), ordered=False)
Copy link
Member

Choose a reason for hiding this comment

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

in #38614 some tests were changed. so I guess we should also be checking the result as well as that a warning is raised.


def test_construction_disallows_scalar(self):
msg = "must be called with a collection of some kind"
Copy link
Member

Choose a reason for hiding this comment

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

we still want to test this message with a scalar passed

with pytest.raises(TypeError, match=msg):
CategoricalIndex(categories=list("abcd"), ordered=False)
CategoricalIndex(data=1, categories=list("abcd"), ordered=False)

def test_construction(self):

Expand Down
16 changes: 11 additions & 5 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,17 @@ def create_index(self) -> Index:

def test_pickle_compat_construction(self):
# need an object to create with
msg = (
r"Index\(\.\.\.\) must be called with a collection of some "
r"kind, None was passed|"
r"__new__\(\) missing 1 required positional argument: 'data'|"
r"__new__\(\) takes at least 2 arguments \(1 given\)"
msg = "|".join(
[
r"Index\(\.\.\.\) must be called with a collection of some "
r"kind, None was passed",
r"DatetimeIndex\(\) must be called with a collection of some "
r"kind, None was passed",
r"TimedeltaIndex\(\) must be called with a collection of some "
r"kind, None was passed",
r"__new__\(\) missing 1 required positional argument: 'data'",
r"__new__\(\) takes at least 2 arguments \(1 given\)",
]
)
with pytest.raises(TypeError, match=msg):
self._index_cls()
Expand Down
3 changes: 0 additions & 3 deletions pandas/tests/indexes/datetimes/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ def test_format(self, simple_index):
def test_shift(self):
pass # handled in test_ops

def test_pickle_compat_construction(self):
pass

def test_intersection(self):
pass # handled in test_setops

Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ def simple_index(self) -> Index:
def index(self, request):
return request.param

@pytest.mark.xfail(reason="Goes through a generate_range path")
def test_pickle_compat_construction(self):
pass
super().test_pickle_compat_construction()

@pytest.mark.parametrize("freq", ["D", "M", "A"])
def test_pickle_round_trip(self, freq):
Expand Down
3 changes: 0 additions & 3 deletions pandas/tests/indexes/timedeltas/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ def test_numeric_compat(self):
def test_shift(self):
pass # this is handled in test_arithmetic.py

def test_pickle_compat_construction(self):
pass

def test_pickle_after_set_freq(self):
tdi = timedelta_range("1 day", periods=4, freq="s")
tdi = tdi._with_freq(None)
Expand Down