99
1010import pandas as pd
1111import 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 )
1314import pandas .util .testing as tm
1415from pandas import (Series , Index , DatetimeIndex , TimedeltaIndex , PeriodIndex ,
1516 Timedelta )
1617from pandas .compat import u , StringIO
1718from pandas .compat .numpy import np_array_datetime64_compat
1819from pandas .core .base import (FrozenList , FrozenNDArray , PandasDelegate ,
1920 NoNewAttributesMixin )
20- from pandas .types .common import is_datetime64_dtype
2121from pandas .tseries .base import DatetimeIndexOpsMixin
2222
2323
@@ -450,7 +450,6 @@ def test_nanops(self):
450450
451451 def test_value_counts_unique_nunique (self ):
452452 for orig in self .objs :
453-
454453 o = orig .copy ()
455454 klass = type (o )
456455 values = o ._values
@@ -504,9 +503,10 @@ def test_value_counts_unique_nunique(self):
504503 def test_value_counts_unique_nunique_null (self ):
505504
506505 for null_obj in [np .nan , None ]:
507- for o in self .objs :
506+ for orig in self .objs :
507+ o = orig .copy ()
508508 klass = type (o )
509- values = o .values
509+ values = o ._values
510510
511511 if not self ._allow_na_ops (o ):
512512 continue
@@ -522,34 +522,43 @@ def test_value_counts_unique_nunique_null(self):
522522 o [0 :2 ] = pd .tslib .iNaT
523523 values = o ._values
524524
525- elif is_datetime64_dtype ( o ) or isinstance ( o , PeriodIndex ):
525+ elif needs_i8_conversion ( o ):
526526 values [0 :2 ] = pd .tslib .iNaT
527+ values = o ._shallow_copy (values )
527528 else :
528529 values [0 :2 ] = null_obj
529530 # check values has the same dtype as the original
531+
530532 self .assertEqual (values .dtype , o .dtype )
531533
532534 # create repeated values, 'n'th element is repeated by n+1
533535 # 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
537539
538- # resets name from Index
539- expected_index = pd .Index (o , name = None )
540540 # 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'
547543 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 )
553562
554563 expected_s_na = Series (list (range (10 , 2 , - 1 )) + [3 ],
555564 index = expected_index [9 :0 :- 1 ],
@@ -578,7 +587,9 @@ def test_value_counts_unique_nunique_null(self):
578587 self .assertIs (result [0 ], pd .NaT )
579588 else :
580589 tm .assert_numpy_array_equal (result [1 :], values [2 :])
590+
581591 self .assertTrue (pd .isnull (result [0 ]))
592+ self .assertEqual (result .dtype , orig .dtype )
582593
583594 self .assertEqual (o .nunique (), 8 )
584595 self .assertEqual (o .nunique (dropna = False ), 9 )
@@ -942,18 +953,14 @@ def test_fillna(self):
942953 # # GH 11343
943954 # though Index.fillna and Series.fillna has separate impl,
944955 # 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 ]
950956
951- for o in self .objs :
952- klass = type (o )
957+ for orig in self .objs :
958+
959+ o = orig .copy ()
953960 values = o .values
954961
955962 # values will not be changed
956- result = o .fillna (get_fill_value ( o ) )
963+ result = o .fillna (o . astype ( object ). values [ 0 ] )
957964 if isinstance (o , Index ):
958965 self .assert_index_equal (o , result )
959966 else :
@@ -962,33 +969,30 @@ def get_fill_value(obj):
962969 self .assertFalse (o is result )
963970
964971 for null_obj in [np .nan , None ]:
965- for o in self .objs :
972+ for orig in self .objs :
973+ o = orig .copy ()
966974 klass = type (o )
967- values = o .values .copy ()
968975
969976 if not self ._allow_na_ops (o ):
970977 continue
971978
972- # value for filling
973- fill_value = get_fill_value (o )
979+ if needs_i8_conversion (o ):
974980
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
979984 else :
985+ values = o .values .copy ()
986+ fill_value = o .values [0 ]
980987 values [0 :2 ] = null_obj
981988
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 )
992996
993997 result = o .fillna (fill_value )
994998 if isinstance (o , Index ):
0 commit comments