@@ -1185,25 +1185,34 @@ Use ``copy=True`` to prevent such a behaviour or simply don't reuse ``Categorica
1185
1185
:class: `Index ` with ``dtype='object' ``, the dtype of the categories will be
1186
1186
preserved as ``object ``. When constructing from a NumPy array
1187
1187
with ``dtype='object' `` or a raw Python sequence, pandas will infer the most
1188
- specific dtype for the categories (for example, ``str `` if all elements are strings).
1188
+ specific dtype for the categories (for example, ``string `` if all elements are strings).
1189
1189
1190
1190
.. ipython :: python
1191
1191
1192
- pd.options. future.infer_string = True
1193
- ser = pd. Series([" foo" , " bar" , " baz" ], dtype = " object" )
1194
- idx = pd. Index([" foo" , " bar" , " baz" ], dtype = " object" )
1195
- arr = np.array([" foo" , " bar" , " baz" ], dtype = " object" )
1196
- pylist = [" foo" , " bar" , " baz" ]
1192
+ with pd.option_context( " future.infer_string" , True ):
1193
+ ser = Series([" foo" , " bar" , " baz" ], dtype = " object" )
1194
+ idx = Index([" foo" , " bar" , " baz" ], dtype = " object" )
1195
+ arr = np.array([" foo" , " bar" , " baz" ], dtype = " object" )
1196
+ pylist = [" foo" , " bar" , " baz" ]
1197
1197
1198
- cat_from_ser = pd. Categorical(ser)
1199
- cat_from_idx = pd. Categorical(idx)
1200
- cat_from_arr = pd. Categorical(arr)
1201
- cat_from_list = pd. Categorical(pylist)
1198
+ cat_from_ser = Categorical(ser)
1199
+ cat_from_idx = Categorical(idx)
1200
+ cat_from_arr = Categorical(arr)
1201
+ cat_from_list = Categorical(pylist)
1202
1202
1203
- # Series/Index with object dtype: preserve object dtype
1204
- assert cat_from_ser.categories.dtype == " object"
1205
- assert cat_from_idx.categories.dtype == " object"
1203
+ # Series/Index with object dtype: infer string
1204
+ # dtype if all elements are strings
1205
+ assert cat_from_ser.categories.inferred_type == " string"
1206
+ assert cat_from_idx.categories.inferred_type == " string"
1206
1207
1207
- # Numpy array or list: infer string dtype
1208
- assert cat_from_arr.categories.dtype == " str"
1209
- assert cat_from_list.categories.dtype == " str"
1208
+ # Numpy array or list: infer string dtype
1209
+ assert cat_from_arr.categories.inferred_type == " string"
1210
+ assert cat_from_list.categories.inferred_type == " string"
1211
+
1212
+ # Mixed types: preserve object dtype
1213
+ ser_mixed = Series([" foo" , 1 , None ], dtype = " object" )
1214
+ idx_mixed = Index([" foo" , 1 , None ], dtype = " object" )
1215
+ cat_mixed_ser = Categorical(ser_mixed)
1216
+ cat_mixed_idx = Categorical(idx_mixed)
1217
+ assert cat_mixed_ser.categories.dtype == " object"
1218
+ assert cat_mixed_idx.categories.dtype == " object"
0 commit comments