|
1 | 1 | # coding=utf-8
|
2 | 2 | # pylint: disable-msg=E1101,W0612
|
3 | 3 |
|
4 |
| -from collections import OrderedDict |
| 4 | +import pytest |
| 5 | + |
5 | 6 | from datetime import datetime, timedelta
|
| 7 | +from collections import OrderedDict |
6 | 8 |
|
| 9 | +from numpy import nan |
7 | 10 | import numpy as np
|
8 | 11 | import numpy.ma as ma
|
9 |
| -import pytest |
10 |
| -from numpy import nan |
11 |
| - |
12 | 12 | import pandas as pd
|
13 |
| -import pandas.util.testing as tm |
14 |
| -from pandas import ( |
15 |
| - Categorical, DataFrame, Index, IntervalIndex, MultiIndex, NaT, Series, |
16 |
| - Timestamp, date_range, isna, period_range, timedelta_range |
17 |
| -) |
18 |
| -from pandas._libs import lib |
19 |
| -from pandas._libs.tslib import iNaT |
| 13 | + |
20 | 14 | from pandas.api.types import CategoricalDtype
|
21 |
| -from pandas.compat import PY36, long, lrange, range, zip |
22 | 15 | from pandas.core.dtypes.common import (
|
23 |
| - is_categorical_dtype, is_datetime64tz_dtype |
24 |
| -) |
| 16 | + is_categorical_dtype, |
| 17 | + is_datetime64tz_dtype) |
| 18 | +from pandas import (Index, Series, isna, date_range, Timestamp, |
| 19 | + NaT, period_range, timedelta_range, MultiIndex, |
| 20 | + IntervalIndex, Categorical, DataFrame) |
| 21 | +from pandas.core.arrays import period_array |
| 22 | +from pandas._libs import lib |
| 23 | +from pandas._libs.tslib import iNaT |
| 24 | + |
| 25 | +from pandas.compat import lrange, range, zip, long, PY36 |
25 | 26 | from pandas.util.testing import assert_series_equal
|
| 27 | +import pandas.util.testing as tm |
26 | 28 |
|
27 | 29 |
|
28 | 30 | class TestSeriesConstructors():
|
@@ -854,17 +856,33 @@ def test_construction_consistency(self):
|
854 | 856 | result = Series(s.values, dtype=s.dtype)
|
855 | 857 | tm.assert_series_equal(result, s)
|
856 | 858 |
|
| 859 | + def test_constructor_infer_period(self): |
| 860 | + data = [pd.Period('2000', 'D'), pd.Period('2001', 'D'), None] |
| 861 | + result = pd.Series(data) |
| 862 | + expected = pd.Series(period_array(data)) |
| 863 | + tm.assert_series_equal(result, expected) |
| 864 | + assert result.dtype == 'Period[D]' |
| 865 | + |
| 866 | + data = np.asarray(data, dtype=object) |
| 867 | + tm.assert_series_equal(result, expected) |
| 868 | + assert result.dtype == 'Period[D]' |
| 869 | + |
| 870 | + def test_constructor_period_incompatible_frequency(self): |
| 871 | + data = [pd.Period('2000', 'D'), pd.Period('2001', 'A')] |
| 872 | + result = pd.Series(data) |
| 873 | + assert result.dtype == object |
| 874 | + assert result.tolist() == data |
| 875 | + |
857 | 876 | def test_constructor_periodindex(self):
|
858 | 877 | # GH7932
|
859 | 878 | # converting a PeriodIndex when put in a Series
|
860 | 879 |
|
861 | 880 | pi = period_range('20130101', periods=5, freq='D')
|
862 | 881 | s = Series(pi)
|
| 882 | + assert s.dtype == 'Period[D]' |
863 | 883 | expected = Series(pi.astype(object))
|
864 | 884 | assert_series_equal(s, expected)
|
865 | 885 |
|
866 |
| - assert s.dtype == 'object' |
867 |
| - |
868 | 886 | def test_constructor_dict(self):
|
869 | 887 | d = {'a': 0., 'b': 1., 'c': 2.}
|
870 | 888 | result = Series(d, index=['b', 'c', 'd', 'a'])
|
@@ -1139,7 +1157,12 @@ def test_convert_non_ns(self):
|
1139 | 1157 | def test_constructor_cant_cast_datetimelike(self, index):
|
1140 | 1158 |
|
1141 | 1159 | # floats are not ok
|
1142 |
| - msg = "Cannot cast {} to ".format(type(index).__name__) |
| 1160 | + msg = "Cannot cast {}.*? to ".format( |
| 1161 | + # strip Index to convert PeriodIndex -> Period |
| 1162 | + # We don't care whether the error message says |
| 1163 | + # PeriodIndex or PeriodArray |
| 1164 | + type(index).__name__.rstrip("Index") |
| 1165 | + ) |
1143 | 1166 | with tm.assert_raises_regex(TypeError, msg):
|
1144 | 1167 | Series(index, dtype=float)
|
1145 | 1168 |
|
|
0 commit comments