66from pandas .core .dtypes .dtypes import CategoricalDtype
77
88import pandas as pd
9- from pandas import Categorical , DataFrame , Series , date_range
9+ from pandas import Categorical , DataFrame , Series
1010import pandas ._testing as tm
1111
1212
@@ -120,18 +120,20 @@ def cmp(a, b):
120120 s .astype ("object" ).astype (CategoricalDtype ()), roundtrip_expected
121121 )
122122
123+ def test_invalid_conversions (self ):
123124 # invalid conversion (these are NOT a dtype)
125+ cat = Categorical ([f"{ i } - { i + 499 } " for i in range (0 , 10000 , 500 )])
126+ ser = Series (np .random .randint (0 , 10000 , 100 )).sort_values ()
127+ ser = pd .cut (ser , range (0 , 10500 , 500 ), right = False , labels = cat )
128+
124129 msg = (
125130 "dtype '<class 'pandas.core.arrays.categorical.Categorical'>' "
126131 "not understood"
127132 )
128-
129- for invalid in [
130- lambda x : x .astype (Categorical ),
131- lambda x : x .astype ("object" ).astype (Categorical ),
132- ]:
133- with pytest .raises (TypeError , match = msg ):
134- invalid (s )
133+ with pytest .raises (TypeError , match = msg ):
134+ ser .astype (Categorical )
135+ with pytest .raises (TypeError , match = msg ):
136+ ser .astype ("object" ).astype (Categorical )
135137
136138 @pytest .mark .parametrize ("dtype" , np .typecodes ["All" ])
137139 def test_astype_empty_constructor_equality (self , dtype ):
@@ -148,27 +150,6 @@ def test_astype_empty_constructor_equality(self, dtype):
148150 as_type_empty = Series ([]).astype (dtype )
149151 tm .assert_series_equal (init_empty , as_type_empty )
150152
151- def test_intercept_astype_object (self ):
152- series = Series (date_range ("1/1/2000" , periods = 10 ))
153-
154- # This test no longer makes sense, as
155- # Series is by default already M8[ns].
156- expected = series .astype ("object" )
157-
158- df = DataFrame ({"a" : series , "b" : np .random .randn (len (series ))})
159- exp_dtypes = Series (
160- [np .dtype ("datetime64[ns]" ), np .dtype ("float64" )], index = ["a" , "b" ]
161- )
162- tm .assert_series_equal (df .dtypes , exp_dtypes )
163-
164- result = df .values .squeeze ()
165- assert (result [:, 0 ] == expected .values ).all ()
166-
167- df = DataFrame ({"a" : series , "b" : ["foo" ] * len (series )})
168-
169- result = df .values .squeeze ()
170- assert (result [:, 0 ] == expected .values ).all ()
171-
172153 def test_series_to_categorical (self ):
173154 # see gh-16524: test conversion of Series to Categorical
174155 series = Series (["a" , "b" , "c" ])
@@ -178,19 +159,6 @@ def test_series_to_categorical(self):
178159
179160 tm .assert_series_equal (result , expected )
180161
181- @pytest .mark .parametrize (
182- "data" ,
183- [
184- pd .period_range ("2000" , periods = 4 ),
185- pd .IntervalIndex .from_breaks ([1 , 2 , 3 , 4 ]),
186- ],
187- )
188- def test_values_compatibility (self , data ):
189- # https://github.com/pandas-dev/pandas/issues/23995
190- result = Series (data ).values
191- expected = np .array (data .astype (object ))
192- tm .assert_numpy_array_equal (result , expected )
193-
194162 def test_reindex_astype_order_consistency (self ):
195163 # GH 17444
196164 s = Series ([1 , 2 , 3 ], index = [2 , 0 , 1 ])
0 commit comments