Skip to content

Commit a051349

Browse files
committed
BUG: astype(Int64) raises AttributeError
1 parent 7343fd3 commit a051349

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

pandas/core/generic.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
is_re_compilable,
2929
is_period_arraylike,
3030
is_object_dtype,
31+
is_extension_array_dtype,
3132
pandas_dtype)
3233
from pandas.core.dtypes.cast import maybe_promote, maybe_upcast_putmask
3334
from pandas.core.dtypes.inference import is_hashable
@@ -5293,8 +5294,9 @@ def astype(self, dtype, copy=True, errors='raise', **kwargs):
52935294
else:
52945295
results.append(results.append(col.copy() if copy else col))
52955296

5296-
elif is_categorical_dtype(dtype) and self.ndim > 1:
5297+
elif is_extension_array_dtype(dtype) and self.ndim > 1:
52975298
# GH 18099: columnwise conversion to categorical
5299+
# and extension dtype
52985300
results = (self[col].astype(dtype, copy=copy) for col in self)
52995301

53005302
else:

pandas/core/internals/blocks.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -671,15 +671,6 @@ def _astype(self, dtype, copy=False, errors='raise', values=None,
671671
raise
672672
newb = self.copy() if copy else self
673673

674-
if newb.is_numeric and self.is_numeric:
675-
if newb.shape != self.shape:
676-
raise TypeError(
677-
"cannot set astype for copy = [{copy}] for dtype "
678-
"({dtype} [{itemsize}]) with smaller itemsize than "
679-
"current ({newb_dtype} [{newb_size}])".format(
680-
copy=copy, dtype=self.dtype.name,
681-
itemsize=self.itemsize, newb_dtype=newb.dtype.name,
682-
newb_size=newb.itemsize))
683674
return newb
684675

685676
def convert(self, copy=True, **kwargs):

pandas/tests/frame/test_dtypes.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from pandas.compat import u
1313
from pandas import _np_version_under1p14
1414

15+
from pandas.core.arrays import integer_array
1516
from pandas.core.dtypes.dtypes import DatetimeTZDtype, CategoricalDtype
1617
from pandas.tests.frame.common import TestData
1718
from pandas.util.testing import (assert_series_equal,
@@ -666,6 +667,48 @@ def test_astype_categoricaldtype_class_raises(self, cls):
666667
with tm.assert_raises_regex(TypeError, xpr):
667668
df['A'].astype(cls)
668669

670+
@pytest.mark.parametrize("dtype", ['Int64', 'Int32', 'Int16'])
671+
def test_astype_extension_dtypes(self, dtype):
672+
# GH 22578
673+
df = pd.DataFrame([[1., 2.], [3., 4.], [5., 6.]], columns=['a', 'b'])
674+
675+
expected1 = pd.DataFrame({'a': integer_array([1, 3, 5],
676+
dtype=dtype),
677+
'b': integer_array([2, 4, 6],
678+
dtype=dtype)})
679+
tm.assert_frame_equal(df.astype(dtype), expected1)
680+
tm.assert_frame_equal(df.astype('int64').astype(dtype), expected1)
681+
tm.assert_frame_equal(df.astype(dtype).astype('float64'), df)
682+
683+
df = pd.DataFrame([[1., 2.], [3., 4.], [5., 6.]], columns=['a', 'b'])
684+
df['b'] = df['b'].astype(dtype)
685+
expected2 = pd.DataFrame({'a': [1., 3., 5.],
686+
'b': integer_array([2, 4, 6],
687+
dtype=dtype)})
688+
tm.assert_frame_equal(df, expected2)
689+
690+
tm.assert_frame_equal(df.astype(dtype), expected1)
691+
tm.assert_frame_equal(df.astype('int64').astype(dtype), expected1)
692+
693+
@pytest.mark.parametrize("dtype", ['Int64', 'Int32', 'Int16'])
694+
def test_astype_extension_dtypes_1d(self, dtype):
695+
# GH 22578
696+
df = pd.DataFrame({'a': [1., 2., 3.]})
697+
698+
expected1 = pd.DataFrame({'a': integer_array([1, 2, 3],
699+
dtype=dtype)})
700+
tm.assert_frame_equal(df.astype(dtype), expected1)
701+
tm.assert_frame_equal(df.astype('int64').astype(dtype), expected1)
702+
703+
df = pd.DataFrame({'a': [1., 2., 3.]})
704+
df['a'] = df['a'].astype(dtype)
705+
expected2 = pd.DataFrame({'a': integer_array([1, 2, 3],
706+
dtype=dtype)})
707+
tm.assert_frame_equal(df, expected2)
708+
709+
tm.assert_frame_equal(df.astype(dtype), expected1)
710+
tm.assert_frame_equal(df.astype('int64').astype(dtype), expected1)
711+
669712
@pytest.mark.parametrize('dtype', [
670713
{100: 'float64', 200: 'uint64'}, 'category', 'float64'])
671714
def test_astype_column_metadata(self, dtype):

0 commit comments

Comments
 (0)