Skip to content

Commit 10da3ae

Browse files
sinhrksjreback
authored andcommitted
BUG: RangeIndex can be created without args
closes pandas-dev#13793 Author: sinhrks <[email protected]> Closes pandas-dev#13803 from sinhrks/range_none and squashes the following commits: aab6ae6 [sinhrks] BUG: RangeIndex can be created without args
1 parent 2d3ede6 commit 10da3ae

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

doc/source/whatsnew/v0.19.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,7 @@ Bug Fixes
766766
- Bug in ``DatetimeIndex`` with nanosecond frequency does not include timestamp specified with ``end`` (:issue:`13672`)
767767

768768
- Bug in ``Index`` raises ``OutOfBoundsDatetime`` if ``datetime`` exceeds ``datetime64[ns]`` bounds, rather than coercing to ``object`` dtype (:issue:`13663`)
769+
- Bug in ``RangeIndex`` can be created without no arguments rather than raises ``TypeError`` (:issue:`13793`)
769770
- Bug in ``.value_counts`` raises ``OutOfBoundsDatetime`` if data exceeds ``datetime64[ns]`` bounds (:issue:`13663`)
770771
- Bug in ``DatetimeIndex`` may raise ``OutOfBoundsDatetime`` if input ``np.datetime64`` has other unit than ``ns`` (:issue:`9114`)
771772
- Bug in ``isnull`` ``notnull`` raise ``TypeError`` if input datetime-like has other unit than ``ns`` (:issue:`13389`)

pandas/indexes/range.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ def _ensure_int(value, field):
7070

7171
return new_value
7272

73-
if start is None:
73+
if start is None and stop is None and step is None:
74+
msg = "RangeIndex(...) must be called with integers"
75+
raise TypeError(msg)
76+
elif start is None:
7477
start = 0
7578
else:
7679
start = _ensure_int(start, 'start')
@@ -122,8 +125,13 @@ def _simple_new(cls, start, stop=None, step=None, name=None,
122125
result = object.__new__(cls)
123126

124127
# handle passed None, non-integers
128+
if start is None and stop is None:
129+
# empty
130+
start, stop, step = 0, 0, 1
131+
125132
if start is None or not is_integer(start):
126133
try:
134+
127135
return RangeIndex(start, stop, step, name=name, **kwargs)
128136
except TypeError:
129137
return Index(start, stop, step, name=name, **kwargs)

pandas/tests/indexes/test_range.py

+28-17
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,28 @@ def test_constructor(self):
7474
self.assertEqual(index._step, 2)
7575
tm.assert_index_equal(Index(expected), index)
7676

77-
index = RangeIndex()
78-
expected = np.empty(0, dtype=np.int64)
79-
self.assertIsInstance(index, RangeIndex)
80-
self.assertEqual(index._start, 0)
81-
self.assertEqual(index._stop, 0)
82-
self.assertEqual(index._step, 1)
83-
tm.assert_index_equal(Index(expected), index)
84-
85-
index = RangeIndex(name='Foo')
86-
self.assertIsInstance(index, RangeIndex)
87-
self.assertEqual(index.name, 'Foo')
77+
msg = "RangeIndex\\(\\.\\.\\.\\) must be called with integers"
78+
with tm.assertRaisesRegexp(TypeError, msg):
79+
RangeIndex()
80+
81+
for index in [RangeIndex(0), RangeIndex(start=0), RangeIndex(stop=0),
82+
RangeIndex(0, 0)]:
83+
expected = np.empty(0, dtype=np.int64)
84+
self.assertIsInstance(index, RangeIndex)
85+
self.assertEqual(index._start, 0)
86+
self.assertEqual(index._stop, 0)
87+
self.assertEqual(index._step, 1)
88+
tm.assert_index_equal(Index(expected), index)
89+
90+
with tm.assertRaisesRegexp(TypeError, msg):
91+
RangeIndex(name='Foo')
92+
93+
for index in [RangeIndex(0, name='Foo'),
94+
RangeIndex(start=0, name='Foo'),
95+
RangeIndex(stop=0, name='Foo'),
96+
RangeIndex(0, 0, name='Foo')]:
97+
self.assertIsInstance(index, RangeIndex)
98+
self.assertEqual(index.name, 'Foo')
8899

89100
# we don't allow on a bare Index
90101
self.assertRaises(TypeError, lambda: Index(0, 1000))
@@ -210,10 +221,10 @@ def test_numeric_compat2(self):
210221
RangeIndex(0, 1000, 1)._int64index // 2),
211222
(RangeIndex(0, 100, 1), 2.0,
212223
RangeIndex(0, 100, 1)._int64index // 2.0),
213-
(RangeIndex(), 50, RangeIndex()),
224+
(RangeIndex(0), 50, RangeIndex(0)),
214225
(RangeIndex(2, 4, 2), 3, RangeIndex(0, 1, 1)),
215226
(RangeIndex(-5, -10, -6), 4, RangeIndex(-2, -1, 1)),
216-
(RangeIndex(-100, -200, 3), 2, RangeIndex())]
227+
(RangeIndex(-100, -200, 3), 2, RangeIndex(0))]
217228
for idx, div, expected in cases_exact:
218229
tm.assert_index_equal(idx // div, expected, exact=True)
219230

@@ -288,7 +299,7 @@ def test_delete(self):
288299
def test_view(self):
289300
super(TestRangeIndex, self).test_view()
290301

291-
i = RangeIndex(name='Foo')
302+
i = RangeIndex(0, name='Foo')
292303
i_view = i.view()
293304
self.assertEqual(i_view.name, 'Foo')
294305

@@ -612,16 +623,16 @@ def test_union(self):
612623
(RI(0, 100, 5), RI(0, 100, 20), RI(0, 100, 5)),
613624
(RI(0, -100, -5), RI(5, -100, -20), RI(-95, 10, 5)),
614625
(RI(0, -11, -1), RI(1, -12, -4), RI(-11, 2, 1)),
615-
(RI(), RI(), RI()),
616-
(RI(0, -10, -2), RI(), RI(0, -10, -2)),
626+
(RI(0), RI(0), RI(0)),
627+
(RI(0, -10, -2), RI(0), RI(0, -10, -2)),
617628
(RI(0, 100, 2), RI(100, 150, 200), RI(0, 102, 2)),
618629
(RI(0, -100, -2), RI(-100, 50, 102), RI(-100, 4, 2)),
619630
(RI(0, -100, -1), RI(0, -50, -3), RI(-99, 1, 1)),
620631
(RI(0, 1, 1), RI(5, 6, 10), RI(0, 6, 5)),
621632
(RI(0, 10, 5), RI(-5, -6, -20), RI(-5, 10, 5)),
622633
(RI(0, 3, 1), RI(4, 5, 1), I64([0, 1, 2, 4])),
623634
(RI(0, 10, 1), I64([]), RI(0, 10, 1)),
624-
(RI(), I64([1, 5, 6]), I64([1, 5, 6]))]
635+
(RI(0), I64([1, 5, 6]), I64([1, 5, 6]))]
625636
for idx1, idx2, expected in cases:
626637
res1 = idx1.union(idx2)
627638
res2 = idx2.union(idx1)

0 commit comments

Comments
 (0)