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(float) in Index does the wrong thing #7464

Merged
merged 1 commit into from
Jun 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/source/v0.14.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ Bug Fixes




- Bug in ``Index.astype(float)`` where it would return an ``object`` dtype
``Index`` (:issue:`7464`).



Expand Down
7 changes: 5 additions & 2 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ def __new__(cls, data, dtype=None, copy=False, name=None, fastpath=False,

if issubclass(data.dtype.type, np.integer):
return Int64Index(data, copy=copy, dtype=dtype, name=name)
if issubclass(data.dtype.type, np.floating):
return Float64Index(data, copy=copy, dtype=dtype, name=name)

subarr = com._asarray_tuplesafe(data, dtype=object)

Expand Down Expand Up @@ -1986,7 +1988,8 @@ def inferred_type(self):
def astype(self, dtype):
if np.dtype(dtype) not in (np.object, np.float64):
raise TypeError('Setting %s dtype to anything other than '
'float64 or object is not supported' % self.__class__)
'float64 or object is not supported' %
self.__class__)
return Index(self.values, name=self.name, dtype=dtype)

def _convert_scalar_indexer(self, key, typ=None):
Expand Down Expand Up @@ -2020,7 +2023,7 @@ def get_value(self, series, key):
k = _values_from_object(key)
loc = self.get_loc(k)
new_values = series.values[loc]
if np.isscalar(new_values):
if np.isscalar(new_values) or new_values is None:
return new_values

new_index = self[loc]
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,7 @@ def test_nan_first_take_datetime(self):
exp = Index([idx[-1], idx[0], idx[1]])
tm.assert_index_equal(res, exp)


class TestFloat64Index(tm.TestCase):
_multiprocess_can_split_ = True

Expand Down Expand Up @@ -1041,6 +1042,13 @@ def test_nan_multiple_containment(self):
np.testing.assert_array_equal(i.isin([np.nan]),
np.array([False, False]))

def test_astype_from_object(self):
index = Index([1.0, np.nan, 0.2], dtype='object')
result = index.astype(float)
expected = Float64Index([1.0, np.nan, 0.2])
tm.assert_equal(result.dtype, expected.dtype)
tm.assert_index_equal(result, expected)


class TestInt64Index(tm.TestCase):
_multiprocess_can_split_ = True
Expand Down