9
9
10
10
import pandas as pd
11
11
import pandas .compat as compat
12
- from pandas .types .common import is_object_dtype , is_datetimetz
12
+ from pandas .types .common import (is_object_dtype , is_datetimetz ,
13
+ needs_i8_conversion )
13
14
import pandas .util .testing as tm
14
15
from pandas import (Series , Index , DatetimeIndex , TimedeltaIndex , PeriodIndex ,
15
16
Timedelta )
16
17
from pandas .compat import u , StringIO
17
18
from pandas .compat .numpy import np_array_datetime64_compat
18
19
from pandas .core .base import (FrozenList , FrozenNDArray , PandasDelegate ,
19
20
NoNewAttributesMixin )
20
- from pandas .types .common import is_datetime64_dtype
21
21
from pandas .tseries .base import DatetimeIndexOpsMixin
22
22
23
23
@@ -450,7 +450,6 @@ def test_nanops(self):
450
450
451
451
def test_value_counts_unique_nunique (self ):
452
452
for orig in self .objs :
453
-
454
453
o = orig .copy ()
455
454
klass = type (o )
456
455
values = o ._values
@@ -504,9 +503,10 @@ def test_value_counts_unique_nunique(self):
504
503
def test_value_counts_unique_nunique_null (self ):
505
504
506
505
for null_obj in [np .nan , None ]:
507
- for o in self .objs :
506
+ for orig in self .objs :
507
+ o = orig .copy ()
508
508
klass = type (o )
509
- values = o .values
509
+ values = o ._values
510
510
511
511
if not self ._allow_na_ops (o ):
512
512
continue
@@ -522,34 +522,43 @@ def test_value_counts_unique_nunique_null(self):
522
522
o [0 :2 ] = pd .tslib .iNaT
523
523
values = o ._values
524
524
525
- elif is_datetime64_dtype ( o ) or isinstance ( o , PeriodIndex ):
525
+ elif needs_i8_conversion ( o ):
526
526
values [0 :2 ] = pd .tslib .iNaT
527
+ values = o ._shallow_copy (values )
527
528
else :
528
529
values [0 :2 ] = null_obj
529
530
# check values has the same dtype as the original
531
+
530
532
self .assertEqual (values .dtype , o .dtype )
531
533
532
534
# create repeated values, 'n'th element is repeated by n+1
533
535
# times
534
- if isinstance (o , PeriodIndex ):
535
- # freq must be specified because repeat makes freq
536
- # ambiguous
536
+ if isinstance (o , ( DatetimeIndex , PeriodIndex ) ):
537
+ expected_index = o . copy ()
538
+ expected_index . name = None
537
539
538
- # resets name from Index
539
- expected_index = pd .Index (o , name = None )
540
540
# attach name to klass
541
- o = klass (np .repeat (values , range (1 , len (o ) + 1 )),
542
- freq = o .freq , name = 'a' )
543
- elif isinstance (o , Index ):
544
- expected_index = pd .Index (values , name = None )
545
- o = klass (
546
- np .repeat (values , range (1 , len (o ) + 1 )), name = 'a' )
541
+ o = klass (values .repeat (range (1 , len (o ) + 1 )))
542
+ o .name = 'a'
547
543
else :
548
- expected_index = pd .Index (values , name = None )
549
- idx = np .repeat (o .index .values , range (1 , len (o ) + 1 ))
550
- o = klass (
551
- np .repeat (values , range (
552
- 1 , len (o ) + 1 )), index = idx , name = 'a' )
544
+ if is_datetimetz (o ):
545
+ expected_index = orig ._values ._shallow_copy (values )
546
+ else :
547
+ expected_index = pd .Index (values )
548
+ expected_index .name = None
549
+ o = o .repeat (range (1 , len (o ) + 1 ))
550
+ o .name = 'a'
551
+
552
+ # check values has the same dtype as the original
553
+ self .assertEqual (o .dtype , orig .dtype )
554
+ # check values correctly have NaN
555
+ nanloc = np .zeros (len (o ), dtype = np .bool )
556
+ nanloc [:3 ] = True
557
+ if isinstance (o , Index ):
558
+ self .assert_numpy_array_equal (pd .isnull (o ), nanloc )
559
+ else :
560
+ exp = pd .Series (nanloc , o .index , name = 'a' )
561
+ self .assert_series_equal (pd .isnull (o ), exp )
553
562
554
563
expected_s_na = Series (list (range (10 , 2 , - 1 )) + [3 ],
555
564
index = expected_index [9 :0 :- 1 ],
@@ -578,7 +587,9 @@ def test_value_counts_unique_nunique_null(self):
578
587
self .assertIs (result [0 ], pd .NaT )
579
588
else :
580
589
tm .assert_numpy_array_equal (result [1 :], values [2 :])
590
+
581
591
self .assertTrue (pd .isnull (result [0 ]))
592
+ self .assertEqual (result .dtype , orig .dtype )
582
593
583
594
self .assertEqual (o .nunique (), 8 )
584
595
self .assertEqual (o .nunique (dropna = False ), 9 )
@@ -942,18 +953,14 @@ def test_fillna(self):
942
953
# # GH 11343
943
954
# though Index.fillna and Series.fillna has separate impl,
944
955
# test here to confirm these works as the same
945
- def get_fill_value (obj ):
946
- if isinstance (obj , pd .tseries .base .DatetimeIndexOpsMixin ):
947
- return obj .asobject .values [0 ]
948
- else :
949
- return obj .values [0 ]
950
956
951
- for o in self .objs :
952
- klass = type (o )
957
+ for orig in self .objs :
958
+
959
+ o = orig .copy ()
953
960
values = o .values
954
961
955
962
# values will not be changed
956
- result = o .fillna (get_fill_value ( o ) )
963
+ result = o .fillna (o . astype ( object ). values [ 0 ] )
957
964
if isinstance (o , Index ):
958
965
self .assert_index_equal (o , result )
959
966
else :
@@ -962,33 +969,30 @@ def get_fill_value(obj):
962
969
self .assertFalse (o is result )
963
970
964
971
for null_obj in [np .nan , None ]:
965
- for o in self .objs :
972
+ for orig in self .objs :
973
+ o = orig .copy ()
966
974
klass = type (o )
967
- values = o .values .copy ()
968
975
969
976
if not self ._allow_na_ops (o ):
970
977
continue
971
978
972
- # value for filling
973
- fill_value = get_fill_value (o )
979
+ if needs_i8_conversion (o ):
974
980
975
- # special assign to the numpy array
976
- if o .values .dtype == 'datetime64[ns]' or isinstance (
977
- o , PeriodIndex ):
978
- values [0 :2 ] = pd .tslib .iNaT
981
+ values = o .astype (object ).values
982
+ fill_value = values [0 ]
983
+ values [0 :2 ] = pd .NaT
979
984
else :
985
+ values = o .values .copy ()
986
+ fill_value = o .values [0 ]
980
987
values [0 :2 ] = null_obj
981
988
982
- if isinstance (o , PeriodIndex ):
983
- # freq must be specified because repeat makes freq
984
- # ambiguous
985
- expected = [fill_value .ordinal ] * 2 + list (values [2 :])
986
- expected = klass (ordinal = expected , freq = o .freq )
987
- o = klass (ordinal = values , freq = o .freq )
988
- else :
989
- expected = [fill_value ] * 2 + list (values [2 :])
990
- expected = klass (expected )
991
- o = klass (values )
989
+ expected = [fill_value ] * 2 + list (values [2 :])
990
+
991
+ expected = klass (expected )
992
+ o = klass (values )
993
+
994
+ # check values has the same dtype as the original
995
+ self .assertEqual (o .dtype , orig .dtype )
992
996
993
997
result = o .fillna (fill_value )
994
998
if isinstance (o , Index ):
0 commit comments