From a9295a6d2ac6c1c9719fa2349e644281af3c3d53 Mon Sep 17 00:00:00 2001 From: sinhrks Date: Wed, 30 Mar 2016 22:17:13 +0900 Subject: [PATCH] TST: Add period and other dtype related tests --- pandas/tests/frame/test_constructors.py | 14 ++++++++ pandas/tests/indexes/test_base.py | 44 ++++++++++++++++++++++++ pandas/tests/indexes/test_multi.py | 41 ++++++++++++++++++++++ pandas/tests/series/test_constructors.py | 2 ++ pandas/tests/test_categorical.py | 25 ++++++++++++++ pandas/tests/test_dtypes.py | 7 ++++ pandas/tools/tests/test_merge.py | 9 +++++ pandas/tseries/tests/test_period.py | 19 ++++++++++ pandas/tseries/tests/test_timeseries.py | 16 +++++++-- 9 files changed, 175 insertions(+), 2 deletions(-) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index ec18844354f6b..861be35f6a2b4 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -483,6 +483,20 @@ def create_data(constructor): assert_frame_equal(result_timedelta, expected) assert_frame_equal(result_Timedelta, expected) + def test_constructor_period(self): + # PeriodIndex + a = pd.PeriodIndex(['2012-01', 'NaT', '2012-04'], freq='M') + b = pd.PeriodIndex(['2012-02-01', '2012-03-01', 'NaT'], freq='D') + df = pd.DataFrame({'a': a, 'b': b}) + self.assertEqual(df['a'].dtype, 'object') + self.assertEqual(df['b'].dtype, 'object') + + # list of periods + df = pd.DataFrame({'a': a.asobject.tolist(), + 'b': b.asobject.tolist()}) + self.assertEqual(df['a'].dtype, 'object') + self.assertEqual(df['b'].dtype, 'object') + def test_nested_dict_frame_constructor(self): rng = pd.period_range('1/1/2000', periods=5) df = DataFrame(randn(10, 5), columns=rng) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 24a20f7adf624..394fd31a0663f 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -103,6 +103,50 @@ def test_construction_list_mixed_tuples(self): self.assertIsInstance(idx2, Index) and self.assertNotInstance( idx2, MultiIndex) + def test_constructor_from_index_datetimetz(self): + idx = pd.date_range('2015-01-01 10:00', freq='D', periods=3, + tz='US/Eastern') + result = pd.Index(idx) + tm.assert_index_equal(result, idx) + self.assertEqual(result.tz, idx.tz) + + result = pd.Index(idx.asobject) + tm.assert_index_equal(result, idx) + self.assertEqual(result.tz, idx.tz) + + def test_constructor_from_index_timedelta(self): + idx = pd.timedelta_range('1 days', freq='D', periods=3) + result = pd.Index(idx) + tm.assert_index_equal(result, idx) + + result = pd.Index(idx.asobject) + tm.assert_index_equal(result, idx) + + def test_constructor_from_index_period(self): + idx = pd.period_range('2015-01-01', freq='D', periods=3) + result = pd.Index(idx) + tm.assert_index_equal(result, idx) + + result = pd.Index(idx.asobject) + tm.assert_index_equal(result, idx) + + def test_constructor_from_series_datetimetz(self): + idx = pd.date_range('2015-01-01 10:00', freq='D', periods=3, + tz='US/Eastern') + result = pd.Index(pd.Series(idx)) + tm.assert_index_equal(result, idx) + self.assertEqual(result.tz, idx.tz) + + def test_constructor_from_series_timedelta(self): + idx = pd.timedelta_range('1 days', freq='D', periods=3) + result = pd.Index(pd.Series(idx)) + tm.assert_index_equal(result, idx) + + def test_constructor_from_series_period(self): + idx = pd.period_range('2015-01-01', freq='D', periods=3) + result = pd.Index(pd.Series(idx)) + tm.assert_index_equal(result, idx) + def test_constructor_from_series(self): expected = DatetimeIndex([Timestamp('20110101'), Timestamp('20120101'), diff --git a/pandas/tests/indexes/test_multi.py b/pandas/tests/indexes/test_multi.py index f70ea49bd4c29..6443dd278bd6b 100644 --- a/pandas/tests/indexes/test_multi.py +++ b/pandas/tests/indexes/test_multi.py @@ -558,6 +558,47 @@ def test_from_arrays(self): ]))) self.assertTrue(result.levels[1].equals(Index(['a', 'b']))) + def test_from_arrays_index_series_datetimetz(self): + idx1 = pd.date_range('2015-01-01 10:00', freq='D', periods=3, + tz='US/Eastern') + idx2 = pd.date_range('2015-01-01 10:00', freq='H', periods=3, + tz='Asia/Tokyo') + result = pd.MultiIndex.from_arrays([idx1, idx2]) + tm.assert_index_equal(result.get_level_values(0), idx1) + tm.assert_index_equal(result.get_level_values(1), idx2) + + result2 = pd.MultiIndex.from_arrays([pd.Series(idx1), pd.Series(idx2)]) + tm.assert_index_equal(result2.get_level_values(0), idx1) + tm.assert_index_equal(result2.get_level_values(1), idx2) + + tm.assert_index_equal(result, result2) + + def test_from_arrays_index_series_timedelta(self): + idx1 = pd.timedelta_range('1 days', freq='D', periods=3) + idx2 = pd.timedelta_range('2 hours', freq='H', periods=3) + result = pd.MultiIndex.from_arrays([idx1, idx2]) + tm.assert_index_equal(result.get_level_values(0), idx1) + tm.assert_index_equal(result.get_level_values(1), idx2) + + result2 = pd.MultiIndex.from_arrays([pd.Series(idx1), pd.Series(idx2)]) + tm.assert_index_equal(result2.get_level_values(0), idx1) + tm.assert_index_equal(result2.get_level_values(1), idx2) + + tm.assert_index_equal(result, result2) + + def test_from_arrays_index_series_period(self): + idx1 = pd.period_range('2011-01-01', freq='D', periods=3) + idx2 = pd.period_range('2015-01-01', freq='H', periods=3) + result = pd.MultiIndex.from_arrays([idx1, idx2]) + tm.assert_index_equal(result.get_level_values(0), idx1) + tm.assert_index_equal(result.get_level_values(1), idx2) + + result2 = pd.MultiIndex.from_arrays([pd.Series(idx1), pd.Series(idx2)]) + tm.assert_index_equal(result2.get_level_values(0), idx1) + tm.assert_index_equal(result2.get_level_values(1), idx2) + + tm.assert_index_equal(result, result2) + def test_from_product(self): first = ['foo', 'bar', 'buz'] diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 89793018b5193..68733700e1483 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -498,6 +498,8 @@ def test_constructor_periodindex(self): expected = Series(pi.asobject) assert_series_equal(s, expected) + self.assertEqual(s.dtype, 'object') + def test_constructor_dict(self): d = {'a': 0., 'b': 1., 'c': 2.} result = Series(d, index=['b', 'c', 'd', 'a']) diff --git a/pandas/tests/test_categorical.py b/pandas/tests/test_categorical.py index b60cbcba45dd8..607e6ae04148e 100755 --- a/pandas/tests/test_categorical.py +++ b/pandas/tests/test_categorical.py @@ -340,6 +340,31 @@ def test_constructor_with_datetimelike(self): result = repr(c) self.assertTrue('NaT' in result) + def test_constructor_from_index_series_datetimetz(self): + idx = pd.date_range('2015-01-01 10:00', freq='D', periods=3, + tz='US/Eastern') + result = pd.Categorical.from_array(idx) + tm.assert_index_equal(result.categories, idx) + + result = pd.Categorical.from_array(pd.Series(idx)) + tm.assert_index_equal(result.categories, idx) + + def test_constructor_from_index_series_timedelta(self): + idx = pd.timedelta_range('1 days', freq='D', periods=3) + result = pd.Categorical.from_array(idx) + tm.assert_index_equal(result.categories, idx) + + result = pd.Categorical.from_array(pd.Series(idx)) + tm.assert_index_equal(result.categories, idx) + + def test_constructor_from_index_series_period(self): + idx = pd.period_range('2015-01-01', freq='D', periods=3) + result = pd.Categorical.from_array(idx) + tm.assert_index_equal(result.categories, idx) + + result = pd.Categorical.from_array(pd.Series(idx)) + tm.assert_index_equal(result.categories, idx) + def test_from_codes(self): # too few categories diff --git a/pandas/tests/test_dtypes.py b/pandas/tests/test_dtypes.py index 943e7c92d988b..f12adab386dab 100644 --- a/pandas/tests/test_dtypes.py +++ b/pandas/tests/test_dtypes.py @@ -4,6 +4,7 @@ import nose import numpy as np from pandas import Series, Categorical, date_range +import pandas.core.common as com from pandas.core.common import (CategoricalDtype, is_categorical_dtype, is_categorical, DatetimeTZDtype, is_datetime64tz_dtype, is_datetimetz, @@ -97,6 +98,12 @@ def test_subclass(self): self.assertTrue(issubclass(type(a), type(a))) self.assertTrue(issubclass(type(a), type(b))) + def test_coerce_to_dtype(self): + self.assertEqual(com._coerce_to_dtype('datetime64[ns, US/Eastern]'), + DatetimeTZDtype('ns', 'US/Eastern')) + self.assertEqual(com._coerce_to_dtype('datetime64[ns, Asia/Tokyo]'), + DatetimeTZDtype('ns', 'Asia/Tokyo')) + def test_compat(self): self.assertFalse(is_datetime64_ns_dtype(self.dtype)) self.assertFalse(is_datetime64_ns_dtype('datetime64[ns, US/Eastern]')) diff --git a/pandas/tools/tests/test_merge.py b/pandas/tools/tests/test_merge.py index eff4b62ff52f3..6d5370bedf65a 100644 --- a/pandas/tools/tests/test_merge.py +++ b/pandas/tools/tests/test_merge.py @@ -1065,6 +1065,8 @@ def test_merge_on_datetime64tz(self): 'key': [1., 2, 3]}) result = pd.merge(left, right, on='key', how='outer') assert_frame_equal(result, expected) + self.assertEqual(result['value_x'].dtype, 'datetime64[ns, US/Eastern]') + self.assertEqual(result['value_y'].dtype, 'datetime64[ns, US/Eastern]') def test_merge_on_periods(self): left = pd.DataFrame({'key': pd.period_range('20151010', periods=2, @@ -1095,6 +1097,8 @@ def test_merge_on_periods(self): 'key': [1., 2, 3]}) result = pd.merge(left, right, on='key', how='outer') assert_frame_equal(result, expected) + self.assertEqual(result['value_x'].dtype, 'object') + self.assertEqual(result['value_y'].dtype, 'object') def test_concat_NaT_series(self): # GH 11693 @@ -1216,6 +1220,7 @@ def test_concat_period_series(self): expected = Series([x[0], x[1], y[0], y[1]], dtype='object') result = concat([x, y], ignore_index=True) tm.assert_series_equal(result, expected) + self.assertEqual(result.dtype, 'object') # different freq x = Series(pd.PeriodIndex(['2015-11-01', '2015-12-01'], freq='D')) @@ -1223,12 +1228,14 @@ def test_concat_period_series(self): expected = Series([x[0], x[1], y[0], y[1]], dtype='object') result = concat([x, y], ignore_index=True) tm.assert_series_equal(result, expected) + self.assertEqual(result.dtype, 'object') x = Series(pd.PeriodIndex(['2015-11-01', '2015-12-01'], freq='D')) y = Series(pd.PeriodIndex(['2015-11-01', '2015-12-01'], freq='M')) expected = Series([x[0], x[1], y[0], y[1]], dtype='object') result = concat([x, y], ignore_index=True) tm.assert_series_equal(result, expected) + self.assertEqual(result.dtype, 'object') # non-period x = Series(pd.PeriodIndex(['2015-11-01', '2015-12-01'], freq='D')) @@ -1236,12 +1243,14 @@ def test_concat_period_series(self): expected = Series([x[0], x[1], y[0], y[1]], dtype='object') result = concat([x, y], ignore_index=True) tm.assert_series_equal(result, expected) + self.assertEqual(result.dtype, 'object') x = Series(pd.PeriodIndex(['2015-11-01', '2015-12-01'], freq='D')) y = Series(['A', 'B']) expected = Series([x[0], x[1], y[0], y[1]], dtype='object') result = concat([x, y], ignore_index=True) tm.assert_series_equal(result, expected) + self.assertEqual(result.dtype, 'object') def test_indicator(self): # PR #10054. xref #7412 and closes #8790. diff --git a/pandas/tseries/tests/test_period.py b/pandas/tseries/tests/test_period.py index e0dad2995f91c..e301d59906627 100644 --- a/pandas/tseries/tests/test_period.py +++ b/pandas/tseries/tests/test_period.py @@ -3833,11 +3833,30 @@ def test_auto_conversion(self): series = Series(list(period_range('2000-01-01', periods=10, freq='D'))) self.assertEqual(series.dtype, 'object') + series = pd.Series([pd.Period('2011-01-01', freq='D'), + pd.Period('2011-02-01', freq='D')]) + self.assertEqual(series.dtype, 'object') + + def test_getitem(self): + self.assertEqual(self.series[1], pd.Period('2000-01-02', freq='D')) + + result = self.series[[2, 4]] + exp = pd.Series([pd.Period('2000-01-03', freq='D'), + pd.Period('2000-01-05', freq='D')], + index=[2, 4]) + self.assert_series_equal(result, exp) + self.assertEqual(result.dtype, 'object') + def test_constructor_cant_cast_period(self): with tm.assertRaises(TypeError): Series(period_range('2000-01-01', periods=10, freq='D'), dtype=float) + def test_constructor_cast_object(self): + s = Series(period_range('1/1/2000', periods=10), dtype=object) + exp = Series(period_range('1/1/2000', periods=10)) + tm.assert_series_equal(s, exp) + def test_series_comparison_scalars(self): val = pd.Period('2000-01-04', freq='D') result = self.series > val diff --git a/pandas/tseries/tests/test_timeseries.py b/pandas/tseries/tests/test_timeseries.py index 615390b5209b6..800c41cb77f2e 100644 --- a/pandas/tseries/tests/test_timeseries.py +++ b/pandas/tseries/tests/test_timeseries.py @@ -3881,8 +3881,17 @@ def test_auto_conversion(self): self.assertEqual(series.dtype, 'M8[ns]') def test_constructor_cant_cast_datetime64(self): - self.assertRaises(TypeError, Series, - date_range('1/1/2000', periods=10), dtype=float) + msg = "Cannot cast datetime64 to " + with tm.assertRaisesRegexp(TypeError, msg): + Series(date_range('1/1/2000', periods=10), dtype=float) + + with tm.assertRaisesRegexp(TypeError, msg): + Series(date_range('1/1/2000', periods=10), dtype=int) + + def test_constructor_cast_object(self): + s = Series(date_range('1/1/2000', periods=10), dtype=object) + exp = Series(date_range('1/1/2000', periods=10)) + tm.assert_series_equal(s, exp) def test_series_comparison_scalars(self): val = datetime(2000, 1, 4) @@ -3941,6 +3950,9 @@ def test_intercept_astype_object(self): df = DataFrame({'a': self.series, 'b': np.random.randn(len(self.series))}) + exp_dtypes = pd.Series([np.dtype('datetime64[ns]'), + np.dtype('float64')], index=['a', 'b']) + tm.assert_series_equal(df.dtypes, exp_dtypes) result = df.values.squeeze() self.assertTrue((result[:, 0] == expected.values).all())