Skip to content

BUG: .astype() of a Float64Index to a Int64Index #12899

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

Closed
wants to merge 1 commit into from

Conversation

jreback
Copy link
Contributor

@jreback jreback commented Apr 14, 2016

closes #12881
closes #12866

xref #12309

@jreback jreback added Indexing Related to indexing on series/frames, not to indexes themselves Dtype Conversions Unexpected or buggy dtype conversions Compat pandas objects compatability with Numpy or Python functions labels Apr 14, 2016
@jreback jreback added this to the 0.18.1 milestone Apr 14, 2016
@jreback
Copy link
Contributor Author

jreback commented Apr 14, 2016

So we allow astyping only to F8 (and now I8).

In [2]: i = pd.Float64Index([0, 1, 2])

In [3]: pd.Float64Index([0, 1, 2]).astype('i8')
Out[3]: Int64Index([0, 1, 2], dtype='int64')

In [4]: pd.Float64Index([0, 1.1, 2]).astype('i8')
Out[4]: Float64Index([0.0, 1.1, 2.0], dtype='float64')

In [5]: pd.Float64Index([0, 1.1, 2]).astype('i4')
TypeError: Setting <class 'pandas.indexes.numeric.Float64Index'> dtype to anything other than float64 or object is not supported

Should we allow other integer/floats? (even though we upcast them anyhow), this is [5]

e.g. we allow:

In [6]: Index([0, 1.1, 2],dtype='float32')
Out[6]: Float64Index([0.0, 1.10000002384, 2.0], dtype='float64')

Further on [4], should this raise instead?

@jreback
Copy link
Contributor Author

jreback commented Apr 14, 2016

@jorisvandenbossche
Copy link
Member

On In[4], shouldn't we follow numpy behaviour here?

In [3]: np.array([0, 1.1, 2]).astype('i8')
Out[3]: array([0, 1, 2], dtype=int64)

@jreback jreback force-pushed the astype branch 2 times, most recently from 7418cc8 to 18fc5ea Compare April 15, 2016 13:21
@jreback
Copy link
Contributor Author

jreback commented Apr 17, 2016

ok all fixed up:

In [8]:  pd.Float64Index([0, 1, 2]).astype('i8')
Out[8]: Int64Index([0, 1, 2], dtype='int64')

In [9]: pd.Float64Index([0, 1.1, 2]).astype('i8')
Out[9]: Int64Index([0, 1, 2], dtype='int64')

In [10]: pd.Float64Index([0, 1.1, 2]).astype('i4')
Out[10]: Int64Index([0, 1, 2], dtype='int64')

In [11]: pd.Float64Index([0, 1.1, 2]).astype('f4')
Out[11]: Float64Index([0.0, 1.10000002384, 2.0], dtype='float64')

@jreback jreback closed this in bac68d6 Apr 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compat pandas objects compatability with Numpy or Python functions Dtype Conversions Unexpected or buggy dtype conversions Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: .astype of Float64Index read_json changes dtype (int => float)
2 participants