Skip to content

Commit 0f2dfbe

Browse files
jschendeljreback
authored andcommitted
TST: Parametrize dtypes tests - test_common.py and test_concat.py (#20340)
1 parent c857b4f commit 0f2dfbe

File tree

2 files changed

+80
-102
lines changed

2 files changed

+80
-102
lines changed

pandas/tests/dtypes/test_common.py

+30-28
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,46 @@ class TestPandasDtype(object):
1616

1717
# Passing invalid dtype, both as a string or object, must raise TypeError
1818
# Per issue GH15520
19-
def test_invalid_dtype_error(self):
20-
msg = 'not understood'
21-
invalid_list = [pd.Timestamp, 'pd.Timestamp', list]
22-
for dtype in invalid_list:
23-
with tm.assert_raises_regex(TypeError, msg):
24-
com.pandas_dtype(dtype)
25-
26-
valid_list = [object, 'float64', np.object_, np.dtype('object'), 'O',
27-
np.float64, float, np.dtype('float64')]
28-
for dtype in valid_list:
29-
com.pandas_dtype(dtype)
30-
31-
def test_numpy_dtype(self):
32-
for dtype in ['M8[ns]', 'm8[ns]', 'object', 'float64', 'int64']:
33-
assert com.pandas_dtype(dtype) == np.dtype(dtype)
19+
@pytest.mark.parametrize('box', [pd.Timestamp, 'pd.Timestamp', list])
20+
def test_invalid_dtype_error(self, box):
21+
with tm.assert_raises_regex(TypeError, 'not understood'):
22+
com.pandas_dtype(box)
23+
24+
@pytest.mark.parametrize('dtype', [
25+
object, 'float64', np.object_, np.dtype('object'), 'O',
26+
np.float64, float, np.dtype('float64')])
27+
def test_pandas_dtype_valid(self, dtype):
28+
assert com.pandas_dtype(dtype) == dtype
29+
30+
@pytest.mark.parametrize('dtype', [
31+
'M8[ns]', 'm8[ns]', 'object', 'float64', 'int64'])
32+
def test_numpy_dtype(self, dtype):
33+
assert com.pandas_dtype(dtype) == np.dtype(dtype)
3434

3535
def test_numpy_string_dtype(self):
3636
# do not parse freq-like string as period dtype
3737
assert com.pandas_dtype('U') == np.dtype('U')
3838
assert com.pandas_dtype('S') == np.dtype('S')
3939

40-
def test_datetimetz_dtype(self):
41-
for dtype in ['datetime64[ns, US/Eastern]',
42-
'datetime64[ns, Asia/Tokyo]',
43-
'datetime64[ns, UTC]']:
44-
assert com.pandas_dtype(dtype) is DatetimeTZDtype(dtype)
45-
assert com.pandas_dtype(dtype) == DatetimeTZDtype(dtype)
46-
assert com.pandas_dtype(dtype) == dtype
40+
@pytest.mark.parametrize('dtype', [
41+
'datetime64[ns, US/Eastern]',
42+
'datetime64[ns, Asia/Tokyo]',
43+
'datetime64[ns, UTC]'])
44+
def test_datetimetz_dtype(self, dtype):
45+
assert com.pandas_dtype(dtype) is DatetimeTZDtype(dtype)
46+
assert com.pandas_dtype(dtype) == DatetimeTZDtype(dtype)
47+
assert com.pandas_dtype(dtype) == dtype
4748

4849
def test_categorical_dtype(self):
4950
assert com.pandas_dtype('category') == CategoricalDtype()
5051

51-
def test_period_dtype(self):
52-
for dtype in ['period[D]', 'period[3M]', 'period[U]',
53-
'Period[D]', 'Period[3M]', 'Period[U]']:
54-
assert com.pandas_dtype(dtype) is PeriodDtype(dtype)
55-
assert com.pandas_dtype(dtype) == PeriodDtype(dtype)
56-
assert com.pandas_dtype(dtype) == dtype
52+
@pytest.mark.parametrize('dtype', [
53+
'period[D]', 'period[3M]', 'period[U]',
54+
'Period[D]', 'Period[3M]', 'Period[U]'])
55+
def test_period_dtype(self, dtype):
56+
assert com.pandas_dtype(dtype) is PeriodDtype(dtype)
57+
assert com.pandas_dtype(dtype) == PeriodDtype(dtype)
58+
assert com.pandas_dtype(dtype) == dtype
5759

5860

5961
dtypes = dict(datetime_tz=com.pandas_dtype('datetime64[ns, US/Eastern]'),

pandas/tests/dtypes/test_concat.py

+50-74
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,53 @@
11
# -*- coding: utf-8 -*-
22

3-
import pandas as pd
3+
import pytest
44
import pandas.core.dtypes.concat as _concat
5-
6-
7-
class TestConcatCompat(object):
8-
9-
def check_concat(self, to_concat, exp):
10-
for klass in [pd.Index, pd.Series]:
11-
to_concat_klass = [klass(c) for c in to_concat]
12-
res = _concat.get_dtype_kinds(to_concat_klass)
13-
assert res == set(exp)
14-
15-
def test_get_dtype_kinds(self):
16-
to_concat = [['a'], [1, 2]]
17-
self.check_concat(to_concat, ['i', 'object'])
18-
19-
to_concat = [[3, 4], [1, 2]]
20-
self.check_concat(to_concat, ['i'])
21-
22-
to_concat = [[3, 4], [1, 2.1]]
23-
self.check_concat(to_concat, ['i', 'f'])
24-
25-
def test_get_dtype_kinds_datetimelike(self):
26-
to_concat = [pd.DatetimeIndex(['2011-01-01']),
27-
pd.DatetimeIndex(['2011-01-02'])]
28-
self.check_concat(to_concat, ['datetime'])
29-
30-
to_concat = [pd.TimedeltaIndex(['1 days']),
31-
pd.TimedeltaIndex(['2 days'])]
32-
self.check_concat(to_concat, ['timedelta'])
33-
34-
def test_get_dtype_kinds_datetimelike_object(self):
35-
to_concat = [pd.DatetimeIndex(['2011-01-01']),
36-
pd.DatetimeIndex(['2011-01-02'], tz='US/Eastern')]
37-
self.check_concat(to_concat,
38-
['datetime', 'datetime64[ns, US/Eastern]'])
39-
40-
to_concat = [pd.DatetimeIndex(['2011-01-01'], tz='Asia/Tokyo'),
41-
pd.DatetimeIndex(['2011-01-02'], tz='US/Eastern')]
42-
self.check_concat(to_concat,
43-
['datetime64[ns, Asia/Tokyo]',
44-
'datetime64[ns, US/Eastern]'])
45-
46-
# timedelta has single type
47-
to_concat = [pd.TimedeltaIndex(['1 days']),
48-
pd.TimedeltaIndex(['2 hours'])]
49-
self.check_concat(to_concat, ['timedelta'])
50-
51-
to_concat = [pd.DatetimeIndex(['2011-01-01'], tz='Asia/Tokyo'),
52-
pd.TimedeltaIndex(['1 days'])]
53-
self.check_concat(to_concat,
54-
['datetime64[ns, Asia/Tokyo]', 'timedelta'])
55-
56-
def test_get_dtype_kinds_period(self):
57-
# because we don't have Period dtype (yet),
58-
# Series results in object dtype
59-
to_concat = [pd.PeriodIndex(['2011-01'], freq='M'),
60-
pd.PeriodIndex(['2011-01'], freq='M')]
61-
res = _concat.get_dtype_kinds(to_concat)
62-
assert res == set(['period[M]'])
63-
64-
to_concat = [pd.Series([pd.Period('2011-01', freq='M')]),
65-
pd.Series([pd.Period('2011-02', freq='M')])]
66-
res = _concat.get_dtype_kinds(to_concat)
67-
assert res == set(['object'])
68-
69-
to_concat = [pd.PeriodIndex(['2011-01'], freq='M'),
70-
pd.PeriodIndex(['2011-01'], freq='D')]
71-
res = _concat.get_dtype_kinds(to_concat)
72-
assert res == set(['period[M]', 'period[D]'])
73-
74-
to_concat = [pd.Series([pd.Period('2011-01', freq='M')]),
75-
pd.Series([pd.Period('2011-02', freq='D')])]
76-
res = _concat.get_dtype_kinds(to_concat)
77-
assert res == set(['object'])
5+
from pandas import (
6+
Index, DatetimeIndex, PeriodIndex, TimedeltaIndex, Series, Period)
7+
8+
9+
@pytest.mark.parametrize('to_concat, expected', [
10+
# int/float/str
11+
([['a'], [1, 2]], ['i', 'object']),
12+
([[3, 4], [1, 2]], ['i']),
13+
([[3, 4], [1, 2.1]], ['i', 'f']),
14+
15+
# datetimelike
16+
([DatetimeIndex(['2011-01-01']), DatetimeIndex(['2011-01-02'])],
17+
['datetime']),
18+
([TimedeltaIndex(['1 days']), TimedeltaIndex(['2 days'])],
19+
['timedelta']),
20+
21+
# datetimelike object
22+
([DatetimeIndex(['2011-01-01']),
23+
DatetimeIndex(['2011-01-02'], tz='US/Eastern')],
24+
['datetime', 'datetime64[ns, US/Eastern]']),
25+
([DatetimeIndex(['2011-01-01'], tz='Asia/Tokyo'),
26+
DatetimeIndex(['2011-01-02'], tz='US/Eastern')],
27+
['datetime64[ns, Asia/Tokyo]', 'datetime64[ns, US/Eastern]']),
28+
([TimedeltaIndex(['1 days']), TimedeltaIndex(['2 hours'])],
29+
['timedelta']),
30+
([DatetimeIndex(['2011-01-01'], tz='Asia/Tokyo'),
31+
TimedeltaIndex(['1 days'])],
32+
['datetime64[ns, Asia/Tokyo]', 'timedelta'])])
33+
@pytest.mark.parametrize('klass', [Index, Series])
34+
def test_get_dtype_kinds(klass, to_concat, expected):
35+
to_concat_klass = [klass(c) for c in to_concat]
36+
result = _concat.get_dtype_kinds(to_concat_klass)
37+
assert result == set(expected)
38+
39+
40+
@pytest.mark.parametrize('to_concat, expected', [
41+
# because we don't have Period dtype (yet),
42+
# Series results in object dtype
43+
([PeriodIndex(['2011-01'], freq='M'),
44+
PeriodIndex(['2011-01'], freq='M')], ['period[M]']),
45+
([Series([Period('2011-01', freq='M')]),
46+
Series([Period('2011-02', freq='M')])], ['object']),
47+
([PeriodIndex(['2011-01'], freq='M'),
48+
PeriodIndex(['2011-01'], freq='D')], ['period[M]', 'period[D]']),
49+
([Series([Period('2011-01', freq='M')]),
50+
Series([Period('2011-02', freq='D')])], ['object'])])
51+
def test_get_dtype_kinds_period(to_concat, expected):
52+
result = _concat.get_dtype_kinds(to_concat)
53+
assert result == set(expected)

0 commit comments

Comments
 (0)