Revert "ERR: Raise ValueError when setting scalars in a dataframe with no index ( #16823) (#16968)"
State: closed

closes #17894

xref #16823

Labels:
name: Indexing
description: Related to indexing on series/frames, not to indexes themselves
name: API Design

Comments:
cc @toobaz @jorisvandenbossche 

```
In [1]: df = pd.DataFrame()

In [2]: df['foo'] = 1

In [3]: df
Out[3]: 
Empty DataFrame
Columns: [foo]
Index: []

In [4]: df.dtypes
Out[4]: 
foo    int64
dtype: object

In [5]: df = pd.DataFrame()

# 17895
In [6]: df.loc[1] = 1
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-24a18e3c59c6> in <module>()
----> 1 df.loc[1] = 1

~/pandas/pandas/core/indexing.py in __setitem__(self, key, value)
    192             key = com._apply_if_callable(key, self.obj)
    193         indexer = self._get_setitem_indexer(key)
--> 194         self._setitem_with_indexer(indexer, value)
    195 
    196     def _has_valid_type(self, k, axis):

~/pandas/pandas/core/indexing.py in _setitem_with_indexer(self, indexer, value)
    421                     # no columns and scalar
    422                     if not len(self.obj.columns):
--> 423                         raise ValueError("cannot set a frame with no defined "
    424                                          "columns")
    425 

ValueError: cannot set a frame with no defined columns

```

so revert puts us back to original. we still are changing the column dtype, though the value is not assigned.
Doing the reverse [6] *does* raise (xref #17895) so inconsistent here.

> so revert puts us back to original. we still are changing the column dtype, though the value is not assigned.

I think this is nice

> Doing the reverse [6] does raise (xref #17895) so inconsistent here.

I agree, I will look at it
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/17902?src=pr&el=h1) Report
> Merging [#17902](https://codecov.io/gh/pandas-dev/pandas/pull/17902?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/5bf7f9a4ff6968fd444fa7098f8dc95586591994?src=pr&el=desc) will **decrease** coverage by `0.01%`.
> The diff coverage is `50%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/17902/graphs/tree.svg?token=eZ4WkYLtcO&width=650&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/17902?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #17902      +/-   ##
==========================================
- Coverage   91.23%   91.22%   -0.02%     
==========================================
  Files         163      163              
  Lines       50105    50101       -4     
==========================================
- Hits        45715    45703      -12     
- Misses       4390     4398       +8
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `89.03% <50%> (ø)` | :arrow_up: |
| #single | `40.32% <50%> (-0.06%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/17902?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/reshape/pivot.py](https://codecov.io/gh/pandas-dev/pandas/pull/17902?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvcmVzaGFwZS9waXZvdC5weQ==) | `96.35% <ø> (-0.03%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/17902?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.75% <50%> (-0.1%)` | :arrow_down: |
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/17902?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `25% <0%> (-58.34%)` | :arrow_down: |
| [pandas/core/groupby.py](https://codecov.io/gh/pandas-dev/pandas/pull/17902?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZ3JvdXBieS5weQ==) | `92.03% <0%> (+0.04%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/17902?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/17902?src=pr&el=footer). Last update [5bf7f9a...365e32c](https://codecov.io/gh/pandas-dev/pandas/pull/17902?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

anyone want to warn on this operation? 
@toobaz btw watched your talk at EuroPython: https://www.youtube.com/watch?v=4JwpDGrMsJE

very nice!
> very nice!

Thanks! I was asked to replicate at a couple of other conferences, so any suggestions to improve are welcome.
near the end of your talk
```
In [16]: df = pd.DataFrame(['a b c']*10000, columns=['col'])

In [18]: %timeit pd.DataFrame(df['col'].apply(lambda x: pd.Series(x.split())))
1.37 s +- 33.4 ms per loop (mean +- std. dev. of 7 runs, 1 loop each)

In [20]: %timeit pd.DataFrame(df['col'].apply(lambda x: x.split()))
3.59 ms +- 240 us per loop (mean +- std. dev. of 7 runs, 100 loops each)

In [24]: %timeit df['col'].str.split(expand=True)
14.2 ms +- 140 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
```

I would also show [24], as it is idiomatic, slightly slower but handles NaN values


Aha, cool, I had missed this, thanks.
ok reverting for now. any thoughts on warning though?
I didn't reply above because I'm slightly against introducing warnings in this case - I consider warnings as something helping you understand the implementation caveats, not the API. But this might be a personal view.

On the other hand, I think that once we also fix #17895 we should clearly document the behavior in the docs.