@@ -147,9 +147,6 @@ class Categorical(PandasObject):
147
147
ordered : boolean, (default False)
148
148
Whether or not this categorical is treated as a ordered categorical. If not given,
149
149
the resulting categorical will not be ordered.
150
- name : str, optional
151
- Name for the Categorical variable. If name is None, will attempt
152
- to infer from values.
153
150
154
151
Attributes
155
152
----------
@@ -159,8 +156,6 @@ class Categorical(PandasObject):
159
156
The codes (integer positions, which point to the categories) of this categorical, read only.
160
157
ordered : boolean
161
158
Whether or not this Categorical is ordered.
162
- name : string
163
- The name of this Categorical.
164
159
165
160
Raises
166
161
------
@@ -205,31 +200,31 @@ class Categorical(PandasObject):
205
200
# For comparisons, so that numpy uses our implementation if the compare ops, which raise
206
201
__array_priority__ = 1000
207
202
_typ = 'categorical'
208
- name = None
209
203
210
204
def __init__ (self , values , categories = None , ordered = False , name = None , fastpath = False ,
211
205
levels = None ):
212
206
213
207
if fastpath :
214
208
# fast path
215
209
self ._codes = _coerce_indexer_dtype (values , categories )
216
- self .name = name
217
210
self .categories = categories
218
211
self ._ordered = ordered
219
212
return
220
213
221
- if name is None :
222
- name = getattr (values , 'name' , None )
214
+ if not name is None :
215
+ msg = "the 'name' keyword is removed, use 'name' with consumers of the " \
216
+ "categorical instead (e.g. 'Series(cat, name=\" something\" )'"
217
+ warn (msg , UserWarning , stacklevel = 2 )
223
218
224
219
# TODO: Remove after deprecation period in 2017/ after 0.18
225
220
if not levels is None :
226
221
warn ("Creating a 'Categorical' with 'levels' is deprecated, use 'categories' instead" ,
227
- FutureWarning )
222
+ FutureWarning , stacklevel = 2 )
228
223
if categories is None :
229
224
categories = levels
230
225
else :
231
226
raise ValueError ("Cannot pass in both 'categories' and (deprecated) 'levels', "
232
- "use only 'categories'" )
227
+ "use only 'categories'" , stacklevel = 2 )
233
228
234
229
# sanitize input
235
230
if is_categorical_dtype (values ):
@@ -293,21 +288,20 @@ def __init__(self, values, categories=None, ordered=False, name=None, fastpath=F
293
288
# TODO: check for old style usage. These warnings should be removes after 0.18/ in 2016
294
289
if is_integer_dtype (values ) and not is_integer_dtype (categories ):
295
290
warn ("Values and categories have different dtypes. Did you mean to use\n "
296
- "'Categorical.from_codes(codes, categories)'?" , RuntimeWarning )
291
+ "'Categorical.from_codes(codes, categories)'?" , RuntimeWarning , stacklevel = 2 )
297
292
298
293
if len (values ) and is_integer_dtype (values ) and (codes == - 1 ).all ():
299
294
warn ("None of the categories were found in values. Did you mean to use\n "
300
- "'Categorical.from_codes(codes, categories)'?" , RuntimeWarning )
295
+ "'Categorical.from_codes(codes, categories)'?" , RuntimeWarning , stacklevel = 2 )
301
296
302
297
self .set_ordered (ordered or False , inplace = True )
303
298
self .categories = categories
304
- self .name = name
305
299
self ._codes = _coerce_indexer_dtype (codes , categories )
306
300
307
301
def copy (self ):
308
302
""" Copy constructor. """
309
303
return Categorical (values = self ._codes .copy (),categories = self .categories ,
310
- name = self . name , ordered = self .ordered , fastpath = True )
304
+ ordered = self .ordered , fastpath = True )
311
305
312
306
def astype (self , dtype ):
313
307
""" coerce this type to another dtype """
@@ -373,9 +367,12 @@ def from_codes(cls, codes, categories, ordered=False, name=None):
373
367
ordered : boolean, (default False)
374
368
Whether or not this categorical is treated as a ordered categorical. If not given,
375
369
the resulting categorical will be unordered.
376
- name : str, optional
377
- Name for the Categorical variable.
378
370
"""
371
+ if not name is None :
372
+ msg = "the 'name' keyword is removed, use 'name' with consumers of the " \
373
+ "categorical instead (e.g. 'Series(cat, name=\" something\" )'"
374
+ warn (msg , UserWarning , stacklevel = 2 )
375
+
379
376
try :
380
377
codes = np .asarray (codes , np .int64 )
381
378
except :
@@ -386,7 +383,7 @@ def from_codes(cls, codes, categories, ordered=False, name=None):
386
383
if len (codes ) and (codes .max () >= len (categories ) or codes .min () < - 1 ):
387
384
raise ValueError ("codes need to be between -1 and len(categories)-1" )
388
385
389
- return Categorical (codes , categories = categories , ordered = ordered , name = name , fastpath = True )
386
+ return Categorical (codes , categories = categories , ordered = ordered , fastpath = True )
390
387
391
388
_codes = None
392
389
@@ -416,8 +413,7 @@ def _get_labels(self):
416
413
417
414
Deprecated, use .codes!
418
415
"""
419
- import warnings
420
- warnings .warn ("'labels' is deprecated. Use 'codes' instead" , FutureWarning )
416
+ warn ("'labels' is deprecated. Use 'codes' instead" , FutureWarning , stacklevel = 3 )
421
417
return self .codes
422
418
423
419
labels = property (fget = _get_labels , fset = _set_codes )
@@ -464,12 +460,12 @@ def _get_categories(self):
464
460
465
461
def _set_levels (self , levels ):
466
462
""" set new levels (deprecated, use "categories") """
467
- warn ("Assigning to 'levels' is deprecated, use 'categories'" , FutureWarning )
463
+ warn ("Assigning to 'levels' is deprecated, use 'categories'" , FutureWarning , stacklevel = 3 )
468
464
self .categories = levels
469
465
470
466
def _get_levels (self ):
471
467
""" Gets the levels (deprecated, use "categories") """
472
- warn ("Accessing 'levels' is deprecated, use 'categories'" , FutureWarning )
468
+ warn ("Accessing 'levels' is deprecated, use 'categories'" , FutureWarning , stacklevel = 3 )
473
469
return self .categories
474
470
475
471
# TODO: Remove after deprecation period in 2017/ after 0.18
@@ -479,7 +475,8 @@ def _get_levels(self):
479
475
480
476
def _set_ordered (self , value ):
481
477
""" Sets the ordered attribute to the boolean value """
482
- warn ("Setting 'ordered' directly is deprecated, use 'set_ordered'" , FutureWarning )
478
+ warn ("Setting 'ordered' directly is deprecated, use 'set_ordered'" , FutureWarning ,
479
+ stacklevel = 3 )
483
480
self .set_ordered (value , inplace = True )
484
481
485
482
def set_ordered (self , value , inplace = False ):
@@ -1140,7 +1137,7 @@ def order(self, inplace=False, ascending=True, na_position='last'):
1140
1137
return
1141
1138
else :
1142
1139
return Categorical (values = codes ,categories = self .categories , ordered = self .ordered ,
1143
- name = self . name , fastpath = True )
1140
+ fastpath = True )
1144
1141
1145
1142
1146
1143
def sort (self , inplace = True , ascending = True , na_position = 'last' ):
@@ -1266,7 +1263,7 @@ def fillna(self, value=None, method=None, limit=None):
1266
1263
values [mask ] = self .categories .get_loc (value )
1267
1264
1268
1265
return Categorical (values , categories = self .categories , ordered = self .ordered ,
1269
- name = self . name , fastpath = True )
1266
+ fastpath = True )
1270
1267
1271
1268
def take_nd (self , indexer , allow_fill = True , fill_value = None ):
1272
1269
""" Take the codes by the indexer, fill with the fill_value.
@@ -1280,7 +1277,7 @@ def take_nd(self, indexer, allow_fill=True, fill_value=None):
1280
1277
1281
1278
codes = take_1d (self ._codes , indexer , allow_fill = True , fill_value = - 1 )
1282
1279
result = Categorical (codes , categories = self .categories , ordered = self .ordered ,
1283
- name = self . name , fastpath = True )
1280
+ fastpath = True )
1284
1281
return result
1285
1282
1286
1283
take = take_nd
@@ -1300,7 +1297,7 @@ def _slice(self, slicer):
1300
1297
1301
1298
_codes = self ._codes [slicer ]
1302
1299
return Categorical (values = _codes ,categories = self .categories , ordered = self .ordered ,
1303
- name = self . name , fastpath = True )
1300
+ fastpath = True )
1304
1301
1305
1302
def __len__ (self ):
1306
1303
"""The length of this Categorical."""
@@ -1313,9 +1310,8 @@ def __iter__(self):
1313
1310
def _tidy_repr (self , max_vals = 10 , footer = True ):
1314
1311
""" a short repr displaying only max_vals and an optional (but default footer) """
1315
1312
num = max_vals // 2
1316
- head = self [:num ]._get_repr (length = False , name = False , footer = False )
1313
+ head = self [:num ]._get_repr (length = False , footer = False )
1317
1314
tail = self [- (max_vals - num ):]._get_repr (length = False ,
1318
- name = False ,
1319
1315
footer = False )
1320
1316
1321
1317
result = '%s, ..., %s' % (head [:- 1 ], tail [1 :])
@@ -1369,14 +1365,11 @@ def _repr_categories_info(self):
1369
1365
1370
1366
def _repr_footer (self ):
1371
1367
1372
- namestr = "Name: %s, " % self .name if self .name is not None else ""
1373
- return u ('%sLength: %d\n %s' ) % (namestr ,
1374
- len (self ), self ._repr_categories_info ())
1368
+ return u ('Length: %d\n %s' ) % (len (self ), self ._repr_categories_info ())
1375
1369
1376
- def _get_repr (self , name = False , length = True , na_rep = 'NaN' , footer = True ):
1370
+ def _get_repr (self , length = True , na_rep = 'NaN' , footer = True ):
1377
1371
from pandas .core import format as fmt
1378
1372
formatter = fmt .CategoricalFormatter (self ,
1379
- name = name ,
1380
1373
length = length ,
1381
1374
na_rep = na_rep ,
1382
1375
footer = footer )
@@ -1389,11 +1382,9 @@ def __unicode__(self):
1389
1382
if len (self ._codes ) > _maxlen :
1390
1383
result = self ._tidy_repr (_maxlen )
1391
1384
elif len (self ._codes ) > 0 :
1392
- result = self ._get_repr (length = len (self ) > _maxlen ,
1393
- name = True )
1385
+ result = self ._get_repr (length = len (self ) > _maxlen )
1394
1386
else :
1395
- result = '[], %s' % self ._get_repr (name = True ,
1396
- length = False ,
1387
+ result = '[], %s' % self ._get_repr (length = False ,
1397
1388
footer = True ,
1398
1389
).replace ("\n " ,", " )
1399
1390
@@ -1562,8 +1553,7 @@ def mode(self):
1562
1553
import pandas .hashtable as htable
1563
1554
good = self ._codes != - 1
1564
1555
result = Categorical (sorted (htable .mode_int64 (_ensure_int64 (self ._codes [good ]))),
1565
- categories = self .categories ,ordered = self .ordered , name = self .name ,
1566
- fastpath = True )
1556
+ categories = self .categories ,ordered = self .ordered , fastpath = True )
1567
1557
return result
1568
1558
1569
1559
def unique (self ):
@@ -1586,8 +1576,6 @@ def equals(self, other):
1586
1576
"""
1587
1577
Returns True if categorical arrays are equal.
1588
1578
1589
- The name of the `Categorical` is not compared!
1590
-
1591
1579
Parameters
1592
1580
----------
1593
1581
other : `Categorical`
@@ -1596,7 +1584,6 @@ def equals(self, other):
1596
1584
-------
1597
1585
are_equal : boolean
1598
1586
"""
1599
- # TODO: should this also test if name is equal?
1600
1587
return self .is_dtype_equal (other ) and np .array_equal (self ._codes , other ._codes )
1601
1588
1602
1589
def is_dtype_equal (self , other ):
@@ -1647,7 +1634,7 @@ def repeat(self, repeats):
1647
1634
"""
1648
1635
codes = self ._codes .repeat (repeats )
1649
1636
return Categorical (values = codes , categories = self .categories ,
1650
- ordered = self .ordered , name = self . name , fastpath = True )
1637
+ ordered = self .ordered , fastpath = True )
1651
1638
1652
1639
1653
1640
##### The Series.cat accessor #####
@@ -1696,7 +1683,6 @@ def _delegate_method(self, name, *args, **kwargs):
1696
1683
if not res is None :
1697
1684
return Series (res , index = self .index )
1698
1685
1699
- # TODO: remove levels after the deprecation period
1700
1686
CategoricalAccessor ._add_delegate_accessors (delegate = Categorical ,
1701
1687
accessors = ["categories" , "ordered" ],
1702
1688
typ = 'property' )
0 commit comments