|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
3 | | -import pandas as pd |
| 3 | +import pytest |
4 | 4 | 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