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

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
Jump to file or symbol
Failed to load files and symbols.
+15 −3
Split
View
@@ -229,7 +229,8 @@ Bug Fixes
-
+- Bug in ``Index.astype(float)`` where it would return an ``object`` dtype
+ ``Index`` (:issue:`7464`).
View
@@ -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)
@@ -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):
@@ -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]
@@ -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
@@ -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