Repository: pandas-dev/pandas

Issue:
RangeIndex.append() should return RangeIndex when possible
#### Code Sample, a copy-pastable example if possible

```python
In [2]: i1, i2 = pd.RangeIndex(2), pd.RangeIndex(2, 4)

In [3]: i1.union(i2)
Out[3]: RangeIndex(start=0, stop=4, step=1)

In [4]: i1.append(i2)
Out[4]: Int64Index([0, 1, 2, 3], dtype='int64')
```
#### Problem description

It would be nice if appending consecutive ``RangeIndex`` returned a ``RangeIndex``.

This was briefly discussed [here](https://github.com/pandas-dev/pandas/pull/12289#r71085353).

#### Expected Output

Same as ``Out[3]:``

PR on its way.

#### Output of ``pd.show_versions()``

<details>
INSTALLED VERSIONS
------------------
commit: f1549668c84b481a67547bb46261d1394c8dd3e7
python: 3.5.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.7.0-1-amd64
machine: x86_64
processor: 
byteorder: little
LC_ALL: None
LANG: it_IT.utf8
LOCALE: it_IT.UTF-8

pandas: 0.20.0rc1+49.gf1549668c
pytest: 3.0.6
pip: 9.0.1
setuptools: 33.1.1
Cython: 0.25.2
numpy: 1.12.0
scipy: 0.18.1
xarray: 0.9.1
IPython: 5.1.0.dev
sphinx: 1.4.9
patsy: 0.3.0-dev
dateutil: 2.5.3
pytz: 2016.7
blosc: None
bottleneck: 1.2.0
tables: 3.3.0
numexpr: 2.6.1
feather: 0.3.1
matplotlib: 2.0.0
openpyxl: 2.3.0
xlrd: 1.0.0
xlwt: 1.1.2
xlsxwriter: 0.9.6
lxml: 3.7.1
bs4: 4.5.3
html5lib: 0.999999999
sqlalchemy: 1.0.15
pymysql: None
psycopg2: None
jinja2: 2.8
s3fs: None
pandas_gbq: None
pandas_datareader: 0.2.1


</details>



Discussion:

Resolving patch:
diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py
--- a/pandas/core/indexes/range.py
+++ b/pandas/core/indexes/range.py
@@ -443,6 +443,63 @@ def join(self, other, how='left', level=None, return_indexers=False,
         return super(RangeIndex, self).join(other, how, level, return_indexers,
                                             sort)
 
+    def append(self, other):
+        """
+        Append a collection of Index options together
+
+        Parameters
+        ----------
+        other : Index or list/tuple of indices
+
+        Returns
+        -------
+        appended : RangeIndex if all indexes are consecutive RangeIndexes,
+                   otherwise Int64Index or Index
+        """
+
+        to_concat = [self]
+
+        if isinstance(other, (list, tuple)):
+            to_concat = to_concat + list(other)
+        else:
+            to_concat.append(other)
+
+        if not all([isinstance(i, RangeIndex) for i in to_concat]):
+            return super(RangeIndex, self).append(other)
+
+        start = step = next = None
+
+        for obj in to_concat:
+            if not len(obj):
+                continue
+
+            if start is None:
+                # This is set by the first non-empty index
+                start = obj._start
+                if step is None and len(obj) > 1:
+                    step = obj._step
+            elif step is None:
+                # First non-empty index had only one element
+                if obj._start == start:
+                    return super(RangeIndex, self).append(other)
+                step = obj._start - start
+
+            non_consecutive = ((step != obj._step and len(obj) > 1) or
+                               (next is not None and obj._start != next))
+            if non_consecutive:
+                return super(RangeIndex, self).append(other)
+
+            if step is not None:
+                next = obj[-1] + step
+
+        if start is None:
+            start = obj._start
+            step = obj._step
+        stop = obj._stop if next is None else next
+        names = set([obj.name for obj in to_concat])
+        name = None if len(names) > 1 else self.name
+        return RangeIndex(start, stop, step, name=name)
+
     def __len__(self):
         """
         return the length of the RangeIndex


Test patch:
diff --git a/pandas/tests/indexes/test_range.py b/pandas/tests/indexes/test_range.py
--- a/pandas/tests/indexes/test_range.py
+++ b/pandas/tests/indexes/test_range.py
@@ -941,3 +941,39 @@ def test_where_array_like(self):
         for klass in klasses:
             result = i.where(klass(cond))
             tm.assert_index_equal(result, expected)
+
+    def test_append(self):
+        # GH16212
+        RI = RangeIndex
+        I64 = Int64Index
+        F64 = Float64Index
+        OI = Index
+        cases = [([RI(1, 12, 5)], RI(1, 12, 5)),
+                 ([RI(0, 6, 4)], RI(0, 6, 4)),
+                 ([RI(1, 3), RI(3, 7)], RI(1, 7)),
+                 ([RI(1, 5, 2), RI(5, 6)], RI(1, 6, 2)),
+                 ([RI(1, 3, 2), RI(4, 7, 3)], RI(1, 7, 3)),
+                 ([RI(-4, 3, 2), RI(4, 7, 2)], RI(-4, 7, 2)),
+                 ([RI(-4, -8), RI(-8, -12)], RI(-8, -12)),
+                 ([RI(-4, -8), RI(3, -4)], RI(3, -8)),
+                 ([RI(-4, -8), RI(3, 5)], RI(3, 5)),
+                 ([RI(-4, -2), RI(3, 5)], I64([-4, -3, 3, 4])),
+                 ([RI(-2,), RI(3, 5)], RI(3, 5)),
+                 ([RI(2,), RI(2)], I64([0, 1, 0, 1])),
+                 ([RI(2,), RI(2, 5), RI(5, 8, 4)], RI(0, 6)),
+                 ([RI(2,), RI(3, 5), RI(5, 8, 4)], I64([0, 1, 3, 4, 5])),
+                 ([RI(-2, 2), RI(2, 5), RI(5, 8, 4)], RI(-2, 6)),
+                 ([RI(3,), I64([-1, 3, 15])], I64([0, 1, 2, -1, 3, 15])),
+                 ([RI(3,), F64([-1, 3.1, 15.])], F64([0, 1, 2, -1, 3.1, 15.])),
+                 ([RI(3,), OI(['a', None, 14])], OI([0, 1, 2, 'a', None, 14])),
+                 ([RI(3, 1), OI(['a', None, 14])], OI(['a', None, 14]))
+                 ]
+
+        for indices, expected in cases:
+            result = indices[0].append(indices[1:])
+            tm.assert_index_equal(result, expected, exact=True)
+
+            if len(indices) == 2:
+                # Append single item rather than list
+                result2 = indices[0].append(indices[1])
+                tm.assert_index_equal(result2, expected, exact=True)