Skip to content

Commit 80e2d95

Browse files
committed
Raise ValueError when setting scalars in a df with no index
1 parent 96168ef commit 80e2d95

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

pandas/core/frame.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -2474,16 +2474,21 @@ def _ensure_valid_index(self, value):
24742474
passed value
24752475
"""
24762476
# GH5632, make sure that we are a Series convertible
2477-
if not len(self.index) and is_list_like(value):
2478-
try:
2479-
value = Series(value)
2480-
except:
2481-
raise ValueError('Cannot set a frame with no defined index '
2482-
'and a value that cannot be converted to a '
2483-
'Series')
2484-
2485-
self._data = self._data.reindex_axis(value.index.copy(), axis=1,
2486-
fill_value=np.nan)
2477+
if not len(self.index):
2478+
if is_list_like(value):
2479+
try:
2480+
value = Series(value)
2481+
except:
2482+
raise ValueError('Cannot set a frame with no defined'
2483+
'index and a value that cannot be '
2484+
'converted to a Series')
2485+
2486+
self._data = self._data.reindex_axis(value.index.copy(), axis=1,
2487+
fill_value=np.nan)
2488+
else:
2489+
# GH16823, Raise an error due to loss of information
2490+
raise ValueError('If using all scalar values, you must pass'
2491+
' an index')
24872492

24882493
def _set_item(self, key, value):
24892494
"""

0 commit comments

Comments
 (0)