Skip to content

Commit bbaa576

Browse files
authored
Revert "ERR: Raise ValueError when setting scalars in a dataframe with no index ( #16823) (#16968)" (#17902)
* Revert "ERR: Raise ValueError when setting scalars in a dataframe with no index ( #16823) (#16968)" This reverts commit f9ba6fe. * TST: expicit test on setting scalars on empty frame closes #17894
1 parent a0a0f5a commit bbaa576

File tree

7 files changed

+29
-28
lines changed

7 files changed

+29
-28
lines changed

doc/source/whatsnew/v0.21.0.txt

-2
Original file line numberDiff line numberDiff line change
@@ -791,8 +791,6 @@ Other API Changes
791791
- Restricted DateOffset keyword arguments. Previously, ``DateOffset`` subclasses allowed arbitrary keyword arguments which could lead to unexpected behavior. Now, only valid arguments will be accepted. (:issue:`17176`).
792792
- Pandas no longer registers matplotlib converters on import. The converters
793793
will be registered and used when the first plot is draw (:issue:`17710`)
794-
- Setting on a column with a scalar value and 0-len index now raises a ``ValueError`` (:issue:`16823`)
795-
796794

797795
.. _whatsnew_0210.deprecations:
798796

pandas/core/frame.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -2557,17 +2557,13 @@ def _ensure_valid_index(self, value):
25572557
passed value
25582558
"""
25592559
# GH5632, make sure that we are a Series convertible
2560-
if not len(self.index):
2561-
if not is_list_like(value):
2562-
# GH16823, Raise an error due to loss of information
2563-
raise ValueError('If using all scalar values, you must pass'
2564-
' an index')
2560+
if not len(self.index) and is_list_like(value):
25652561
try:
25662562
value = Series(value)
25672563
except:
2568-
raise ValueError('Cannot set a frame with no defined'
2569-
'index and a value that cannot be '
2570-
'converted to a Series')
2564+
raise ValueError('Cannot set a frame with no defined index '
2565+
'and a value that cannot be converted to a '
2566+
'Series')
25712567

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

pandas/core/reshape/pivot.py

-3
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,6 @@ def crosstab(index, columns, values=None, rownames=None, colnames=None,
454454

455455
from pandas import DataFrame
456456
df = DataFrame(data, index=common_idx)
457-
if not len(df):
458-
return DataFrame(index=common_idx)
459-
460457
if values is None:
461458
df['__dummy__'] = 0
462459
kwargs = {'aggfunc': len, 'fill_value': 0}

pandas/tests/frame/test_indexing.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -722,9 +722,11 @@ def test_setitem_empty_frame_with_boolean(self):
722722
assert_frame_equal(df, df2)
723723

724724
def test_setitem_scalars_no_index(self):
725-
# GH16823
725+
# GH16823 / 17894
726726
df = DataFrame()
727-
pytest.raises(ValueError, df.__setitem__, 'foo', 1)
727+
df['foo'] = 1
728+
expected = DataFrame(columns=['foo']).astype(np.int64)
729+
assert_frame_equal(df, expected)
728730

729731
def test_getitem_empty_frame_with_boolean(self):
730732
# Test for issue #11859

pandas/tests/indexing/test_loc.py

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

424424
def test_loc_setitem_consistency_empty(self):
425425
# empty (essentially noops)
426-
# GH16823
426+
expected = DataFrame(columns=['x', 'y'])
427+
expected['x'] = expected['x'].astype(np.int64)
427428
df = DataFrame(columns=['x', 'y'])
428-
with tm.assert_raises_regex(ValueError, 'If using all scalar values'):
429-
df.loc[:, 'x'] = 1
429+
df.loc[:, 'x'] = 1
430+
tm.assert_frame_equal(df, expected)
430431

431432
df = DataFrame(columns=['x', 'y'])
432-
with tm.assert_raises_regex(ValueError, 'If using all scalar values'):
433-
df['x'] = 1
433+
df['x'] = 1
434+
tm.assert_frame_equal(df, expected)
434435

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

pandas/tests/indexing/test_partial.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -575,16 +575,24 @@ def f():
575575
def test_partial_set_empty_frame_row(self):
576576
# GH5720, GH5744
577577
# don't create rows when empty
578+
expected = DataFrame(columns=['A', 'B', 'New'],
579+
index=pd.Index([], dtype='int64'))
580+
expected['A'] = expected['A'].astype('int64')
581+
expected['B'] = expected['B'].astype('float64')
582+
expected['New'] = expected['New'].astype('float64')
583+
578584
df = DataFrame({"A": [1, 2, 3], "B": [1.2, 4.2, 5.2]})
579585
y = df[df.A > 5]
580-
# GH16823
581-
# Setting a column with a scalar and no index should raise
582-
with tm.assert_raises_regex(ValueError, 'If using all scalar values'):
583-
y['New'] = np.nan
586+
y['New'] = np.nan
587+
tm.assert_frame_equal(y, expected)
588+
# tm.assert_frame_equal(y,expected)
584589

590+
expected = DataFrame(columns=['a', 'b', 'c c', 'd'])
591+
expected['d'] = expected['d'].astype('int64')
585592
df = DataFrame(columns=['a', 'b', 'c c'])
586-
with tm.assert_raises_regex(ValueError, 'If using all scalar values'):
587-
df['d'] = 3
593+
df['d'] = 3
594+
tm.assert_frame_equal(df, expected)
595+
tm.assert_series_equal(df['c c'], Series(name='c c', dtype=object))
588596

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

pandas/tests/reshape/test_pivot.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1228,8 +1228,7 @@ def test_crosstab_no_overlap(self):
12281228
s2 = pd.Series([4, 5, 6], index=[4, 5, 6])
12291229

12301230
actual = crosstab(s1, s2)
1231-
expected = pd.DataFrame(
1232-
index=pd.Index([], dtype='int64')).astype('int64')
1231+
expected = pd.DataFrame()
12331232

12341233
tm.assert_frame_equal(actual, expected)
12351234

0 commit comments

Comments
 (0)