question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: iloc setting columns not taking effect

See original GitHub issue
arr = 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:open
  • Created 3 years ago
  • Comments:34 (34 by maintainers)

github_iconTop GitHub Comments

1reaction
jbrockmendelcommented, Apr 7, 2020

seems to not break anything (passed pandas/tests/frame/methods/**/*),

I’ll be pretty surprised if that passes the full test suite, but give it a try

0reactions
jbrockmendelcommented, Apr 8, 2020

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 with df.iloc[foo, :] = bar

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found