Skip to content

Commit 58b0f20

Browse files
committed
Fixed special case for astype
1 parent 0a2d514 commit 58b0f20

File tree

2 files changed

+4
-9
lines changed

2 files changed

+4
-9
lines changed

pandas/core/internals.py

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ def is_categorical_astype(self, dtype):
141141
if dtype == CategoricalDtype():
142142
return True
143143

144+
elif dtype is Categorical:
144145
# this is a pd.Categorical, but is not
145146
# a valid type for astypeing
146147
raise TypeError("invalid type {0} for astype".format(dtype))

pandas/tests/test_categorical.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -4149,12 +4149,6 @@ def f():
41494149
df = pd.DataFrame({'a': pd.Categorical(idx)})
41504150
tm.assert_frame_equal(df.fillna(value=pd.NaT), df)
41514151

4152-
def test_astype_new(self):
4153-
s = self.cat['value_group']
4154-
r1 = s.astype('object')
4155-
r2 = r1.astype('category')
4156-
tm.assert_series_equal(r2, s)
4157-
41584152
def test_astype_to_other(self):
41594153

41604154
s = self.cat['value_group']
@@ -4197,9 +4191,9 @@ def cmp(a, b):
41974191
tm.assert_series_equal(result, s, check_categorical=False)
41984192

41994193
# invalid conversion (these are NOT a dtype)
4200-
# for invalid in [lambda x: x.astype(pd.Categorical),
4201-
# lambda x: x.astype('object').astype(pd.Categorical)]:
4202-
# pytest.raises(TypeError, lambda: invalid(s))
4194+
for invalid in [lambda x: x.astype(pd.Categorical),
4195+
lambda x: x.astype('object').astype(pd.Categorical)]:
4196+
pytest.raises(TypeError, lambda: invalid(s))
42034197

42044198
def test_astype_categorical(self):
42054199

0 commit comments

Comments
 (0)