Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: astype assignment via iloc/loc not working #4312

Closed
jreback opened this issue Jul 22, 2013 · 5 comments · Fixed by #4624
Closed

BUG: astype assignment via iloc/loc not working #4312

jreback opened this issue Jul 22, 2013 · 5 comments · Fixed by #4624
Labels
Dtype Conversions Unexpected or buggy dtype conversions Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@jreback
Copy link
Contributor

jreback commented Jul 22, 2013

http://stackoverflow.com/questions/17778139/pandas-unable-to-change-column-data-type/17778560#17778560

This 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

@TomAugspurger
Copy link
Contributor

@jreback is this fixed or did you want to raise?

In [10]: df = pd.DataFrame({'A': [1., 2., 3., 4.]})

In [11]: df.dtypes
Out[11]: 
A    float64
dtype: object

In [12]: df.loc[:, 'A'] = df['A'].astype(np.int64)

In [13]: df.dtypes
Out[13]: 
A    float64
dtype: object

In [14]: pd.__version__
Out[14]: '0.13.0rc1-27-g4d5ca5c'

I think this is still the same.

@TomAugspurger
Copy link
Contributor

Ah, I guess it's just loc that isn't working. iloc handles the dtype changes fine.

@jreback
Copy link
Contributor Author

jreback commented Dec 15, 2013

hmm thought all those cases were tested

can u create a new issue pls

@rishik-bot
Copy link

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!

@pierrelouisbescond
Copy link

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants