Skip to content

Commit 4012248

Browse files
committed
Raise ValueError when settings scalars with no index
1 parent 965c1c8 commit 4012248

File tree

6 files changed

+29
-29
lines changed

6 files changed

+29
-29
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ Other API Changes
481481
- :class:`Period` is now immutable, and will now raise an ``AttributeError`` when a user tries to assign a new value to the ``ordinal`` or ``freq`` attributes (:issue:`17116`).
482482
- :func:`to_datetime` when passed a tz-aware ``origin=`` kwarg will now raise a more informative ``ValueError`` rather than a ``TypeError`` (:issue:`16842`)
483483
- Renamed non-functional ``index`` to ``index_col`` in :func:`read_stata` to improve API consistency (:issue:`16342`)
484+
- Setting on a column with a scalar value and no index now raises a ``ValueError`` (:issue:`16823`)
484485

485486

486487
.. _whatsnew_0210.deprecations:

pandas/core/frame.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -2500,13 +2500,17 @@ def _ensure_valid_index(self, value):
25002500
passed value
25012501
"""
25022502
# GH5632, make sure that we are a Series convertible
2503-
if not len(self.index) and is_list_like(value):
2503+
if not len(self.index):
2504+
if not is_list_like(value):
2505+
# GH16823, Raise an error due to loss of information
2506+
raise ValueError('If using all scalar values, you must pass'
2507+
' an index')
25042508
try:
25052509
value = Series(value)
25062510
except:
2507-
raise ValueError('Cannot set a frame with no defined index '
2508-
'and a value that cannot be converted to a '
2509-
'Series')
2511+
raise ValueError('Cannot set a frame with no defined'
2512+
'index and a value that cannot be '
2513+
'converted to a Series')
25102514

25112515
self._data = self._data.reindex_axis(value.index.copy(), axis=1,
25122516
fill_value=np.nan)

pandas/tests/frame/test_indexing.py

+5
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,11 @@ def test_setitem_empty_frame_with_boolean(self):
721721
df[df > df2] = 47
722722
assert_frame_equal(df, df2)
723723

724+
def test_setitem_scalars_no_index(self):
725+
# GH16823
726+
df = DataFrame()
727+
pytest.raises(ValueError, df.__setitem__, 'foo', 1)
728+
724729
def test_getitem_empty_frame_with_boolean(self):
725730
# Test for issue #11859
726731

pandas/tests/indexing/test_loc.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -373,15 +373,14 @@ def test_loc_setitem_consistency(self):
373373

374374
def test_loc_setitem_consistency_empty(self):
375375
# empty (essentially noops)
376-
expected = DataFrame(columns=['x', 'y'])
377-
expected['x'] = expected['x'].astype(np.int64)
376+
# GH16823
378377
df = DataFrame(columns=['x', 'y'])
379-
df.loc[:, 'x'] = 1
380-
tm.assert_frame_equal(df, expected)
378+
with tm.assert_raises_regex(ValueError, 'If using all scalar values'):
379+
df.loc[:, 'x'] = 1
381380

382381
df = DataFrame(columns=['x', 'y'])
383-
df['x'] = 1
384-
tm.assert_frame_equal(df, expected)
382+
with tm.assert_raises_regex(ValueError, 'If using all scalar values'):
383+
df['x'] = 1
385384

386385
def test_loc_setitem_consistency_slice_column_len(self):
387386
# .loc[:,column] setting with slice == len of the column

pandas/tests/indexing/test_partial.py

+6-14
Original file line numberDiff line numberDiff line change
@@ -523,24 +523,16 @@ def f():
523523
def test_partial_set_empty_frame_row(self):
524524
# GH5720, GH5744
525525
# don't create rows when empty
526-
expected = DataFrame(columns=['A', 'B', 'New'],
527-
index=pd.Index([], dtype='int64'))
528-
expected['A'] = expected['A'].astype('int64')
529-
expected['B'] = expected['B'].astype('float64')
530-
expected['New'] = expected['New'].astype('float64')
531-
532526
df = DataFrame({"A": [1, 2, 3], "B": [1.2, 4.2, 5.2]})
533527
y = df[df.A > 5]
534-
y['New'] = np.nan
535-
tm.assert_frame_equal(y, expected)
536-
# tm.assert_frame_equal(y,expected)
528+
# GH16823
529+
# Setting a column with a scalar and no index should raise
530+
with tm.assert_raises_regex(ValueError, 'If using all scalar values'):
531+
y['New'] = np.nan
537532

538-
expected = DataFrame(columns=['a', 'b', 'c c', 'd'])
539-
expected['d'] = expected['d'].astype('int64')
540533
df = DataFrame(columns=['a', 'b', 'c c'])
541-
df['d'] = 3
542-
tm.assert_frame_equal(df, expected)
543-
tm.assert_series_equal(df['c c'], Series(name='c c', dtype=object))
534+
with tm.assert_raises_regex(ValueError, 'If using all scalar values'):
535+
df['d'] = 3
544536

545537
# reindex columns is ok
546538
df = DataFrame({"A": [1, 2, 3], "B": [1.2, 4.2, 5.2]})

pandas/tests/reshape/test_pivot.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1223,11 +1223,10 @@ def test_crosstab_no_overlap(self):
12231223

12241224
s1 = pd.Series([1, 2, 3], index=[1, 2, 3])
12251225
s2 = pd.Series([4, 5, 6], index=[4, 5, 6])
1226-
1227-
actual = crosstab(s1, s2)
1228-
expected = pd.DataFrame()
1229-
1230-
tm.assert_frame_equal(actual, expected)
1226+
# GH16823
1227+
# Setting a column with a scalar and no index should raise
1228+
with tm.assert_raises_regex(ValueError, 'If using all scalar values'):
1229+
crosstab(s1, s2)
12311230

12321231
def test_margin_dropna(self):
12331232
# GH 12577

0 commit comments

Comments
 (0)