BUG: astype assignment via iloc/loc not working
See original GitHub issueThis might be trying to coerce object
dtype to a real dtype (int/float) and is failing
Should prob raise for now (or work). Not working with iloc/loc.
In [66]: df = DataFrame([['1','2','3','.4',5,6.,'foo']],columns=list('ABCDEFG'))
In [67]: df.dtypes
Out[67]:
A object
B object
C object
D object
E int64
F float64
G object
dtype: object
In [68]: df.iloc[:,0:3] = df.iloc[:,0:3].astype(int)
In [69]: df.dtypes
Out[69]:
A object
B object
C object
D object
E int64
F float64
G object
dtype: object
In [70]: df.iloc[:,0:3] = df.iloc[:,0:3].convert_objects(convert_numeric=True)
In [71]: df.dtypes
Out[71]:
A object
B object
C object
D object
E int64
F float64
G object
dtype: object
Issue Analytics
- State:
- Created 10 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
I need to change the type of few columns in a pandas ...
I need to change the type of few columns in a pandas dataframe. Can't do so using iloc ; :27]=df1.iloc[:,0:27].astype('int') ; ]=df1.iloc[:,0].
Read more >pandas: Get/Set element values with at, iat, loc, iloc - nkmk note
Use at, iat, loc, or iloc to access data at any location in pandas. ... In addition to getting data, you can also...
Read more >pandas.DataFrame.loc — pandas 1.5.2 documentation
Returns a cross-section (row(s) or column(s)) from the Series/DataFrame. Series.loc. Access group of values using labels. Examples. Getting values. > ...
Read more >SettingwithCopyWarning: How to Fix This Warning in Pandas
Using loc doesn't end our problems because get operations with loc can still return either a view or a copy. Let's quickly examine...
Read more >Part 3 - Introduction to Pandas | ArcGIS API for Python
Let's find issues for pandas on GitHub using the add-on requests library. ... Special indexing operators such as loc and iloc can be...
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 Free
Top 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
Hello Guys, In reference to above issue, I recently encountered something similar. In a dataframe with around 40+ columns I am trying to change dtype for first 27 columns from float to int by using iloc: df1.iloc[:,0:27]=df1.iloc[:,0:27].astype(‘int’) However, it’s not working. I’m not getting any error, but dtype is not changing as well. Now the strangest part: If I first change dtype for only 1st column (like below): df1.iloc[:,0]=df1.iloc[:,0].astype(‘int’) and then run the earlier line of code: df1.iloc[:,0:27]=df1.iloc[:,0:27].astype(‘int’) It works as required. Any help to understand this will be grateful. Thanks!
Hi, I am experiencing a similar issue: I am starting from a 35 x 4800 DataFrame where all columns’ types are “object” after a SimpleImputer. When trying to go back to “int” on the first 30 columns that are integers:
df.iloc[:, 0:30] = df.iloc[:, 0:30].astype(int) => does not work
…
df.iloc[:, 0] = df.iloc[:, 0].astype(int) df.iloc[:, 1:30] = df.loc[:, “first_col”].astype(int) => works
…
df.loc[:, “first_col”] = df.loc[:, “first_col”].astype(int) df.iloc[:, 1:30] = df.loc[:, “first_col”].astype(int) => works
…
df.iloc[:, 0:30].astype(int).dtypes => shows all columns to “int32”