Repository: pandas-dev/pandas

Issue:
Constructing DataFrames using range fails
Working on removing ``compat.lrange``, I've discovered that 1-dim lists, iterators and generators work as expected:

```python
>>> pd.DataFrame(list(range(3)))  # ok
   0
0  0
1  1
2  2
>>> pd.DataFrame(iter(range(3)))  # ok
   0
0  0
1  1
2  2
>>> pd.DataFrame((i for i in range(3))  # ok
   0
0  0
1  1
2  2
```

while constructing with a plain ``range`` does not work:

```python
>>> pd.DataFrame(range(3))  # not ok
AttributeError: 'range' object has no attribute 'ndim'
```

Fix upcoming.


Discussion:

Resolving patch:
diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py
--- a/pandas/core/dtypes/cast.py
+++ b/pandas/core/dtypes/cast.py
@@ -35,8 +35,8 @@
 def maybe_convert_platform(values):
     """ try to do platform conversion, allow ndarray or list here """
 
-    if isinstance(values, (list, tuple)):
-        values = construct_1d_object_array_from_listlike(list(values))
+    if isinstance(values, (list, tuple, range)):
+        values = construct_1d_object_array_from_listlike(values)
     if getattr(values, 'dtype', None) == np.object_:
         if hasattr(values, '_values'):
             values = values._values


Test patch:
diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py
--- a/pandas/tests/frame/test_constructors.py
+++ b/pandas/tests/frame/test_constructors.py
@@ -33,11 +33,13 @@ class TestDataFrameConstructors(TestData):
         lambda: DataFrame(()),
         lambda: DataFrame([]),
         lambda: DataFrame((x for x in [])),
+        lambda: DataFrame(range(0)),
         lambda: DataFrame(data=None),
         lambda: DataFrame(data={}),
         lambda: DataFrame(data=()),
         lambda: DataFrame(data=[]),
-        lambda: DataFrame(data=(x for x in []))
+        lambda: DataFrame(data=(x for x in [])),
+        lambda: DataFrame(data=range(0)),
     ])
     def test_empty_constructor(self, constructor):
         expected = DataFrame()
@@ -999,6 +1001,17 @@ def __len__(self, n):
                             array.array('i', range(10))])
         tm.assert_frame_equal(result, expected, check_dtype=False)
 
+    def test_constructor_range(self):
+        # GH26342
+        result = DataFrame(range(10))
+        expected = DataFrame(list(range(10)))
+        tm.assert_frame_equal(result, expected)
+
+    def test_constructor_list_of_ranges(self):
+        result = DataFrame([range(10), range(10)])
+        expected = DataFrame([list(range(10)), list(range(10))])
+        tm.assert_frame_equal(result, expected)
+
     def test_constructor_iterable(self):
         # GH 21987
         class Iter:
@@ -1011,9 +1024,13 @@ def __iter__(self):
         tm.assert_frame_equal(result, expected)
 
     def test_constructor_iterator(self):
+        result = DataFrame(iter(range(10)))
+        expected = DataFrame(list(range(10)))
+        tm.assert_frame_equal(result, expected)
 
+    def test_constructor_list_of_iterators(self):
+        result = DataFrame([iter(range(10)), iter(range(10))])
         expected = DataFrame([list(range(10)), list(range(10))])
-        result = DataFrame([range(10), range(10)])
         tm.assert_frame_equal(result, expected)
 
     def test_constructor_generator(self):
@@ -2251,8 +2268,13 @@ def test_to_frame_with_falsey_names(self):
 
     @pytest.mark.parametrize('dtype', [None, 'uint8', 'category'])
     def test_constructor_range_dtype(self, dtype):
-        # GH 16804
         expected = DataFrame({'A': [0, 1, 2, 3, 4]}, dtype=dtype or 'int64')
+
+        # GH 26342
+        result = DataFrame(range(5), columns=['A'], dtype=dtype)
+        tm.assert_frame_equal(result, expected)
+
+        # GH 16804
         result = DataFrame({'A': range(5)}, dtype=dtype)
         tm.assert_frame_equal(result, expected)