BUG: iloc setting columns not taking effect
See original GitHub issuearr = np.random.randn(10 ** 6).reshape(500, 2000).astype(np.float64)
df = pd.DataFrame(arr)
df.iloc[:, 1000:] = df.iloc[:, 1000:].astype(np.float32)
>>> df.dtypes.value_counts()
float64 2000
dtype: int64
I would expect 1000 float64 columns and 1000 float32 columns. I get the expected behavior if I set a single column before trying to set all 1000:
df = pd.DataFrame(arr)
df[1000] = df[1000].astype(np.float32)
df.iloc[:, 1000:] = df.iloc[:, 1000:].astype(np.float32)
>>> df.dtypes.value_counts()
float32 1000
float64 1000
dtype: int64
Issue Analytics
- State:
- Created 3 years ago
- Comments:34 (34 by maintainers)
Top Results From Across the Web
Why does one use of iloc() give a SettingWithCopyWarning ...
Sometimes you will notice it occurs inconsistently. Instead, just avoid chained indexing or generally working with what could be a copy. You ...
Read more >Intro to data structures — pandas 1.5.2 documentation
When working with raw NumPy arrays, looping through value-by-value is usually not necessary. The same is true when working with Series in pandas....
Read more >A Python Beginner's Look at .loc. As a ... - Towards Data Science
As a Python beginner, using .loc to retrieve and update values in a pandas ... Passing just a column label or a blank...
Read more >How to Fix: KeyError in Pandas - GeeksforGeeks
Since there is no column with the name country we get a KeyError. How to Fix the KeyError? We can simply fix the...
Read more >Tips for Selecting Columns in a DataFrame
This article will discuss several tips and shortcuts for using iloc to work with a data set that has a large number of...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ll be pretty surprised if that passes the full test suite, but give it a try
Important background: to a very rough approximation, we can think of a DataFrame as a dict of Series with the keys being
df.columns
. So under the hood, the data in any given column is a single array. But the data in a single row contains elements from many columns, which may be many arrays.So when we do
df.iloc[:, foo] = bar
we can say “cleanly swap out some new arrays for the old ones” whereas we cannot do that withdf.iloc[foo, :] = bar