File tree 4 files changed +24
-2
lines changed
4 files changed +24
-2
lines changed Original file line number Diff line number Diff line change @@ -45,8 +45,12 @@ Performance
45
45
46
46
.. _whatsnew_0160.performance:
47
47
48
+
48
49
- Fixed a severe performance regression for ``.loc`` indexing with an array or list (:issue:9126:).
49
50
51
+ - Improved the speed of `nunique` by calling `unique` instead of `value_counts` (:issue:`9129`, :issue:`7771`)
52
+
53
+
50
54
Bug Fixes
51
55
~~~~~~~~~
52
56
@@ -114,3 +118,4 @@ Bug Fixes
114
118
115
119
- DataFrame now properly supports simultaneous ``copy`` and ``dtype`` arguments in constructor (:issue:`9099`)
116
120
- Bug in read_csv when using skiprows on a file with CR line endings with the c engine. (:issue:`9079`)
121
+ - isnull now detects NaT in PeriodIndex (:issue:`9129`)
Original file line number Diff line number Diff line change @@ -441,7 +441,12 @@ def nunique(self, dropna=True):
441
441
-------
442
442
nunique : int
443
443
"""
444
- return len (self .value_counts (dropna = dropna ))
444
+ uniqs = self .unique ()
445
+ n = len (uniqs )
446
+ if dropna and com .isnull (uniqs ).any ():
447
+ n -= 1
448
+ return n
449
+
445
450
446
451
def factorize (self , sort = False , na_sentinel = - 1 ):
447
452
"""
Original file line number Diff line number Diff line change @@ -302,7 +302,7 @@ def _isnull_ndarraylike(obj):
302
302
vec = lib .isnullobj (values .ravel ())
303
303
result [...] = vec .reshape (shape )
304
304
305
- elif dtype in _DATELIKE_DTYPES :
305
+ elif is_datetimelike ( obj ) :
306
306
# this is the NaT pattern
307
307
result = values .view ('i8' ) == tslib .iNaT
308
308
else :
@@ -2366,6 +2366,9 @@ def is_datetime_arraylike(arr):
2366
2366
return arr .dtype == object and lib .infer_dtype (arr ) == 'datetime'
2367
2367
return getattr (arr , 'inferred_type' , None ) == 'datetime'
2368
2368
2369
+ def is_datetimelike (arr ):
2370
+ return arr .dtype in _DATELIKE_DTYPES or isinstance (arr , ABCPeriodIndex )
2371
+
2369
2372
def _coerce_to_dtype (dtype ):
2370
2373
""" coerce a string / np.dtype to a dtype """
2371
2374
if is_categorical_dtype (dtype ):
Original file line number Diff line number Diff line change @@ -157,6 +157,15 @@ def test_isnull_datetime():
157
157
assert (mask [0 ])
158
158
assert (not mask [1 :].any ())
159
159
160
+ # GH 9129
161
+ pidx = idx .to_period (freq = 'M' )
162
+ mask = isnull (pidx )
163
+ assert (mask [0 ])
164
+ assert (not mask [1 :].any ())
165
+
166
+ mask = isnull (pidx [1 :])
167
+ assert (not mask .any ())
168
+
160
169
161
170
class TestIsNull (tm .TestCase ):
162
171
def test_0d_array (self ):
You can’t perform that action at this time.
0 commit comments