1
1
# -*- coding: utf-8 -*-
2
2
3
3
from __future__ import print_function
4
+
5
+ import itertools
4
6
from datetime import timedelta
5
7
6
8
import numpy as np
9
+
10
+ import pandas as pd
11
+ import pandas .util .testing as tm
7
12
from pandas import (DataFrame , Series , date_range , Timedelta , Timestamp ,
8
13
compat , concat , option_context )
9
14
from pandas .compat import u
10
- from pandas .types .dtypes import DatetimeTZDtype
11
15
from pandas .tests .frame .common import TestData
16
+ from pandas .types .dtypes import DatetimeTZDtype
12
17
from pandas .util .testing import (assert_series_equal ,
13
18
assert_frame_equal ,
14
19
makeCustomDataframe as mkdf )
15
- import pandas .util .testing as tm
16
- import pandas as pd
17
20
18
21
19
22
class TestDataFrameDataTypes (tm .TestCase , TestData ):
20
-
21
23
def test_concat_empty_dataframe_dtypes (self ):
22
24
df = DataFrame (columns = list ("abc" ))
23
25
df ['a' ] = df ['a' ].astype (np .bool_ )
@@ -198,7 +200,7 @@ def test_select_dtypes_not_an_attr_but_still_valid_dtype(self):
198
200
def test_select_dtypes_empty (self ):
199
201
df = DataFrame ({'a' : list ('abc' ), 'b' : list (range (1 , 4 ))})
200
202
with tm .assertRaisesRegexp (ValueError , 'at least one of include or '
201
- 'exclude must be nonempty' ):
203
+ 'exclude must be nonempty' ):
202
204
df .select_dtypes ()
203
205
204
206
def test_select_dtypes_raises_on_string (self ):
@@ -536,7 +538,6 @@ def test_arg_for_errors_in_astype(self):
536
538
537
539
538
540
class TestDataFrameDatetimeWithTZ (tm .TestCase , TestData ):
539
-
540
541
def test_interleave (self ):
541
542
542
543
# interleave with object
@@ -635,33 +636,44 @@ def test_values_is_ndarray_with_datetime64tz(self):
635
636
["B" ],
636
637
["B" , "B" ],
637
638
]:
638
- self .assertTrue (type (df [col ].values )== np .ndarray )
639
-
639
+ self .assertTrue (type (df [col ].values ) == np .ndarray )
640
640
641
641
def test_values_dtypes_with_datetime64tz (self ):
642
- df = DataFrame ({'A' : date_range ('20130101' , periods = 3 ),
643
- 'B' : date_range ('20130101' , periods = 3 , tz = 'US/Eastern' ),
642
+ df = DataFrame ({'dt' : date_range ('20130101' , periods = 3 ),
643
+ 'dttz' : date_range ('20130101' , periods = 3 , tz = 'US/Eastern' ),
644
+ 'td' : date_range ('20130102' , periods = 3 )
645
+ - date_range ('20130101' , periods = 3 ),
646
+ 'cat' : pd .Categorical (['a' , 'b' , 'b' ]),
647
+ 'b' : [True , False , False ],
648
+ 'i' : [1 , 2 , 3 ],
649
+ 'f' : [1.3 , 2 , 3 ],
650
+ 'c' : [1j , 2 , 3 ],
644
651
})
645
652
646
- for col in [
647
- ["A" ],
648
- ["A" , "A" ],
649
- ["A" , "B" ],
650
- ["B" ],
651
- ["B" , "B" ],
652
- ]:
653
+ cols = itertools .chain (itertools .combinations_with_replacement (df .columns , 1 ),
654
+ itertools .combinations_with_replacement (df .columns , 2 ))
655
+ for col in cols :
653
656
654
- df_sub = df [col ]
657
+ df_sub = df [list ( col ) ]
655
658
arr = df_sub .values
656
-
657
- # array has the same dtype as dataframe only and only if
658
- # - all columns are of type datetime64[ns]
659
- # TODO: replace 2nd condition by 'all columns are of type datetime64[ns,same timezone]'
660
- # - all columns are of the same dtype being one of type datetime64[ns]
661
- # or datetime64[ns,tz] with the same tz
662
- if all (df_sub .dtypes .values == "<M8[ns]" ):
663
- self .assertTrue (arr .dtype == df_sub .dtypes .values [0 ])
664
-
665
- # otherwise, dtype is object
659
+ dts = df_sub .dtypes .values
660
+ dt = arr .dtype
661
+
662
+ # all columns of the same type
663
+ if len (set (dts )) == 1 :
664
+ if dts [0 ] in ("<M8[ns]" , "<m8[ns]" ,
665
+ bool , complex , int , float ):
666
+ self .assertTrue (dt == dts [0 ])
667
+ else :
668
+ self .assertTrue (dt == object )
669
+
670
+ # different type of columns
666
671
else :
667
- self .assertTrue (arr .dtype == object )
672
+ # all numeric and complex
673
+ if all (np .in1d (dts , (complex , int , float ))) and complex in dts :
674
+ self .assertTrue (arr .dtype == complex )
675
+ # all numeric and float
676
+ elif all (np .in1d (dts , (complex , int , float ))) and float in dts :
677
+ self .assertTrue (arr .dtype == float )
678
+ else :
679
+ self .assertTrue (arr .dtype == object )
0 commit comments