Skip to content

Commit 6fa7426

Browse files
committed
add test for issue pandas-dev#14052
1 parent 78f931d commit 6fa7426

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

pandas/tests/frame/test_dtypes.py

+41-29
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
# -*- coding: utf-8 -*-
22

33
from __future__ import print_function
4+
5+
import itertools
46
from datetime import timedelta
57

68
import numpy as np
9+
10+
import pandas as pd
11+
import pandas.util.testing as tm
712
from pandas import (DataFrame, Series, date_range, Timedelta, Timestamp,
813
compat, concat, option_context)
914
from pandas.compat import u
10-
from pandas.types.dtypes import DatetimeTZDtype
1115
from pandas.tests.frame.common import TestData
16+
from pandas.types.dtypes import DatetimeTZDtype
1217
from pandas.util.testing import (assert_series_equal,
1318
assert_frame_equal,
1419
makeCustomDataframe as mkdf)
15-
import pandas.util.testing as tm
16-
import pandas as pd
1720

1821

1922
class TestDataFrameDataTypes(tm.TestCase, TestData):
20-
2123
def test_concat_empty_dataframe_dtypes(self):
2224
df = DataFrame(columns=list("abc"))
2325
df['a'] = df['a'].astype(np.bool_)
@@ -198,7 +200,7 @@ def test_select_dtypes_not_an_attr_but_still_valid_dtype(self):
198200
def test_select_dtypes_empty(self):
199201
df = DataFrame({'a': list('abc'), 'b': list(range(1, 4))})
200202
with tm.assertRaisesRegexp(ValueError, 'at least one of include or '
201-
'exclude must be nonempty'):
203+
'exclude must be nonempty'):
202204
df.select_dtypes()
203205

204206
def test_select_dtypes_raises_on_string(self):
@@ -536,7 +538,6 @@ def test_arg_for_errors_in_astype(self):
536538

537539

538540
class TestDataFrameDatetimeWithTZ(tm.TestCase, TestData):
539-
540541
def test_interleave(self):
541542

542543
# interleave with object
@@ -635,33 +636,44 @@ def test_values_is_ndarray_with_datetime64tz(self):
635636
["B"],
636637
["B", "B"],
637638
]:
638-
self.assertTrue(type(df[col].values)==np.ndarray)
639-
639+
self.assertTrue(type(df[col].values) == np.ndarray)
640640

641641
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],
644651
})
645652

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:
653656

654-
df_sub = df[col]
657+
df_sub = df[list(col)]
655658
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
666671
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

Comments
 (0)