Skip to content

Commit

Permalink
CLN: remove dtype kwarg from _simple_new (#32260)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel committed Feb 26, 2020
1 parent a713063 commit 1b8b428
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 23 deletions.
4 changes: 2 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ def asi8(self):
return None

@classmethod
def _simple_new(cls, values, name=None, dtype=None):
def _simple_new(cls, values, name: Label = None):
"""
We require that we have a dtype compat for the values. If we are passed
a non-dtype compat, then coerce using the constructor.
Expand Down Expand Up @@ -3310,7 +3310,7 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None):
values = range(0)
else:
values = self._data[:0] # appropriately-dtyped empty array
target = self._simple_new(values, dtype=self.dtype, **attrs)
target = self._simple_new(values, **attrs)
else:
target = ensure_index(target)

Expand Down
5 changes: 2 additions & 3 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ def _create_from_codes(self, codes, dtype=None, name=None):
return CategoricalIndex(cat, name=name)

@classmethod
def _simple_new(cls, values: Categorical, name=None, dtype=None):
# GH#32204 dtype is included for compat with Index._simple_new
def _simple_new(cls, values: Categorical, name: Label = None):
assert isinstance(values, Categorical), type(values)
result = object.__new__(cls)

Expand Down Expand Up @@ -433,7 +432,7 @@ def where(self, cond, other=None):
other = self._na_value
values = np.where(cond, self.values, other)
cat = Categorical(values, dtype=self.dtype)
return self._shallow_copy(cat)
return type(self)._simple_new(cat, name=self.name)

def reindex(self, target, method=None, level=None, limit=None, tolerance=None):
"""
Expand Down
10 changes: 4 additions & 6 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from pandas._libs import Timedelta, Timestamp, lib
from pandas._libs.interval import Interval, IntervalMixin, IntervalTree
from pandas._typing import AnyArrayLike
from pandas._typing import AnyArrayLike, Label
from pandas.util._decorators import Appender, Substitution, cache_readonly
from pandas.util._exceptions import rewrite_exception

Expand Down Expand Up @@ -191,7 +191,7 @@ def func(intvidx_self, other, sort=False):
class IntervalIndex(IntervalMixin, ExtensionIndex):
_typ = "intervalindex"
_comparables = ["name"]
_attributes = ["name", "closed"]
_attributes = ["name"]

# we would like our indexing holder to defer to us
_defer_to_indexing = True
Expand Down Expand Up @@ -227,17 +227,15 @@ def __new__(
return cls._simple_new(array, name)

@classmethod
def _simple_new(cls, array, name, closed=None):
def _simple_new(cls, array: IntervalArray, name: Label = None):
"""
Construct from an IntervalArray
Parameters
----------
array : IntervalArray
name : str
name : Label, default None
Attached as result.name
closed : Any
Ignored.
"""
assert isinstance(array, IntervalArray), type(array)

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def __new__(
return cls._simple_new(data, name=name)

@classmethod
def _simple_new(cls, values, name=None, freq=None, **kwargs):
def _simple_new(cls, values: PeriodArray, name: Label = None):
"""
Create a new PeriodIndex.
Expand All @@ -228,7 +228,6 @@ def _simple_new(cls, values, name=None, freq=None, **kwargs):
or coercion.
"""
assert isinstance(values, PeriodArray), type(values)
assert freq is None or freq == values.freq, (freq, values.freq)

result = object.__new__(cls)
result._data = values
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def __new__(
# RangeIndex
if isinstance(start, RangeIndex):
start = start._range
return cls._simple_new(start, dtype=dtype, name=name)
return cls._simple_new(start, name=name)

# validate the arguments
if com.all_none(start, stop, step):
Expand All @@ -113,7 +113,7 @@ def __new__(
raise ValueError("Step must not be zero")

rng = range(start, stop, step)
return cls._simple_new(rng, dtype=dtype, name=name)
return cls._simple_new(rng, name=name)

@classmethod
def from_range(cls, data: range, name=None, dtype=None) -> "RangeIndex":
Expand All @@ -131,10 +131,10 @@ def from_range(cls, data: range, name=None, dtype=None) -> "RangeIndex":
)

cls._validate_dtype(dtype)
return cls._simple_new(data, dtype=dtype, name=name)
return cls._simple_new(data, name=name)

@classmethod
def _simple_new(cls, values: range, name=None, dtype=None) -> "RangeIndex":
def _simple_new(cls, values: range, name: Label = None) -> "RangeIndex":
result = object.__new__(cls)

assert isinstance(values, range)
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/indexes/period/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@ def test_constructor_simple_new(self):
idx = period_range("2007-01", name="p", periods=2, freq="M")

with pytest.raises(AssertionError, match="<class .*PeriodIndex'>"):
idx._simple_new(idx, name="p", freq=idx.freq)
idx._simple_new(idx, name="p")

result = idx._simple_new(idx._data, name="p", freq=idx.freq)
result = idx._simple_new(idx._data, name="p")
tm.assert_index_equal(result, idx)

with pytest.raises(AssertionError):
Expand All @@ -339,19 +339,19 @@ def test_constructor_simple_new_empty(self):
# GH13079
idx = PeriodIndex([], freq="M", name="p")
with pytest.raises(AssertionError, match="<class .*PeriodIndex'>"):
idx._simple_new(idx, name="p", freq="M")
idx._simple_new(idx, name="p")

result = idx._simple_new(idx._data, name="p", freq="M")
result = idx._simple_new(idx._data, name="p")
tm.assert_index_equal(result, idx)

@pytest.mark.parametrize("floats", [[1.1, 2.1], np.array([1.1, 2.1])])
def test_constructor_floats(self, floats):
with pytest.raises(AssertionError, match="<class "):
PeriodIndex._simple_new(floats, freq="M")
PeriodIndex._simple_new(floats)

msg = "PeriodIndex does not allow floating point in construction"
with pytest.raises(TypeError, match=msg):
PeriodIndex(floats, freq="M")
PeriodIndex(floats)

def test_constructor_nat(self):
msg = "start and end must not be NaT"
Expand Down

0 comments on commit 1b8b428

Please sign in to comment.